comparison flys-client/src/main/java/de/intevation/flys/client/server/ChartOutputServiceImpl.java @ 280:b493d606fdef

Enabled the client to request charts in different sizes. flys-client/trunk@1909 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 12 May 2011 09:14:46 +0000
parents 6838e4112eeb
children 8ea213bd8fba
comparison
equal deleted inserted replaced
279:e763d8efd42f 280:b493d606fdef
2 2
3 import java.io.OutputStream; 3 import java.io.OutputStream;
4 import java.io.IOException; 4 import java.io.IOException;
5 5
6 import org.w3c.dom.Document; 6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
7 8
8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpServletResponse;
11 12
13 import de.intevation.artifacts.common.ArtifactNamespaceContext;
12 import de.intevation.artifacts.common.utils.ClientProtocolUtils; 14 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
15 import de.intevation.artifacts.common.utils.XMLUtils;
16 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
13 17
14 import de.intevation.artifacts.httpclient.http.HttpClient; 18 import de.intevation.artifacts.httpclient.http.HttpClient;
15 import de.intevation.artifacts.httpclient.http.HttpClientImpl; 19 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
16 20
17 21
23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
24 */ 28 */
25 public class ChartOutputServiceImpl 29 public class ChartOutputServiceImpl
26 extends HttpServlet 30 extends HttpServlet
27 { 31 {
32 /** The default chart width if no value is specified in the request.*/
33 public static final int DEFAULT_CHART_WIDTH = 600;
34
35 /** The default chart height if no value is specified in the request.*/
36 public static final int DEFAULT_CHART_HEIGHT = 400;
37
38
28 public void doGet(HttpServletRequest req, HttpServletResponse resp) { 39 public void doGet(HttpServletRequest req, HttpServletResponse resp) {
29 System.out.println("ChartOutputServiceImpl.doGet"); 40 System.out.println("ChartOutputServiceImpl.doGet");
30 41
31 try { 42 try {
32 OutputStream out = resp.getOutputStream(); 43 OutputStream out = resp.getOutputStream();
33 44
34 String serverUrl = req.getParameter("server"); 45 String serverUrl = req.getParameter("server");
35 String uuid = req.getParameter("uuid"); 46 String uuid = req.getParameter("uuid");
36 String type = req.getParameter("type"); 47 String type = req.getParameter("type");
37 String locale = req.getParameter("locale"); 48 String locale = req.getParameter("locale");
38 String mimeType = "image/png";
39 49
40 Document request = ClientProtocolUtils.newOutCollectionDocument( 50 Document request = ClientProtocolUtils.newOutCollectionDocument(
41 uuid, type); 51 uuid, type, getChartAttributes(req));
42 52
43 HttpClient client = new HttpClientImpl(serverUrl, locale); 53 HttpClient client = new HttpClientImpl(serverUrl, locale);
44 client.collectionOut(request, uuid, "chart", out); 54 client.collectionOut(request, uuid, "chart", out);
45 55
46 out.close(); 56 out.close();
47 out.flush(); 57 out.flush();
48 } 58 }
49 catch (IOException ioe) { 59 catch (IOException ioe) {
50 System.err.println(ioe.getMessage()); 60 System.err.println(ioe.getMessage());
51 } 61 }
62 catch (Exception e) {
63 System.err.println(e.getMessage());
64 }
65 }
66
67
68 /**
69 * This method returns a document which might contain parameters to adjust
70 * chart settings. The document is created using the information that are
71 * contained in the request object.
72 *
73 * @param req The request document.
74 *
75 * @return a document to adjust chart settings.
76 */
77 protected Document getChartAttributes(HttpServletRequest req) {
78 Document doc = XMLUtils.newDocument();
79
80 ElementCreator ec = new ElementCreator(
81 doc,
82 ArtifactNamespaceContext.NAMESPACE_URI,
83 ArtifactNamespaceContext.NAMESPACE_PREFIX);
84
85 appendChartSize(req, doc, ec);
86
87 return doc;
88 }
89
90
91 /**
92 * This method extracts the size (width/height) of a chart from request
93 * object and append those values - if they exist - to the attribute
94 * document used to adjust chart settings.
95 *
96 * @param req The request object that might contain the chart size.
97 * @param doc The attribute document used to adjust chart settings.
98 * @param ec The ElementCreator that might be used to create new Elements.
99 */
100 protected void appendChartSize(
101 HttpServletRequest req,
102 Document doc,
103 ElementCreator ec)
104 {
105 Element size = ec.create("size");
106
107 String width = req.getParameter("width");
108 String height = req.getParameter("height");
109
110 if (width == null || height == null) {
111 width = Integer.toString(DEFAULT_CHART_WIDTH);
112 height = Integer.toString(DEFAULT_CHART_HEIGHT);
113 }
114
115 ec.addAttr(size, "width", width, true);
116 ec.addAttr(size, "height", height, true);
117
118 doc.appendChild(size);
52 } 119 }
53 } 120 }
121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org