annotate flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 5607:6e0d122904f9

Explicitly set the content type for the pdf download. This should make the mimetype detection of browsers more robust
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 09 Apr 2013 11:53:47 +0200
parents 0d8564196d73
children 6602be8ca685
rev   line source
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
3 import de.intevation.artifacts.common.ArtifactNamespaceContext;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
4 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
5 import de.intevation.artifacts.common.utils.JSON;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
6 import de.intevation.artifacts.common.utils.StringUtils;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
7 import de.intevation.artifacts.common.utils.XMLUtils;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
8 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
9 import de.intevation.artifacts.httpclient.http.HttpClient;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
10 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
11 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
4436
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
12 import de.intevation.flys.client.shared.MapUtils;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
13 import de.intevation.flys.client.shared.model.MapConfig;
5606
0d8564196d73 Break printing to rework the parametrization of the print job
Andre Heinecke <aheinecke@intevation.de>
parents: 5600
diff changeset
14 import de.intevation.flys.client.client.FLYSConstants;
4433
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
15
4436
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
16 import java.io.IOException;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
17 import java.io.InputStream;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
18 import java.io.OutputStream;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
19 import java.io.UnsupportedEncodingException;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
20 import java.net.URLEncoder;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
21 import java.util.ArrayList;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
22 import java.util.Collections;
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
23 import java.util.Enumeration;
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
24 import java.util.HashMap;
4436
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
25 import java.util.LinkedHashMap;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
26 import java.util.List;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
27 import java.util.Map;
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
28
4436
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
29 import javax.servlet.ServletException;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
30 import javax.servlet.http.HttpServlet;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
31 import javax.servlet.http.HttpServletRequest;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
32 import javax.servlet.http.HttpServletResponse;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
33
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
34 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
35 import org.apache.commons.httpclient.methods.GetMethod;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
36 import org.apache.log4j.Logger;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
37 import org.w3c.dom.Document;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
38 import org.w3c.dom.Element;
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
39 import org.w3c.dom.NodeList;
4433
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
40 /*
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
41 import java.io.BufferedInputStream;
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
42 import java.io.BufferedOutputStream;
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
43 import java.io.File;
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
44 import java.io.FileInputStream;
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
45 import java.io.FileOutputStream;
5b8919ef601d Backed out changeset e8a4d2fd25cc
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4424
diff changeset
46 */
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
47 /* Used by direct API call. -> Enforce GPLv3
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 import org.mapfish.print.MapPrinter;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 import org.mapfish.print.output.OutputFactory;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 import org.mapfish.print.output.OutputFormat;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 import org.mapfish.print.utils.PJsonObject;
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
53 */
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 public class MapPrintServiceImpl
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 extends HttpServlet
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 private static final Logger log =
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 Logger.getLogger(MapPrintServiceImpl.class);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
61 protected static class Layer implements Comparable<Layer> {
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
62
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
63 protected int pos;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
64 protected String url;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
65 protected String layers;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
66 protected String description;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
67
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
68 public Layer() {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
69 }
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
70
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
71 public boolean setup(Element element) {
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
72
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
73 Element parent = (Element)element.getParentNode();
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
74 String parentName = parent.getAttribute("name");
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
75 if (!(parentName.equals("map")
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
76 || parentName.equals("floodmap"))) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
77 return false;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
78 }
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
79
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
80 String ns = ArtifactNamespaceContext.NAMESPACE_URI;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
81
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
82 String visible = element.getAttributeNS(ns, "visible");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
83 String active = element.getAttributeNS(ns, "active");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
84
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
85 if (visible.equals("0") || active.equals("0")) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
86 return false;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
87 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
88
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
89 url = element.getAttributeNS(ns, "url");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
90 layers = element.getAttributeNS(ns, "layers");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
91 description = element.getAttributeNS(ns, "description");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
92
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
93 try {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
94 pos = Integer.parseInt(element.getAttributeNS(ns, "pos"));
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
95 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
96 catch (NumberFormatException nfe) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
97 return false;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
98 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
99
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
100 return true;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
101 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
102
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
103 public Map<String, Object> toMap() {
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
104 Map<String, Object> layer = new LinkedHashMap<String, Object>();
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
105
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
106 layer.put("type", "WMS");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
107 List<Object> subLayers = new ArrayList<Object>(1);
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
108 subLayers.add(layers);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
109 layer.put("layers", subLayers);
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
110 layer.put("baseURL", url);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
111 layer.put("format", "image/png"); // TODO: Make configurable.
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
112
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
113 return layer;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
114 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
115
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
116 @Override
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
117 public int compareTo(Layer other) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
118 int d = pos - other.pos;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
119 if (d < 0) return -1;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
120 return d > 0 ? +1 : 0;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
121 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
122 } // class Layer
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
123
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
124 protected static String generateSpec(
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
125 Document descDocument,
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
126 MapConfig mapConfig,
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
127 Double minX, Double minY,
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
128 Double maxX, Double maxY,
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
129 Map<String, Object> pageSpecs
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
130 ) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
131 Map<String, Object> spec = new LinkedHashMap<String, Object>();
4969
555fa2774d3c Map printing in landscape mode. Layout still buggy.
Christian Lins <christian.lins@intevation.de>
parents: 4525
diff changeset
132 spec.put("layout", "A4 landscape");
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
133 spec.put("pageSize", "A4");
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
134 spec.put("landscape", "false");
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
135 spec.put("title", "FLYS Druck");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
136 spec.put("srs", "EPSG:" + mapConfig.getSrid());
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
137 spec.put("dpi", Integer.valueOf(254));
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
138 spec.put("units", "m");
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
139 spec.put("geodaetic", "true");
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
140 spec.put("outputFormat", "pdf");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
141
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
142 spec.putAll(pageSpecs);
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
143
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
144 String ns = ArtifactNamespaceContext.NAMESPACE_URI;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
145
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
146 List<Layer> ls = new ArrayList<Layer>();
4472
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
147 Layer l = new Layer();
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
148
4472
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
149 NodeList facets = descDocument.getElementsByTagNameNS(ns, "facet");
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
150
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
151 for (int i = 0, N = facets.getLength(); i < N; ++i) {
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
152 Element element = (Element)facets.item(i);
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
153 if (l.setup(element)) {
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
154 ls.add(l);
dc7e41efd5ba Work (in progress) on a print settings dialog.
Christian Lins <christian.lins@intevation.de>
parents: 4436
diff changeset
155 l = new Layer();
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
156 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
157 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
158
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
159 // Establish Z order.
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
160 Collections.sort(ls);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
161
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
162 List<Object> layers = new ArrayList<Object>(ls.size());
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
163
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
164 for (int i = ls.size()-1; i >= 0; --i) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
165 layers.add(ls.get(i).toMap());
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
166 }
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
167
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
168 spec.put("layers", layers);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
169 spec.put("name", "Name");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
170
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
171 List<Object> pages = new ArrayList<Object>(1);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
172
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
173
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
174 Map<String, Object> page = new LinkedHashMap<String, Object>();
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
175
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
176 List<Object> bounds = new ArrayList<Object>(4);
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
177 bounds.add(minX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
178 bounds.add(minY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
179 bounds.add(maxX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
180 bounds.add(maxY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
181 page.put("bbox", bounds);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
182
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
183 /*
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
184 bounds.add(Double.valueOf((minX+maxX)*0.5));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
185 bounds.add(Double.valueOf((minY+maxY)*0.5));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
186
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
187 page.put("center", bounds);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
188 page.put("scale", Integer.valueOf(50000));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
189 */
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
190
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
191 page.put("mapTitle", "FLYS Karte");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
192 page.put("comment", "Eine mit FLYS gedruckte Karte.");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
193 page.put("rotation", Integer.valueOf(0));
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
194
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
195 // This may overwrite default settings above
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
196 page.putAll(pageSpecs);
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
197
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
198 pages.add(page);
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
199 spec.put("pages", pages);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
200
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
201 List<Object> legends = new ArrayList<Object>(layers.size());
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
202
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
203 for (Layer layer: ls) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
204 Map<String, Object> legend = new LinkedHashMap<String, Object>();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
205 List<Object> classes = new ArrayList<Object>(1);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
206 Map<String, Object> clazz = new LinkedHashMap<String, Object>();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
207 String lgu = MapUtils.getLegendGraphicUrl(layer.url, layer.layers);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
208 clazz.put("icon", lgu);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
209 clazz.put("name", layer.description);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
210 classes.add(clazz);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
211 legend.put("classes", classes);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
212 legend.put("name", layer.description);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
213 legends.add(legend);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
214 }
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
215
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
216 spec.put("legends", legends);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
217
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
218 return JSON.toJSONString(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
219 }
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
222 @Override
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
223 public void doGet(HttpServletRequest req, HttpServletResponse resp)
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
224 throws ServletException, IOException
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
225 {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
226 log.info("MapPrintServiceImpl.doGet");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
227
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
228 String uuid = req.getParameter("uuid");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
229
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
230 if (uuid == null || !StringUtils.checkUUID(uuid)) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
231 throw new ServletException("Missing or misspelled UUID");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
232 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
233
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
234 String minXS = req.getParameter("minx");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
235 String maxXS = req.getParameter("maxx");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
236 String minYS = req.getParameter("miny");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
237 String maxYS = req.getParameter("maxy");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
238
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
239 Double minX = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
240 Double maxX = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
241 Double minY = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
242 Double maxY = null;
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
243
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
244 if (minXS != null && maxXS != null
2515
532c590beb3e Fixed problem with selecting the wrong map scale for printing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2510
diff changeset
245 && minYS != null && maxYS != null) {
532c590beb3e Fixed problem with selecting the wrong map scale for printing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2510
diff changeset
246 log.debug("all parameters found -> parsing");
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
247 try {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
248 minX = Double.parseDouble(minXS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
249 minY = Double.parseDouble(minYS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
250 maxX = Double.parseDouble(maxXS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
251 maxY = Double.parseDouble(maxYS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
252 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
253 catch (NumberFormatException nfe) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
254 throw new ServletException("Misspelled minX, minY, maxX or maxY");
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
255 }
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
256 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
257
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
258 String mapType = req.getParameter("maptype");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
259
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
260 if (mapType == null || !mapType.equals("floodmap")) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
261 mapType = "map";
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
262 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
263
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
264 // Retrieve print settings from request
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
265 Map<String, Object> pageSpecs = new HashMap<String, Object>();
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
266 Enumeration<String> paramNames = req.getParameterNames();
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
267 while (paramNames.hasMoreElements()) {
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
268 String paramName = paramNames.nextElement();
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
269 if (paramName.startsWith("mapfish-")) {
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
270 String paramValue = req.getParameter(paramName);
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
271 pageSpecs.put(paramName.substring(8), paramValue);
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
272 }
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
273 }
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
274
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
275 String url = getURL();
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
276
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
277 Document requestOut =
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
278 ClientProtocolUtils.newOutCollectionDocument(
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
279 uuid, mapType, mapType);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
280 Document requestDesc =
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
281 ClientProtocolUtils.newDescribeCollectionDocument(uuid);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
282
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
283 Document outDocument;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
284 Document descDocument;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
285
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
286 try {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
287 HttpClient client = new HttpClientImpl(url);
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
288
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
289 descDocument = (Document)client.doCollectionAction(
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
290 requestDesc, uuid, new DocumentResponseHandler());
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
291
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
292 InputStream is = client.collectionOut(
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
293 requestOut, uuid, mapType);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
294
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
295 try {
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
296 outDocument = XMLUtils.parseDocument(is);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
297 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
298 finally {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
299 is.close();
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
300 is = null;
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
301 }
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
302
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
303 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
304 catch (ConnectionException ce) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
305 log.error(ce);
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
306 throw new ServletException(ce);
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
307 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
308
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
309 MapConfig mapConfig = MapHelper.parseConfig(outDocument);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
310
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
311 if (minX == null) {
2515
532c590beb3e Fixed problem with selecting the wrong map scale for printing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2510
diff changeset
312 log.debug("parameters missing -> fallback to max extent");
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
313 String [] parts = mapConfig.getMaxExtent().split("\\s+");
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
314 if (parts.length < 4) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
315 throw new ServletException(
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
316 "Max extent has less than 4 values");
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
317 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
318 try {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
319 minX = Double.valueOf(parts[0]);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
320 minY = Double.valueOf(parts[1]);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
321 maxX = Double.valueOf(parts[2]);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
322 maxY = Double.valueOf(parts[3]);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
323 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
324 catch (NumberFormatException nfe) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
325 throw new ServletException(nfe);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
326 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
327 }
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
328 if (log.isDebugEnabled()) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
329 log.debug("minX: " + minX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
330 log.debug("maxX: " + maxX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
331 log.debug("minY: " + minY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
332 log.debug("maxY: " + maxY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
333 }
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
334
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
335 String spec = generateSpec(
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
336 descDocument,
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
337 mapConfig,
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
338 minX, minY,
4473
6db783627137 Map print does now submit additional parameters to MapPrintServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 4472
diff changeset
339 maxX, maxY,
4474
bd78d2b0e309 Map print settings can now be configured via settings window.
Christian Lins <christian.lins@intevation.de>
parents: 4473
diff changeset
340 pageSpecs);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
341
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
342 if (log.isDebugEnabled()) {
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
343 log.debug("Generated spec:");
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
344 log.debug(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
345 //System.err.println(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
346 }
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
347
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
348 producePDF(spec, resp);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
349 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
350
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
351 protected String getURL() throws ServletException {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
352 String url = getServletContext().getInitParameter("server-url");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
353 if (url == null) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
354 throw new ServletException("Missing server-url");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
355 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
356 return url;
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
357 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
358
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
359 private static final String encode(String s) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
360 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
361 return URLEncoder.encode(s, "UTF-8");
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
362 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
363 catch (UnsupportedEncodingException usee) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
364 // Should not happen.
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
365 return s;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
366 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
367 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
368
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
369 protected void producePDF(String json, HttpServletResponse resp)
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
370 throws ServletException, IOException
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
371 {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
372 String printUrl = getInitParameter("print-url");
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
373
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
374 if (printUrl == null) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
375 throw new ServletException("Missing 'print-url' in web.xml");
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
376 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
377
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
378 String url = printUrl + "/print.pdf?spec=" + encode(json);
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
379
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
380 org.apache.commons.httpclient.HttpClient client =
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
381 new org.apache.commons.httpclient.HttpClient(
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
382 new MultiThreadedHttpConnectionManager());
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
383
4436
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
384 // FIXME: The request is not authenticated.
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
385 // Currently this is not a problem because /flys/map-print
9fca4d60fb7c Reintroduce wrongly outbacked changes (rev 4418-4425).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4433
diff changeset
386 // is whitelisted in GGInAFilter.
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
387 GetMethod get = new GetMethod(url);
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
388 int result = client.executeMethod(get);
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
389 InputStream in = get.getResponseBodyAsStream();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
390
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
391 if (in != null) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
392 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
393 OutputStream out = resp.getOutputStream();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
394 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
395 byte [] buf = new byte[4096];
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
396 int r;
5600
8128622e4b28 Set filename for the response
Andre Heinecke <aheinecke@intevation.de>
parents: 4969
diff changeset
397 resp.setHeader("Content-Disposition",
8128622e4b28 Set filename for the response
Andre Heinecke <aheinecke@intevation.de>
parents: 4969
diff changeset
398 "attachment;filename=flys-karte.pdf");
5607
6e0d122904f9 Explicitly set the content type for the pdf download.
Andre Heinecke <aheinecke@intevation.de>
parents: 5606
diff changeset
399 resp.setContentType("application/pdf");
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
400 while ((r = in.read(buf)) >= 0) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
401 out.write(buf, 0, r);
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
402 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
403 out.flush();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
404 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
405 finally {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
406 out.close();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
407 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
408 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
409 finally {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
410 in.close();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
411 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
412 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
413 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
414
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
415 /* Use this if you want directly call the MapPrinter. Enforces GPLv3!
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
416
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
417 protected MapPrinter getMapPrinter() throws ServletException, IOException {
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
418 String configPath = getInitParameter("config");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
419 if (configPath == null) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
420 throw new ServletException("Missing configuration in web.xml");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
421 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
422
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
423 File configFile = new File(configPath);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
424 if (!configFile.isAbsolute()) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
425 configFile = new File(getServletContext().getRealPath(configPath));
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
426 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
427
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
428 if (!configFile.isFile() || !configFile.canRead()) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
429 throw new ServletException("Cannot read '" + configFile + "'");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
430 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
431
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
432 return new MapPrinter(configFile);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
433 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
434
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
435 protected void producePDF(String json, HttpServletResponse resp)
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
436 throws ServletException, IOException
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
437 {
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
438 PJsonObject jsonSpec = MapPrinter.parseSpec(json);
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
439
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
440 MapPrinter printer = getMapPrinter();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
441
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
442 OutputFormat outputFormat = OutputFactory.create(
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
443 printer.getConfig(), jsonSpec);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
444
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
445 resp.setHeader("Content-Disposition", "attachment;filename=print.pdf");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
446 resp.setHeader("Content-Type", "application/pdf");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
447
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
448 // XXX: Streaming the generated PDF directly
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
449 // to the request out does not work. :-/
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
450 File tmpFile = File.createTempFile("map-printing", null);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
451
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
452 try {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
453 OutputStream out =
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
454 new BufferedOutputStream(
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
455 new FileOutputStream(tmpFile));
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
456 try {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
457 outputFormat.print(printer, jsonSpec, out, "");
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
458 out.flush();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
459 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
460 catch (Exception e) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
461 log.error(e);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
462 throw new ServletException(e);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
463 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
464 finally {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
465 printer.stop();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
466 out.close();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
467 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
468 InputStream in =
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
469 new BufferedInputStream(
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
470 new FileInputStream(tmpFile));
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
471 out = resp.getOutputStream();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
472 try {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
473 byte [] buf = new byte[4096];
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
474 int r;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
475 while ((r = in.read(buf)) >= 0) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
476 out.write(buf, 0, r);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
477 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
478 out.flush();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
479 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
480 finally {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
481 in.close();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
482 out.close();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
483 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
484 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
485 finally {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
486 if (tmpFile.exists()) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
487 tmpFile.delete();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
488 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
489 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
490 }
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
491 */
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
492 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
493 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org