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