changeset 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 87634a9a8478
children e3bd1f412421
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java flys-client/src/main/webapp/WEB-INF/config.yaml flys-client/src/main/webapp/WEB-INF/web.xml
diffstat 4 files changed, 242 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- 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	<sascha.teichmann@intevation.de>
+
+	* 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	<felix.wolfsteller@intevation.de>
 
 	* 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	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.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 :
--- 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
--- 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 @@
     <servlet-class>de.intevation.flys.client.server.ChartOutputServiceImpl</servlet-class>
   </servlet>
 
+  <servlet-mapping>
+    <servlet-name>ChartOutputService</servlet-name>
+    <url-pattern>/flys/chart</url-pattern>
+  </servlet-mapping>
+
+  <!-- MapFish Print -->
   <servlet>
     <servlet-name>MapOutputService</servlet-name>
     <servlet-class>de.intevation.flys.client.server.MapOutputServiceImpl</servlet-class>
@@ -248,11 +254,22 @@
     <url-pattern>/flys/map</url-pattern>
   </servlet-mapping>
 
+  <!-- Servlet to bridge between MapFish Print and FLYS3 -->
+  <servlet>
+    <servlet-name>MapPrintService</servlet-name>
+    <servlet-class>de.intevation.flys.client.server.MapPrintServiceImpl</servlet-class>
+    <init-param>
+      <param-name>config</param-name>
+      <param-value>WEB-INF/config.yaml</param-value>
+    </init-param>
+  </servlet>
+
   <servlet-mapping>
-    <servlet-name>ChartOutputService</servlet-name>
-    <url-pattern>/flys/chart</url-pattern>
+    <servlet-name>MapPrintService</servlet-name>
+    <url-pattern>/flys/map-print</url-pattern>
   </servlet-mapping>
 
+
   <servlet>
     <servlet-name>FixingsKMChartService</servlet-name>
     <servlet-class>de.intevation.flys.client.server.FixingsKMChartServiceImpl</servlet-class>
@@ -423,20 +440,6 @@
     <url-pattern>/flys/fileupload</url-pattern>
   </servlet-mapping>
 
-  <servlet>
-    <servlet-name>mapfish.print</servlet-name>
-    <servlet-class>org.mapfish.print.servlet.MapPrinterServlet</servlet-class>
-    <init-param>
-      <param-name>config</param-name>
-      <param-value>config.yaml</param-value>
-    </init-param>
-  </servlet>
-
-  <servlet-mapping>
-    <servlet-name>mapfish.print</servlet-name>
-    <url-pattern>/flys/map-print</url-pattern>
-  </servlet-mapping>
-
   <!-- Default page to serve -->
   <welcome-file-list>
     <welcome-file>FLYS.html</welcome-file>

http://dive4elements.wald.intevation.org