changeset 2519:427df4e81af0

Call MapFish Printer via servlet to evade viral GPLv3. flys-client/trunk@4398 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 14 May 2012 09:52:04 +0000
parents b3bf9e5ce9e6
children fe67f1345687
files flys-client/ChangeLog flys-client/pom.xml flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java flys-client/src/main/webapp/WEB-INF/web.xml
diffstat 4 files changed, 99 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Sun May 13 18:29:22 2012 +0000
+++ b/flys-client/ChangeLog	Mon May 14 09:52:04 2012 +0000
@@ -1,3 +1,15 @@
+2012-05-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java:
+	  Call MapFish print via Apache Commons Http Client to re-establish
+	  LGPL conformance.
+
+	* src/main/webapp/WEB-INF/web.xml: Configured the MapFish Print servlet
+	  again. You need to adjust the init parameter 'print-url' of the 
+	  servlet MapPrintService to point to the MapFish Print servlet.
+
+	* pom.xml: Added dependency to Apache Commons Http Client 3.1
+
 2012-05-13	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSConstants.java,
--- a/flys-client/pom.xml	Sun May 13 18:29:22 2012 +0000
+++ b/flys-client/pom.xml	Mon May 14 09:52:04 2012 +0000
@@ -85,6 +85,11 @@
       <version>0.6</version>
     </dependency>
     <dependency>
+      <groupId>commons-httpclient</groupId>
+      <artifactId>commons-httpclient</artifactId>
+      <version>3.1</version>
+    </dependency>
+    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.14</version>
--- a/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java	Sun May 13 18:29:22 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java	Mon May 14 09:52:04 2012 +0000
@@ -17,14 +17,19 @@
 import de.intevation.flys.client.shared.model.MapConfig;
 import de.intevation.flys.client.shared.MapUtils;
 
+/*
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+*/
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+import java.net.URLEncoder;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -38,14 +43,19 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+
+import org.apache.commons.httpclient.methods.GetMethod;
+
 import org.apache.log4j.Logger;
 
+/* Used by direct API call. -> Enforce GPLv3
 import org.mapfish.print.MapPrinter;
-
 import org.mapfish.print.output.OutputFactory;
 import org.mapfish.print.output.OutputFormat;
 
 import org.mapfish.print.utils.PJsonObject;
+*/ 
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -336,6 +346,58 @@
         return url;
     }
 
+    private static final String encode(String s) {
+        try {
+            return URLEncoder.encode(s, "UTF-8");
+        }
+        catch (UnsupportedEncodingException usee) {
+            // Should not happen.
+            return s;
+        }
+    }
+
+    protected void producePDF(String json, HttpServletResponse resp)
+    throws ServletException, IOException
+    {
+        String printUrl = getInitParameter("print-url");
+
+        if (printUrl == null) {
+            throw new ServletException("Missing 'print-url' in web.xml");
+        }
+
+        String url = printUrl + "/print.pdf?spec=" + encode(json);
+
+        org.apache.commons.httpclient.HttpClient client =
+            new org.apache.commons.httpclient.HttpClient(
+                new MultiThreadedHttpConnectionManager());
+
+        GetMethod get = new GetMethod(url);
+        int result = client.executeMethod(get);
+        InputStream in = get.getResponseBodyAsStream();
+
+        if (in != null) {
+            try {
+                OutputStream 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 {
+                    out.close();
+                }
+            }
+            finally {
+                in.close();
+            }
+        }
+    }
+
+    /* Use this if you want directly call the MapPrinter. Enforces GPLv3!
+
     protected MapPrinter getMapPrinter() throws ServletException, IOException {
         String configPath = getInitParameter("config");
         if (configPath == null) {
@@ -354,7 +416,6 @@
         return new MapPrinter(configFile);
     }
 
-
     protected void producePDF(String json, HttpServletResponse resp)
     throws ServletException, IOException
     {
@@ -411,5 +472,6 @@
             }
         }
     }
+    */
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/webapp/WEB-INF/web.xml	Sun May 13 18:29:22 2012 +0000
+++ b/flys-client/src/main/webapp/WEB-INF/web.xml	Mon May 14 09:52:04 2012 +0000
@@ -254,6 +254,20 @@
     <url-pattern>/flys/map</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>WEB-INF/config.yaml</param-value>
+    </init-param>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>mapfish.print</servlet-name>
+    <url-pattern>/flys/mapfish-print/*</url-pattern>
+  </servlet-mapping>
+
   <!-- Servlet to bridge between MapFish Print and FLYS3 -->
   <servlet>
     <servlet-name>MapPrintService</servlet-name>
@@ -262,6 +276,10 @@
       <param-name>config</param-name>
       <param-value>WEB-INF/config.yaml</param-value>
     </init-param>
+    <init-param>
+      <param-name>print-url</param-name>
+      <param-value>http://localhost:8888/flys/mapfish-print</param-value>
+    </init-param>
   </servlet>
 
   <servlet-mapping>

http://dive4elements.wald.intevation.org