# HG changeset patch # User Sascha L. Teichmann # Date 1336144770 0 # Node ID 328aa273ef3bc3b0ef32ebc6c13b21aac4d0e63c # Parent 87634a9a8478136d43923f32c4ee7f843aee8855 Call MapPrinter directly and not via the MapFish Print servlet. flys-client/trunk@4347 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 87634a9a8478 -r 328aa273ef3b flys-client/ChangeLog --- a/flys-client/ChangeLog Thu May 03 08:57:35 2012 +0000 +++ b/flys-client/ChangeLog Fri May 04 15:19:30 2012 +0000 @@ -1,3 +1,16 @@ +2012-05-04 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java: + New. Call the MapPrint of MapFish Print directly. This removes the + need of stupid passing around a complex JSON document to another servlet. + The generation of the PDF is done nearly the same way the ShellMapPrinter + works without the shell overhead. + + * src/main/webapp/WEB-INF/web.xml: Register new servlet and remove old + MapFish Print one. + + * src/main/webapp/WEB-INF/config.yaml: Fix some image refs. + 2012-05-03 Felix Wolfsteller * src/main/java/de/intevation/flys/client/client/FLYSConstants.java, @@ -28,7 +41,6 @@ attribute 'newTab' that could be used to open the link in a new tab (HTML target='_blank'). ->>>>>>> .r4339 2012-05-02 Felix Wolfsteller * src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java: diff -r 87634a9a8478 -r 328aa273ef3b flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java --- /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 : diff -r 87634a9a8478 -r 328aa273ef3b flys-client/src/main/webapp/WEB-INF/config.yaml --- a/flys-client/src/main/webapp/WEB-INF/config.yaml Thu May 03 08:57:35 2012 +0000 +++ b/flys-client/src/main/webapp/WEB-INF/config.yaml Fri May 04 15:19:30 2012 +0000 @@ -84,7 +84,7 @@ maxHeight: 160 spacingAfter: 100 align: center - url: 'file://${configDir}/logo-camptocamp-transparent.png' + url: 'file://${configDir}/../images/flys_logo.gif' - !text font: Helvetica fontSize: 14 @@ -107,7 +107,7 @@ maxWidth: 40 backgroundColor: #FF0000 align: left - url: '${configDir}/small-logo-camptocamp.png' + url: '${configDir}/../images/properties.gif' - !text backgroundColor: #FF0000 text: ©Camptocamp SA diff -r 87634a9a8478 -r 328aa273ef3b flys-client/src/main/webapp/WEB-INF/web.xml --- a/flys-client/src/main/webapp/WEB-INF/web.xml Thu May 03 08:57:35 2012 +0000 +++ b/flys-client/src/main/webapp/WEB-INF/web.xml Fri May 04 15:19:30 2012 +0000 @@ -238,6 +238,12 @@ de.intevation.flys.client.server.ChartOutputServiceImpl + + ChartOutputService + /flys/chart + + + MapOutputService de.intevation.flys.client.server.MapOutputServiceImpl @@ -248,11 +254,22 @@ /flys/map + + + MapPrintService + de.intevation.flys.client.server.MapPrintServiceImpl + + config + WEB-INF/config.yaml + + + - ChartOutputService - /flys/chart + MapPrintService + /flys/map-print + FixingsKMChartService de.intevation.flys.client.server.FixingsKMChartServiceImpl @@ -423,20 +440,6 @@ /flys/fileupload - - mapfish.print - org.mapfish.print.servlet.MapPrinterServlet - - config - config.yaml - - - - - mapfish.print - /flys/map-print - - FLYS.html