Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
2518:b3bf9e5ce9e6 | 2519:427df4e81af0 |
---|---|
15 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; | 15 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; |
16 | 16 |
17 import de.intevation.flys.client.shared.model.MapConfig; | 17 import de.intevation.flys.client.shared.model.MapConfig; |
18 import de.intevation.flys.client.shared.MapUtils; | 18 import de.intevation.flys.client.shared.MapUtils; |
19 | 19 |
20 /* | |
20 import java.io.BufferedInputStream; | 21 import java.io.BufferedInputStream; |
21 import java.io.BufferedOutputStream; | 22 import java.io.BufferedOutputStream; |
22 import java.io.File; | 23 import java.io.File; |
23 import java.io.FileInputStream; | 24 import java.io.FileInputStream; |
24 import java.io.FileOutputStream; | 25 import java.io.FileOutputStream; |
26 */ | |
25 import java.io.IOException; | 27 import java.io.IOException; |
26 import java.io.InputStream; | 28 import java.io.InputStream; |
27 import java.io.OutputStream; | 29 import java.io.OutputStream; |
30 import java.io.UnsupportedEncodingException; | |
31 | |
32 import java.net.URLEncoder; | |
28 | 33 |
29 import java.util.ArrayList; | 34 import java.util.ArrayList; |
30 import java.util.Collections; | 35 import java.util.Collections; |
31 import java.util.LinkedHashMap; | 36 import java.util.LinkedHashMap; |
32 import java.util.List; | 37 import java.util.List; |
36 | 41 |
37 import javax.servlet.http.HttpServlet; | 42 import javax.servlet.http.HttpServlet; |
38 import javax.servlet.http.HttpServletRequest; | 43 import javax.servlet.http.HttpServletRequest; |
39 import javax.servlet.http.HttpServletResponse; | 44 import javax.servlet.http.HttpServletResponse; |
40 | 45 |
46 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; | |
47 | |
48 import org.apache.commons.httpclient.methods.GetMethod; | |
49 | |
41 import org.apache.log4j.Logger; | 50 import org.apache.log4j.Logger; |
42 | 51 |
52 /* Used by direct API call. -> Enforce GPLv3 | |
43 import org.mapfish.print.MapPrinter; | 53 import org.mapfish.print.MapPrinter; |
44 | |
45 import org.mapfish.print.output.OutputFactory; | 54 import org.mapfish.print.output.OutputFactory; |
46 import org.mapfish.print.output.OutputFormat; | 55 import org.mapfish.print.output.OutputFormat; |
47 | 56 |
48 import org.mapfish.print.utils.PJsonObject; | 57 import org.mapfish.print.utils.PJsonObject; |
58 */ | |
49 | 59 |
50 import org.w3c.dom.Document; | 60 import org.w3c.dom.Document; |
51 import org.w3c.dom.Element; | 61 import org.w3c.dom.Element; |
52 import org.w3c.dom.NodeList; | 62 import org.w3c.dom.NodeList; |
53 | 63 |
334 throw new ServletException("Missing server-url"); | 344 throw new ServletException("Missing server-url"); |
335 } | 345 } |
336 return url; | 346 return url; |
337 } | 347 } |
338 | 348 |
349 private static final String encode(String s) { | |
350 try { | |
351 return URLEncoder.encode(s, "UTF-8"); | |
352 } | |
353 catch (UnsupportedEncodingException usee) { | |
354 // Should not happen. | |
355 return s; | |
356 } | |
357 } | |
358 | |
359 protected void producePDF(String json, HttpServletResponse resp) | |
360 throws ServletException, IOException | |
361 { | |
362 String printUrl = getInitParameter("print-url"); | |
363 | |
364 if (printUrl == null) { | |
365 throw new ServletException("Missing 'print-url' in web.xml"); | |
366 } | |
367 | |
368 String url = printUrl + "/print.pdf?spec=" + encode(json); | |
369 | |
370 org.apache.commons.httpclient.HttpClient client = | |
371 new org.apache.commons.httpclient.HttpClient( | |
372 new MultiThreadedHttpConnectionManager()); | |
373 | |
374 GetMethod get = new GetMethod(url); | |
375 int result = client.executeMethod(get); | |
376 InputStream in = get.getResponseBodyAsStream(); | |
377 | |
378 if (in != null) { | |
379 try { | |
380 OutputStream out = resp.getOutputStream(); | |
381 try { | |
382 byte [] buf = new byte[4096]; | |
383 int r; | |
384 while ((r = in.read(buf)) >= 0) { | |
385 out.write(buf, 0, r); | |
386 } | |
387 out.flush(); | |
388 } | |
389 finally { | |
390 out.close(); | |
391 } | |
392 } | |
393 finally { | |
394 in.close(); | |
395 } | |
396 } | |
397 } | |
398 | |
399 /* Use this if you want directly call the MapPrinter. Enforces GPLv3! | |
400 | |
339 protected MapPrinter getMapPrinter() throws ServletException, IOException { | 401 protected MapPrinter getMapPrinter() throws ServletException, IOException { |
340 String configPath = getInitParameter("config"); | 402 String configPath = getInitParameter("config"); |
341 if (configPath == null) { | 403 if (configPath == null) { |
342 throw new ServletException("Missing configuration in web.xml"); | 404 throw new ServletException("Missing configuration in web.xml"); |
343 } | 405 } |
351 throw new ServletException("Cannot read '" + configFile + "'"); | 413 throw new ServletException("Cannot read '" + configFile + "'"); |
352 } | 414 } |
353 | 415 |
354 return new MapPrinter(configFile); | 416 return new MapPrinter(configFile); |
355 } | 417 } |
356 | |
357 | 418 |
358 protected void producePDF(String json, HttpServletResponse resp) | 419 protected void producePDF(String json, HttpServletResponse resp) |
359 throws ServletException, IOException | 420 throws ServletException, IOException |
360 { | 421 { |
361 PJsonObject jsonSpec = MapPrinter.parseSpec(json); | 422 PJsonObject jsonSpec = MapPrinter.parseSpec(json); |
409 if (tmpFile.exists()) { | 470 if (tmpFile.exists()) { |
410 tmpFile.delete(); | 471 tmpFile.delete(); |
411 } | 472 } |
412 } | 473 } |
413 } | 474 } |
475 */ | |
414 } | 476 } |
415 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 477 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |