comparison flys-client/src/main/java/org/dive4elements/river/client/server/ChartOutputServiceImpl.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/server/ChartOutputServiceImpl.java@2867a0192aed
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.server;
2
3 import java.io.OutputStream;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.w3c.dom.Document;
9
10 import org.apache.log4j.Logger;
11
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
17
18 import de.intevation.artifacts.httpclient.http.HttpClient;
19 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
20
21
22 /**
23 * This service is used to request a chart from the artifact server. The
24 * response is directed directly to the output stream, so the image that is
25 * retrieved is displayed in the UI afterwards.
26 *
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
28 */
29 public class ChartOutputServiceImpl
30 extends HttpServlet
31 {
32 private static final Logger logger =
33 Logger.getLogger(ChartOutputServiceImpl.class);
34
35
36 public void doGet(HttpServletRequest req, HttpServletResponse resp) {
37 logger.info("ChartOutputServiceImpl.doGet");
38
39 try {
40 OutputStream out = resp.getOutputStream();
41
42 String url = getServletContext().getInitParameter("server-url");
43
44 String uuid = req.getParameter("uuid");
45 String type = req.getParameter("type");
46 String locale = req.getParameter("locale");
47
48 prepareHeader(req, resp);
49
50 Document request = ClientProtocolUtils.newOutCollectionDocument(
51 uuid, type, type,
52 ChartServiceHelper.getChartAttributes(prepareChartAttributes(req)));
53
54 HttpClient client = new HttpClientImpl(url, locale);
55 client.collectionOut(request, uuid, "chart", out);
56
57 out.close();
58 out.flush();
59 }
60 catch (IOException ioe) {
61 logger.error(ioe, ioe);
62 }
63 catch (Exception e) {
64 logger.error(e, e);
65 }
66 }
67
68
69 protected Map<String, String> prepareChartAttributes(HttpServletRequest req) {
70 Map<String, String> attr = new HashMap<String, String>();
71
72 Map params = req.getParameterMap();
73
74 attr.put("width", req.getParameter("width"));
75 attr.put("height", req.getParameter("height"));
76 attr.put("minx", req.getParameter("minx"));
77 attr.put("maxx", req.getParameter("maxx"));
78 attr.put("miny", req.getParameter("miny"));
79 attr.put("maxy", req.getParameter("maxy"));
80 attr.put("format", req.getParameter("format"));
81 attr.put("km", req.getParameter("currentKm"));
82
83 if (logger.isDebugEnabled()) {
84 logger.debug("====== ZOOM VALUES =======");
85 logger.debug(" min x: " + req.getParameter("minx"));
86 logger.debug(" max x: " + req.getParameter("maxx"));
87 logger.debug(" min y: " + req.getParameter("miny"));
88 logger.debug(" max y: " + req.getParameter("maxy"));
89 }
90
91 return attr;
92 }
93
94
95 protected void prepareHeader(
96 HttpServletRequest req,
97 HttpServletResponse resp
98 ) {
99 String export = req.getParameter("export");
100
101 if (export != null && export.equals("true")) {
102 String format = req.getParameter("format");
103
104 if (format == null || format.length() == 0) {
105 format = "png";
106 }
107
108 String fn = "chart_export" + getFileExtension(format);
109
110 resp.setHeader("Content-Disposition", "attachment;filename=" + fn);
111 resp.setHeader("Content-Type", getMimeType(format));
112 }
113 }
114
115
116 protected String getFileExtension(String format) {
117 if (format.equals("png")) {
118 return ".png";
119 }
120 else if (format.equals("pdf")) {
121 return ".pdf";
122 }
123 else if (format.equals("svg")) {
124 return ".svg";
125 }
126 else if (format.equals("csv")) {
127 return ".csv";
128 }
129
130 return ".png";
131 }
132
133
134 protected String getMimeType(String format) {
135 if (format.equals("png")) {
136 return "image/png";
137 }
138 else if (format.equals("pdf")) {
139 return "application/pdf";
140 }
141 else if (format.equals("svg")) {
142 return "svg+xml";
143 }
144 else if (format.equals("csv")) {
145 return "text/plain";
146 }
147
148 return "image/png";
149 }
150 }
151 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org