diff flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 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 532c590beb3e
children 51ed89b754ae
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org