annotate gwt-client/src/main/java/org/dive4elements/river/client/server/SQKMChartServiceImpl.java @ 9726:0a5239a1e46e 3.2.x

Upgrade to Log4j 2
author Tom Gottfried <tom@intevation.de>
date Wed, 02 Mar 2022 10:26:50 +0100
parents ea9eef426962
children
rev   line source
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5993
ea9eef426962 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5861
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5993
ea9eef426962 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5861
diff changeset
6 * documentation coming with Dive4Elements River for details.
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
9 package org.dive4elements.river.client.server;
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
10
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5993
diff changeset
11 import org.apache.logging.log4j.Logger;
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5993
diff changeset
12 import org.apache.logging.log4j.LogManager;
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import java.io.IOException;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15 import java.io.InputStream;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
16 import java.io.OutputStream;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
17
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
18 import javax.servlet.http.HttpServlet;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
19 import javax.servlet.http.HttpServletRequest;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
20 import javax.servlet.http.HttpServletResponse;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
22 import org.w3c.dom.Document;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23 import org.w3c.dom.Element;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 import org.w3c.dom.NodeList;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
26 import org.dive4elements.artifacts.common.utils.XMLUtils;
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
28 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
29 import org.dive4elements.artifacts.httpclient.http.HttpClient;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
30 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
32 import org.dive4elements.artifacts.httpclient.http.response.StreamResponseHandler;
2953
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34 public class SQKMChartServiceImpl
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 extends HttpServlet
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37 private static final Logger log =
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5993
diff changeset
38 LogManager.getLogger(FixingsKMChartServiceImpl.class);
2953
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 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
41
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 public SQKMChartServiceImpl() {
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
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 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
46
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 log.info("SQKMChartServiceImpl.doGet");
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 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
50 String locale = req.getParameter("locale");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 String filter = req.getParameter("filter");
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 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
54 log.warn("Missing 'filter' parameter.");
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 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
59 locale = "de";
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 }
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 Document filterDoc = XMLUtils.jsonToXML(filter);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 if (filterDoc == null) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 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
66 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 InputStream in;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71 try {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 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
73 in = (InputStream)client.callService(
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74 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
75 SERVICE_NAME,
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 filterDoc,
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 new StreamResponseHandler());
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 catch (ConnectionException ce) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 log.error(ce);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81 return;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 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
85
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 try {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 OutputStream out = resp.getOutputStream();
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 byte [] buf = new byte[4096];
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 int i = -1;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 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
92 out.write(buf, 0, i);
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 out.flush();
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 catch (IOException ioe) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97 log.error(ioe);
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99 finally {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 try { in.close(); }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 catch (IOException ioe) { /* ignored */ }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
103 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105 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
106
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 NodeList formats = document.getElementsByTagName("format");
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 String format = "png";
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 if (formats.getLength() > 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
112 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
113 if (type.length() > 0) {
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
114 format = type;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
115 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
116 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
117
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
118 return "image/" + format;
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
119 }
75e9eab05ee4 Added overview chart to sq relation period input panel.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
120 }

http://dive4elements.wald.intevation.org