Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java Fri May 04 15:19:30 2012 +0000 @@ -0,0 +1,208 @@ +package de.intevation.flys.client.server; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.FileOutputStream; +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.OutputStream; + +import javax.servlet.ServletException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; + +import org.mapfish.print.MapPrinter; + +import org.mapfish.print.output.OutputFactory; +import org.mapfish.print.output.OutputFormat; + +import org.mapfish.print.utils.PJsonObject; + +public class MapPrintServiceImpl +extends HttpServlet +{ + private static final Logger log = + Logger.getLogger(MapPrintServiceImpl.class); + + private static final String DEMO_JSON = + "{" + + "\"layout\": \"A4 portrait\"," + + "\"title\": \"A simple example\"," + + "\"srs\": \"EPSG:4326\"," + + "\"dpi\": 254," + + "\"units\": \"degrees\"," + + "\"outputFormat\": \"pdf\"," + + "\"layers\": [{" + + " \"type\": \"WMS\"," + + " \"layers\": [\"basic\"]," + + " \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," + + " \"format\": \"image/jpeg\"" + + "}," + + "{" + + " \"type\": \"WMS\"," + + " \"layers\": [\"routes\"]," + + " \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," + + " \"format\": \"image/png\"" + + "}, {" + + "\"opacity\": 0.5," + + "\"customParams\": {}," + + "\"type\": \"Vector\"," + + "\"styles\": {" + + " \"1\": {" + + " \"fillColor\": \"red\"," + + " \"strokeColor\": \"red\"," + + " \"fillOpacity\": 0.4000000000000001," + + " \"hoverFillColor\": \"white\"," + + " \"hoverFillOpacity\": 0.8000000000000002," + + " \"strokeOpacity\": 1," + + " \"strokeWidth\": 0.5," + + " \"strokeLinecap\": \"round\"," + + " \"strokeDashstyle\": \"solid\"," + + " \"hoverStrokeColor\": \"red\"," + + " \"hoverStrokeOpacity\": 1.0," + + " \"hoverStrokeWidth\": 0.2," + + " \"pointRadius\": 6," + + " \"hoverPointRadius\": 1," + + " \"hoverPointUnit\": \"%\"," + + " \"pointerEvents\": \"visiblePainted\"," + + " \"cursor\": \"inherit\"" + + " }" + + "}," + + "\"styleProperty\": \"_style\"," + + "\"geoJson\": {" + + " \"type\": \"FeatureCollection\"," + + " \"features\": [{" + + " \"type\": \"Feature\"," + + " \"id\": \"OpenLayers.Feature.Vector_243\"," + + " \"properties\": {" + + " \"_style\": 1," + + " \"name\": \"\"," + + " \"description\": \"\"," + + " \"ext-comp-1048\": \"default\"" + + " }," + + " \"geometry\": {" + + " \"type\": \"Polygon\"," + + " \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" + + " }" + + " }]" + + "}," + + "\"name\": \"Cosmetic\"" + + " }" + + "]," + + "\"pages\": [" + + " {" + + " \"center\": [6, 45.5]," + + " \"scale\": 4000000," + + " \"mapTitle\": \"First map\"," + + " \"comment\": \"This is the first page selected by the user.\"," + + " \"rotation\": 0," + + " \"data\": {" + + " \"data\": [" + + " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + + " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + + " ]," + + " \"columns\": [\"id\", \"name\", \"icon\"]" + + " }" + + " },{" + + " \"center\": [6, 45.5]," + + " \"scale\": 4000000," + + " \"mapTitle\": \"First map\"," + + " \"comment\": \"This is the first page selected by the user.\"," + + " \"rotation\": 0," + + " \"data\": {" + + " \"data\": [" + + " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + + " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + + " ]," + + " \"columns\": [\"id\", \"name\", \"icon\"]" + + " }" + + " }" + + "]" + + "}"; + + + private MapPrinter getMapPrinter() throws ServletException, IOException { + String configPath = getInitParameter("config"); + if (configPath == null) { + throw new ServletException("Missing configuration in web.xml"); + } + + File configFile = new File(configPath); + if (!configFile.isAbsolute()) { + configFile = new File(getServletContext().getRealPath(configPath)); + } + + if (!configFile.isFile() || !configFile.canRead()) { + throw new ServletException("Cannot read '" + configFile + "'"); + } + + return new MapPrinter(configFile); + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + log.info("MapPrintServiceImpl.doGet"); + + PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON); + + MapPrinter printer = getMapPrinter(); + + OutputFormat outputFormat = OutputFactory.create( + printer.getConfig(), jsonSpec); + + resp.setHeader("Content-Disposition", "attachment;filename=print.pdf"); + resp.setHeader("Content-Type", "application/pdf"); + + // XXX: Streaming the generated PDF directly + // to the request out does not work. :-/ + File tmpFile = File.createTempFile("map-printing", null); + + try { + OutputStream out = + new BufferedOutputStream( + new FileOutputStream(tmpFile)); + try { + outputFormat.print(printer, jsonSpec, out, ""); + out.flush(); + } + catch (Exception e) { + log.error(e); + throw new ServletException(e); + } + finally { + printer.stop(); + out.close(); + } + InputStream in = + new BufferedInputStream( + new FileInputStream(tmpFile)); + out = resp.getOutputStream(); + try { + byte [] buf = new byte[4096]; + int r; + while ((r = in.read(buf)) >= 0) { + out.write(buf, 0, r); + } + out.flush(); + } + finally { + in.close(); + out.close(); + } + } + finally { + if (tmpFile.exists()) { + tmpFile.delete(); + } + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :