Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 2502:328aa273ef3b
Call MapPrinter directly and not via the MapFish Print servlet.
flys-client/trunk@4347 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 04 May 2012 15:19:30 +0000 |
parents | |
children | e3bd1f412421 |
comparison
equal
deleted
inserted
replaced
2501:87634a9a8478 | 2502:328aa273ef3b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 import java.io.FileOutputStream; | |
7 import java.io.FileInputStream; | |
8 import java.io.BufferedInputStream; | |
9 import java.io.BufferedOutputStream; | |
10 import java.io.OutputStream; | |
11 | |
12 import javax.servlet.ServletException; | |
13 | |
14 import javax.servlet.http.HttpServlet; | |
15 import javax.servlet.http.HttpServletRequest; | |
16 import javax.servlet.http.HttpServletResponse; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 | |
20 import org.mapfish.print.MapPrinter; | |
21 | |
22 import org.mapfish.print.output.OutputFactory; | |
23 import org.mapfish.print.output.OutputFormat; | |
24 | |
25 import org.mapfish.print.utils.PJsonObject; | |
26 | |
27 public class MapPrintServiceImpl | |
28 extends HttpServlet | |
29 { | |
30 private static final Logger log = | |
31 Logger.getLogger(MapPrintServiceImpl.class); | |
32 | |
33 private static final String DEMO_JSON = | |
34 "{" + | |
35 "\"layout\": \"A4 portrait\"," + | |
36 "\"title\": \"A simple example\"," + | |
37 "\"srs\": \"EPSG:4326\"," + | |
38 "\"dpi\": 254," + | |
39 "\"units\": \"degrees\"," + | |
40 "\"outputFormat\": \"pdf\"," + | |
41 "\"layers\": [{" + | |
42 " \"type\": \"WMS\"," + | |
43 " \"layers\": [\"basic\"]," + | |
44 " \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," + | |
45 " \"format\": \"image/jpeg\"" + | |
46 "}," + | |
47 "{" + | |
48 " \"type\": \"WMS\"," + | |
49 " \"layers\": [\"routes\"]," + | |
50 " \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," + | |
51 " \"format\": \"image/png\"" + | |
52 "}, {" + | |
53 "\"opacity\": 0.5," + | |
54 "\"customParams\": {}," + | |
55 "\"type\": \"Vector\"," + | |
56 "\"styles\": {" + | |
57 " \"1\": {" + | |
58 " \"fillColor\": \"red\"," + | |
59 " \"strokeColor\": \"red\"," + | |
60 " \"fillOpacity\": 0.4000000000000001," + | |
61 " \"hoverFillColor\": \"white\"," + | |
62 " \"hoverFillOpacity\": 0.8000000000000002," + | |
63 " \"strokeOpacity\": 1," + | |
64 " \"strokeWidth\": 0.5," + | |
65 " \"strokeLinecap\": \"round\"," + | |
66 " \"strokeDashstyle\": \"solid\"," + | |
67 " \"hoverStrokeColor\": \"red\"," + | |
68 " \"hoverStrokeOpacity\": 1.0," + | |
69 " \"hoverStrokeWidth\": 0.2," + | |
70 " \"pointRadius\": 6," + | |
71 " \"hoverPointRadius\": 1," + | |
72 " \"hoverPointUnit\": \"%\"," + | |
73 " \"pointerEvents\": \"visiblePainted\"," + | |
74 " \"cursor\": \"inherit\"" + | |
75 " }" + | |
76 "}," + | |
77 "\"styleProperty\": \"_style\"," + | |
78 "\"geoJson\": {" + | |
79 " \"type\": \"FeatureCollection\"," + | |
80 " \"features\": [{" + | |
81 " \"type\": \"Feature\"," + | |
82 " \"id\": \"OpenLayers.Feature.Vector_243\"," + | |
83 " \"properties\": {" + | |
84 " \"_style\": 1," + | |
85 " \"name\": \"\"," + | |
86 " \"description\": \"\"," + | |
87 " \"ext-comp-1048\": \"default\"" + | |
88 " }," + | |
89 " \"geometry\": {" + | |
90 " \"type\": \"Polygon\"," + | |
91 " \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" + | |
92 " }" + | |
93 " }]" + | |
94 "}," + | |
95 "\"name\": \"Cosmetic\"" + | |
96 " }" + | |
97 "]," + | |
98 "\"pages\": [" + | |
99 " {" + | |
100 " \"center\": [6, 45.5]," + | |
101 " \"scale\": 4000000," + | |
102 " \"mapTitle\": \"First map\"," + | |
103 " \"comment\": \"This is the first page selected by the user.\"," + | |
104 " \"rotation\": 0," + | |
105 " \"data\": {" + | |
106 " \"data\": [" + | |
107 " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + | |
108 " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + | |
109 " ]," + | |
110 " \"columns\": [\"id\", \"name\", \"icon\"]" + | |
111 " }" + | |
112 " },{" + | |
113 " \"center\": [6, 45.5]," + | |
114 " \"scale\": 4000000," + | |
115 " \"mapTitle\": \"First map\"," + | |
116 " \"comment\": \"This is the first page selected by the user.\"," + | |
117 " \"rotation\": 0," + | |
118 " \"data\": {" + | |
119 " \"data\": [" + | |
120 " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + | |
121 " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + | |
122 " ]," + | |
123 " \"columns\": [\"id\", \"name\", \"icon\"]" + | |
124 " }" + | |
125 " }" + | |
126 "]" + | |
127 "}"; | |
128 | |
129 | |
130 private MapPrinter getMapPrinter() throws ServletException, IOException { | |
131 String configPath = getInitParameter("config"); | |
132 if (configPath == null) { | |
133 throw new ServletException("Missing configuration in web.xml"); | |
134 } | |
135 | |
136 File configFile = new File(configPath); | |
137 if (!configFile.isAbsolute()) { | |
138 configFile = new File(getServletContext().getRealPath(configPath)); | |
139 } | |
140 | |
141 if (!configFile.isFile() || !configFile.canRead()) { | |
142 throw new ServletException("Cannot read '" + configFile + "'"); | |
143 } | |
144 | |
145 return new MapPrinter(configFile); | |
146 } | |
147 | |
148 @Override | |
149 public void doGet(HttpServletRequest req, HttpServletResponse resp) | |
150 throws ServletException, IOException | |
151 { | |
152 log.info("MapPrintServiceImpl.doGet"); | |
153 | |
154 PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON); | |
155 | |
156 MapPrinter printer = getMapPrinter(); | |
157 | |
158 OutputFormat outputFormat = OutputFactory.create( | |
159 printer.getConfig(), jsonSpec); | |
160 | |
161 resp.setHeader("Content-Disposition", "attachment;filename=print.pdf"); | |
162 resp.setHeader("Content-Type", "application/pdf"); | |
163 | |
164 // XXX: Streaming the generated PDF directly | |
165 // to the request out does not work. :-/ | |
166 File tmpFile = File.createTempFile("map-printing", null); | |
167 | |
168 try { | |
169 OutputStream out = | |
170 new BufferedOutputStream( | |
171 new FileOutputStream(tmpFile)); | |
172 try { | |
173 outputFormat.print(printer, jsonSpec, out, ""); | |
174 out.flush(); | |
175 } | |
176 catch (Exception e) { | |
177 log.error(e); | |
178 throw new ServletException(e); | |
179 } | |
180 finally { | |
181 printer.stop(); | |
182 out.close(); | |
183 } | |
184 InputStream in = | |
185 new BufferedInputStream( | |
186 new FileInputStream(tmpFile)); | |
187 out = resp.getOutputStream(); | |
188 try { | |
189 byte [] buf = new byte[4096]; | |
190 int r; | |
191 while ((r = in.read(buf)) >= 0) { | |
192 out.write(buf, 0, r); | |
193 } | |
194 out.flush(); | |
195 } | |
196 finally { | |
197 in.close(); | |
198 out.close(); | |
199 } | |
200 } | |
201 finally { | |
202 if (tmpFile.exists()) { | |
203 tmpFile.delete(); | |
204 } | |
205 } | |
206 } | |
207 } | |
208 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |