annotate flys-client/src/main/java/de/intevation/flys/client/server/SQKMChartServiceImpl.java @ 5720:aa1a43f5c03a

Send errors as plaintext and not as pdf documents
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 15 Apr 2013 18:32:37 +0200
parents 75e9eab05ee4
children
rev   line source
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
2
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
3 import org.apache.log4j.Logger;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
4
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
5 import java.io.IOException;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
6 import java.io.InputStream;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
7 import java.io.OutputStream;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
8
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
9 import javax.servlet.http.HttpServlet;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
10 import javax.servlet.http.HttpServletRequest;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
11 import javax.servlet.http.HttpServletResponse;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
12
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13 import org.w3c.dom.Document;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import org.w3c.dom.Element;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15 import org.w3c.dom.NodeList;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
16
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
17 import de.intevation.artifacts.common.utils.XMLUtils;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
18
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
19 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
20 import de.intevation.artifacts.httpclient.http.HttpClient;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
22
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23 import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25 public class SQKMChartServiceImpl
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
26 extends HttpServlet
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27 {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
28 private static final Logger log =
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
29 Logger.getLogger(FixingsKMChartServiceImpl.class);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 public static final String SERVICE_NAME = "sq-km-chart";
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33 public SQKMChartServiceImpl() {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 public void doGet(HttpServletRequest req, HttpServletResponse resp) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 log.info("SQKMChartServiceImpl.doGet");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 String url = getServletContext().getInitParameter("server-url");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
41 String locale = req.getParameter("locale");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 String filter = req.getParameter("filter");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44 if (filter == null || filter.length() == 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 log.warn("Missing 'filter' parameter.");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
46 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 if (locale == null || locale.length() == 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 locale = "de";
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
53 Document filterDoc = XMLUtils.jsonToXML(filter);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
54
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 if (filterDoc == null) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 log.warn("Creating filter document failed.");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
59
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 InputStream in;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
61
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
62 try {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 HttpClient client = new HttpClientImpl(url, locale);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 in = (InputStream)client.callService(
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 url, // XXX: Why? The URL is passed by construction already.
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 SERVICE_NAME,
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 filterDoc,
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68 new StreamResponseHandler());
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70 catch (ConnectionException ce) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71 log.error(ce);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 resp.setHeader("Content-Type", guessMIMEType(filterDoc));
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 try {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 OutputStream out = resp.getOutputStream();
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 byte [] buf = new byte[4096];
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81 int i = -1;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 while ((i = in.read(buf)) >= 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 out.write(buf, 0, i);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 out.flush();
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 catch (IOException ioe) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 log.error(ioe);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 finally {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 try { in.close(); }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
92 catch (IOException ioe) { /* ignored */ }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
94 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
95
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
96 protected static String guessMIMEType(Document document) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98 NodeList formats = document.getElementsByTagName("format");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 String format = "png";
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102 if (formats.getLength() > 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
103 String type = ((Element)formats.item(0)).getAttribute("type");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104 if (type.length() > 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105 format = type;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
106 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
108
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
109 return "image/" + format;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
110 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
111 }

http://dive4elements.wald.intevation.org