annotate flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 3530:8c9c202292fe

Set the default value of the RadioGroupItem if a loaded module is selected flys-client/trunk@5333 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 31 Aug 2012 13:29:26 +0000
parents 51ed89b754ae
children 6ef48927df38
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
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.ClientProtocolUtils;
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.JSON;
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.StringUtils;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
8 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
9
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.exceptions.ConnectionException;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
11
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
12 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
13 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
14
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
15 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
16
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
17 import de.intevation.flys.client.shared.model.MapConfig;
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
18 import de.intevation.flys.client.shared.MapUtils;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
19
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
20 /*
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
21 import java.io.BufferedInputStream;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
22 import java.io.BufferedOutputStream;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
23 import java.io.File;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
24 import java.io.FileInputStream;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
25 import java.io.FileOutputStream;
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
26 */
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
27 import java.io.IOException;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
28 import java.io.InputStream;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
29 import java.io.OutputStream;
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
30 import java.io.UnsupportedEncodingException;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
31
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
32 import java.net.URLEncoder;
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
33
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
34 import java.util.ArrayList;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
35 import java.util.Collections;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
36 import java.util.LinkedHashMap;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
37 import java.util.List;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
38 import java.util.Map;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
39
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
40 import javax.servlet.ServletException;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
41
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
42 import javax.servlet.http.HttpServlet;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
43 import javax.servlet.http.HttpServletRequest;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
44 import javax.servlet.http.HttpServletResponse;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
45
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
46 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
47
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
48 import org.apache.commons.httpclient.methods.GetMethod;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
49
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 import org.apache.log4j.Logger;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
52 /* 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
53 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
54 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
55 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
56
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 import org.mapfish.print.utils.PJsonObject;
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
58 */
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
60 import org.w3c.dom.Document;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
61 import org.w3c.dom.Element;
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
62 import org.w3c.dom.NodeList;
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
63
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 public class MapPrintServiceImpl
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 extends HttpServlet
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 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
68 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
69
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
70 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
71
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
72 protected int pos;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
73 protected String url;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
74 protected String layers;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
75 protected String description;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
76
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
77 public Layer() {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
78 }
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
79
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
80 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
81
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
82 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
83 String parentName = parent.getAttribute("name");
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
84 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
85 || parentName.equals("floodmap"))) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
86 return false;
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
87 }
2506
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 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
90
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
91 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
92 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
93
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
94 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
95 return false;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
96 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
97
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
98 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
99 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
100 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
101
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
102 try {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
103 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
104 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
105 catch (NumberFormatException nfe) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
106 return false;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
107 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
108
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
109 return true;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
110 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
111
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
112 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
113 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
114
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
115 layer.put("type", "WMS");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
116 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
117 subLayers.add(layers);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
118 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
119 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
120 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
121
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
122 return 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
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
125 @Override
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
126 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
127 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
128 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
129 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
130 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
131 } // class Layer
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
132
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
133 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
134 Document descDocument,
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
135 MapConfig mapConfig,
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
136 Double minX, Double minY,
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
137 Double maxX, Double maxY
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
138 ) {
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
139 Map<String, Object> spec = new LinkedHashMap<String, Object>();
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
140 spec.put("layout", "A4 portrait");
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
141 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
142 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
143 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
144 spec.put("units", "m");
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
145 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
146 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
147
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
148 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
149
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
150 List<Layer> ls = new ArrayList<Layer>();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
151 { Layer l = new Layer();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
152
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
153 NodeList facets = descDocument.getElementsByTagNameNS(ns, "facet");
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
154
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
155 for (int i = 0, N = facets.getLength(); i < N; ++i) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
156 Element element = (Element)facets.item(i);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
157 if (l.setup(element)) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
158 ls.add(l);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
159 l = new Layer();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
160 }
2506
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 }
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
163
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
164 // Establish Z order.
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
165 Collections.sort(ls);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
166
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
167 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
168
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
169 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
170 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
171 }
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 spec.put("layers", layers);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
174 spec.put("name", "Name");
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> pages = new ArrayList<Object>(1);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
177
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
178
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
179 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
180
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
181 List<Object> bounds = new ArrayList<Object>(4);
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
182 bounds.add(minX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
183 bounds.add(minY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
184 bounds.add(maxX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
185 bounds.add(maxY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
186 page.put("bbox", bounds);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
187
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
188 /*
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
189 bounds.add(Double.valueOf((minX+maxX)*0.5));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
190 bounds.add(Double.valueOf((minY+maxY)*0.5));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
191
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
192 page.put("center", bounds);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
193 page.put("scale", Integer.valueOf(50000));
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
194 */
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
195
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
196 page.put("mapTitle", "FLYS Karte");
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
197 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
198 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
199
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
200 pages.add(page);
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
201 spec.put("pages", pages);
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 List<Object> legends = new ArrayList<Object>(layers.size());
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
204
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
205 for (Layer layer: ls) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
206 Map<String, Object> legend = new LinkedHashMap<String, Object>();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
207 List<Object> classes = new ArrayList<Object>(1);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
208 Map<String, Object> clazz = new LinkedHashMap<String, Object>();
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
209 String lgu = MapUtils.getLegendGraphicUrl(layer.url, layer.layers);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
210 clazz.put("icon", lgu);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
211 clazz.put("name", layer.description);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
212 classes.add(clazz);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
213 legend.put("classes", classes);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
214 legend.put("name", layer.description);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
215 legends.add(legend);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
216 }
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
217
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
218 spec.put("legends", legends);
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
219
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
220 return JSON.toJSONString(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
221 }
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
224 @Override
2905
51ed89b754ae FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2519
diff changeset
225 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
226 throws ServletException, IOException
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 log.info("MapPrintServiceImpl.doGet");
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 String uuid = req.getParameter("uuid");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
231
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
232 if (uuid == null || !StringUtils.checkUUID(uuid)) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
233 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
234 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
235
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
236 String minXS = req.getParameter("minx");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
237 String maxXS = req.getParameter("maxx");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
238 String minYS = req.getParameter("miny");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
239 String maxYS = req.getParameter("maxy");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
240
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
241 Double minX = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
242 Double maxX = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
243 Double minY = null;
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
244 Double maxY = null;
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
245
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
246 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
247 && 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
248 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
249 try {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
250 minX = Double.parseDouble(minXS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
251 minY = Double.parseDouble(minYS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
252 maxX = Double.parseDouble(maxXS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
253 maxY = Double.parseDouble(maxYS);
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
254 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
255 catch (NumberFormatException nfe) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
256 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
257 }
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
258 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
259
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
260 String mapType = req.getParameter("maptype");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
261
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
262 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
263 mapType = "map";
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
264 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
265
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
266 String url = getURL();
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
267
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
268 Document requestOut =
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
269 ClientProtocolUtils.newOutCollectionDocument(
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
270 uuid, mapType, mapType);
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
271 Document requestDesc =
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
272 ClientProtocolUtils.newDescribeCollectionDocument(uuid);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
273
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
274 Document outDocument;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
275 Document descDocument;
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
276
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
277 try {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
278 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
279
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
280 descDocument = (Document)client.doCollectionAction(
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
281 requestDesc, uuid, new DocumentResponseHandler());
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
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 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
284 requestOut, uuid, mapType);
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 {
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
287 outDocument = XMLUtils.parseDocument(is);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
288 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
289 finally {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
290 is.close();
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
291 is = null;
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
292 }
2506
47d07709ba09 Printing: extract layer order and visibility from collections describe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2504
diff changeset
293
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
294 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
295 catch (ConnectionException ce) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
296 log.error(ce);
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
297 throw new ServletException(ce);
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
298 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
299
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
300 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
301
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
302 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
303 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
304 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
305 if (parts.length < 4) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
306 throw new ServletException(
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
307 "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
308 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
309 try {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
310 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
311 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
312 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
313 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
314 }
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
315 catch (NumberFormatException nfe) {
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
316 throw new ServletException(nfe);
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 }
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
319 if (log.isDebugEnabled()) {
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
320 log.debug("minX: " + minX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
321 log.debug("maxX: " + maxX);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
322 log.debug("minY: " + minY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
323 log.debug("maxY: " + maxY);
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
324 }
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
325
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
326 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
327 descDocument,
2508
4aa70825bde1 map print: generate valid url to access service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2506
diff changeset
328 mapConfig,
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
329 minX, minY,
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
330 maxX, maxY);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
331
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
332 if (log.isDebugEnabled()) {
2510
4861ab2c28d2 map print: add legend
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2508
diff changeset
333 log.debug("Generated spec:");
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
334 log.debug(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
335 //System.err.println(spec);
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
336 }
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
337
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
338 producePDF(spec, resp);
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
339 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
340
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
341 protected String getURL() throws ServletException {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
342 String url = getServletContext().getInitParameter("server-url");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
343 if (url == null) {
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
344 throw new ServletException("Missing server-url");
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
345 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
346 return url;
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
347 }
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
348
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
349 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
350 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
351 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
352 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
353 catch (UnsupportedEncodingException usee) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
354 // Should not happen.
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
355 return s;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
356 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
357 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
358
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
359 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
360 throws ServletException, IOException
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
361 {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
362 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
363
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
364 if (printUrl == null) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
365 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
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 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
369
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
370 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
371 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
372 new MultiThreadedHttpConnectionManager());
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 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
375 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
376 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
377
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
378 if (in != null) {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
379 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
380 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
381 try {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
382 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
383 int r;
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
384 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
385 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
386 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
387 out.flush();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
388 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
389 finally {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
390 out.close();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
391 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
392 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
393 finally {
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
394 in.close();
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
395 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
396 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
397 }
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
398
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
399 /* 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
400
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
401 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
402 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
403 if (configPath == null) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
404 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
405 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
406
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
407 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
408 if (!configFile.isAbsolute()) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
409 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
410 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
411
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
412 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
413 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
414 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
415
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
416 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
417 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
418
2503
e3bd1f412421 Added GET parameters to map printer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2502
diff changeset
419 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
420 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
421 {
2504
425bc486a40f Generate map print spec dynamically from artifact
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2503
diff changeset
422 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
423
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
424 MapPrinter printer = getMapPrinter();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
425
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
426 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
427 printer.getConfig(), jsonSpec);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
428
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
429 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
430 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
431
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
432 // 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
433 // 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
434 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
435
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
436 try {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
437 OutputStream out =
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
438 new BufferedOutputStream(
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
439 new FileOutputStream(tmpFile));
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
440 try {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
441 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
442 out.flush();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
443 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
444 catch (Exception e) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
445 log.error(e);
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
446 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
447 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
448 finally {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
449 printer.stop();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
450 out.close();
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 InputStream in =
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
453 new BufferedInputStream(
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
454 new FileInputStream(tmpFile));
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
455 out = resp.getOutputStream();
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 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
458 int r;
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
459 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
460 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
461 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
462 out.flush();
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 in.close();
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 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
469 finally {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
470 if (tmpFile.exists()) {
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
471 tmpFile.delete();
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
472 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
473 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
474 }
2519
427df4e81af0 Call MapFish Printer via servlet to evade viral GPLv3.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
475 */
2502
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
476 }
328aa273ef3b Call MapPrinter directly and not via the MapFish Print servlet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
477 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org