Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/FixingsKMChartServiceImpl.java @ 2488:88d44cae592e
Added new proxy servlet to bridge the fixing km chart requests to the artefact server.
flys-client/trunk@4275 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 19 Apr 2012 11:00:18 +0000 |
parents | |
children | f905c936ffff |
comparison
equal
deleted
inserted
replaced
2487:75d11997921c | 2488:88d44cae592e |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import de.intevation.artifacts.common.utils.XMLUtils; | |
4 | |
5 import de.intevation.artifacts.httpclient.http.HttpClient; | |
6 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
7 | |
8 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
9 | |
10 import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; | |
11 | |
12 import java.io.IOException; | |
13 import java.io.InputStream; | |
14 import java.io.OutputStream; | |
15 | |
16 import javax.servlet.http.HttpServlet; | |
17 import javax.servlet.http.HttpServletRequest; | |
18 import javax.servlet.http.HttpServletResponse; | |
19 | |
20 import org.apache.log4j.Logger; | |
21 | |
22 import org.w3c.dom.Document; | |
23 import org.w3c.dom.Element; | |
24 import org.w3c.dom.NodeList; | |
25 | |
26 public class FixingsKMChartServiceImpl | |
27 extends HttpServlet | |
28 { | |
29 private static final Logger log = | |
30 Logger.getLogger(FixingsKMChartServiceImpl.class); | |
31 | |
32 public static final String SERVICE_NAME = "fixings-km-chart"; | |
33 | |
34 public FixingsKMChartServiceImpl() { | |
35 } | |
36 | |
37 public void doGet(HttpServletRequest req, HttpServletResponse resp) { | |
38 | |
39 log.info("FixingsKMChartServiceImpl.doGet"); | |
40 | |
41 String url = getServletContext().getInitParameter("server-url"); | |
42 String locale = req.getParameter("locale"); | |
43 String filter = req.getParameter("filter"); | |
44 | |
45 if (filter == null || filter.length() == 0) { | |
46 log.warn("Missing 'filter' parameter."); | |
47 return; | |
48 } | |
49 | |
50 if (locale == null || locale.length() == 0) { | |
51 locale = "de"; | |
52 } | |
53 | |
54 Document filterDoc = XMLUtils.jsonToXML(filter); | |
55 | |
56 if (filterDoc == null) { | |
57 log.warn("Creating filter document failed."); | |
58 return; | |
59 } | |
60 | |
61 HttpClient client; | |
62 | |
63 client = new HttpClientImpl(url, locale); | |
64 | |
65 resp.setHeader("Content-Type", guessMIMEType(filterDoc)); | |
66 | |
67 InputStream in; | |
68 | |
69 try { | |
70 in = (InputStream)client.callService( | |
71 url, // XXX: Why? The URL is passed by construction already. | |
72 SERVICE_NAME, | |
73 filterDoc, | |
74 new StreamResponseHandler()); | |
75 } | |
76 catch (ConnectionException ce) { | |
77 log.error(ce); | |
78 return; | |
79 } | |
80 | |
81 try { | |
82 OutputStream out = resp.getOutputStream(); | |
83 | |
84 byte [] buf = new byte[4096]; | |
85 int i = -1; | |
86 while ((i = in.read(buf)) >= 0) { | |
87 out.write(buf, 0, i); | |
88 } | |
89 out.flush(); | |
90 } | |
91 catch (IOException ioe) { | |
92 log.error(ioe); | |
93 } | |
94 finally { | |
95 try { in.close(); } | |
96 catch (IOException ioe) { /* ignored */ } | |
97 } | |
98 } | |
99 | |
100 protected static String guessMIMEType(Document document) { | |
101 | |
102 NodeList formats = document.getElementsByTagName("format"); | |
103 | |
104 String format = "png"; | |
105 | |
106 if (formats.getLength() > 0) { | |
107 String type = ((Element)formats.item(0)).getAttribute("type"); | |
108 if (type.length() > 0) { | |
109 format = type; | |
110 } | |
111 } | |
112 | |
113 return "image/" + format; | |
114 } | |
115 } | |
116 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |