Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java @ 1417:42d6cf6e10b7
Moved code to parse WMS Capabilities to an own class 'CapabilitiesParser' and added code to parse SRS definitions.
flys-client/trunk@3325 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 28 Nov 2011 15:55:26 +0000 |
parents | 16f19f12a962 |
children |
comparison
equal
deleted
inserted
replaced
1416:16f19f12a962 | 1417:42d6cf6e10b7 |
---|---|
1 package de.intevation.flys.client.server; | 1 package de.intevation.flys.client.server; |
2 | |
3 import java.io.InputStream; | |
4 import java.io.IOException; | |
5 import java.net.MalformedURLException; | |
6 import java.net.URL; | |
7 import java.net.URLConnection; | |
8 import java.util.ArrayList; | |
9 import java.util.List; | |
10 | |
11 import javax.xml.xpath.XPathConstants; | |
12 | 2 |
13 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | 3 import com.google.gwt.user.server.rpc.RemoteServiceServlet; |
14 | 4 |
15 import org.w3c.dom.Document; | |
16 import org.w3c.dom.Node; | |
17 import org.w3c.dom.NodeList; | |
18 | |
19 import org.apache.log4j.Logger; | 5 import org.apache.log4j.Logger; |
20 | |
21 import de.intevation.artifacts.common.utils.XMLUtils; | |
22 | 6 |
23 import de.intevation.flys.client.shared.exceptions.ServerException; | 7 import de.intevation.flys.client.shared.exceptions.ServerException; |
24 import de.intevation.flys.client.shared.model.Capabilities; | 8 import de.intevation.flys.client.shared.model.Capabilities; |
25 import de.intevation.flys.client.shared.model.ContactInformation; | |
26 import de.intevation.flys.client.shared.model.WMSLayer; | |
27 import de.intevation.flys.client.client.services.GCService; | 9 import de.intevation.flys.client.client.services.GCService; |
28 | 10 |
29 | 11 |
30 /** | 12 /** |
31 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 13 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
32 */ | 14 */ |
33 public class GCServiceImpl | 15 public class GCServiceImpl |
34 extends RemoteServiceServlet | 16 extends RemoteServiceServlet |
35 implements GCService | 17 implements GCService |
36 { | 18 { |
37 public static final String ERR_GC_REQUEST_FAILED = | 19 private static Logger logger = Logger.getLogger(GCServiceImpl.class); |
38 "error_gc_req_failed"; | |
39 | |
40 public static final String ERR_GC_DOC_NOT_VALID = | |
41 "error_gc_doc_not_valid"; | |
42 | |
43 public static final String ERR_MALFORMED_URL = | |
44 "error_malformed_url"; | |
45 | |
46 public static final String XPATH_WMS_CAPS = | |
47 "/WMS_Capabilities"; | |
48 | |
49 public static final String XPATH_WMT_CAPS = | |
50 "/WMT_MS_Capabilities"; | |
51 | |
52 public static final String XPATH_TITLE = | |
53 "Service/Title/text()"; | |
54 | |
55 public static final String XPATH_ONLINE_RESOURCE = | |
56 "Service/OnlineResource/@href"; | |
57 | |
58 public static final String XPATH_CONTACT_INFORMATION = | |
59 "Service/ContactInformation"; | |
60 | |
61 public static final String XPATH_CI_PERSON = | |
62 "ContactPersonPrimary/ContactPerson/text()"; | |
63 | |
64 public static final String XPATH_CI_ORGANIZATION = | |
65 "ContactPersonPrimary/ContactOrganization/text()"; | |
66 | |
67 public static final String XPATH_CI_ADDRESS = | |
68 "ContactAddress/Address/text()"; | |
69 | |
70 public static final String XPATH_CI_CITY = | |
71 "ContactAddress/City/text()"; | |
72 | |
73 public static final String XPATH_CI_POSTCODE = | |
74 "ContactAddress/PostCode/text()"; | |
75 | |
76 public static final String XPATH_CI_PHONE = | |
77 "ContactVoiceTelephone/text()"; | |
78 | |
79 public static final String XPATH_CI_EMAIL = | |
80 "ContactElectronicMailAddress/text()"; | |
81 | |
82 public static final String XPATH_FEES = | |
83 "Service/Fees/text()"; | |
84 | |
85 public static final String XPATH_ACCESS_CONSTRAINTS = | |
86 "Service/AccessConstraints/text()"; | |
87 | |
88 public static final String XPATH_LAYERS = | |
89 "Capability/Layer"; | |
90 | |
91 | |
92 private Logger logger = Logger.getLogger(GCServiceImpl.class); | |
93 | 20 |
94 | 21 |
95 public Capabilities query(String path) | 22 public Capabilities query(String path) |
96 throws ServerException | 23 throws ServerException |
97 { | 24 { |
98 logger.info("GCServiceImpl.query"); | 25 logger.info("GCServiceImpl.query"); |
99 | 26 |
100 try { | 27 return CapabilitiesParser.getCapabilities(path); |
101 URL url = new URL(path); | |
102 | |
103 logger.debug("Open connection to url: " + path); | |
104 | |
105 URLConnection conn = url.openConnection(); | |
106 conn.connect(); | |
107 | |
108 InputStream is = conn.getInputStream(); | |
109 | |
110 return parseCapabilitiesResponse(is); | |
111 } | |
112 catch (MalformedURLException mue) { | |
113 logger.warn(mue, mue); | |
114 throw new ServerException(ERR_MALFORMED_URL); | |
115 } | |
116 catch (IOException ioe) { | |
117 logger.warn(ioe, ioe); | |
118 } | |
119 | |
120 throw new ServerException(ERR_GC_REQUEST_FAILED); | |
121 } | |
122 | |
123 | |
124 protected Capabilities parseCapabilitiesResponse(InputStream is) | |
125 throws ServerException | |
126 { | |
127 logger.debug("GCServiceImpl.parseCapabilitiesResponse"); | |
128 | |
129 Document doc = XMLUtils.parseDocument(is, false); | |
130 | |
131 if (doc == null) { | |
132 throw new ServerException(ERR_GC_DOC_NOT_VALID); | |
133 } | |
134 | |
135 Node capabilities = getCapabilitiesNode(doc); | |
136 | |
137 String title = (String) XMLUtils.xpath( | |
138 capabilities, | |
139 XPATH_TITLE, | |
140 XPathConstants.STRING); | |
141 | |
142 String onlineResource = (String) XMLUtils.xpath( | |
143 capabilities, | |
144 XPATH_ONLINE_RESOURCE, | |
145 XPathConstants.STRING); | |
146 | |
147 String fees = (String) XMLUtils.xpath( | |
148 capabilities, | |
149 XPATH_FEES, | |
150 XPathConstants.STRING); | |
151 | |
152 String accessConstraints = (String) XMLUtils.xpath( | |
153 capabilities, | |
154 XPATH_ACCESS_CONSTRAINTS, | |
155 XPathConstants.STRING); | |
156 | |
157 Node contactInformation = (Node) XMLUtils.xpath( | |
158 capabilities, | |
159 XPATH_CONTACT_INFORMATION, | |
160 XPathConstants.NODE); | |
161 | |
162 ContactInformation ci = parseContactInformation(contactInformation); | |
163 | |
164 logger.debug("Found fees: " + fees); | |
165 logger.debug("Found access constraints: " + accessConstraints); | |
166 | |
167 NodeList layerNodes = (NodeList) XMLUtils.xpath( | |
168 capabilities, | |
169 XPATH_LAYERS, | |
170 XPathConstants.NODESET); | |
171 | |
172 List<WMSLayer> layers = parseLayers(layerNodes, onlineResource); | |
173 | |
174 return new Capabilities( | |
175 title, | |
176 onlineResource, | |
177 ci, | |
178 fees, | |
179 accessConstraints, | |
180 layers); | |
181 } | |
182 | |
183 | |
184 protected Node getCapabilitiesNode(Document doc) | |
185 throws ServerException { | |
186 Node capabilities = (Node) XMLUtils.xpath( | |
187 doc, | |
188 XPATH_WMS_CAPS, | |
189 XPathConstants.NODE); | |
190 | |
191 if (capabilities == null) { | |
192 logger.info("No '/WMS_Capabilities' node found."); | |
193 logger.info("Try to find a '/WMT_MS_Capabilities' node."); | |
194 | |
195 capabilities = (Node) XMLUtils.xpath( | |
196 doc, | |
197 XPATH_WMT_CAPS, | |
198 XPathConstants.NODE); | |
199 } | |
200 | |
201 if (capabilities == null) { | |
202 throw new ServerException(ERR_GC_DOC_NOT_VALID); | |
203 } | |
204 | |
205 return capabilities; | |
206 } | |
207 | |
208 | |
209 protected ContactInformation parseContactInformation(Node node) { | |
210 String person = (String) XMLUtils.xpath( | |
211 node, | |
212 XPATH_CI_PERSON, | |
213 XPathConstants.STRING); | |
214 | |
215 String organization = (String) XMLUtils.xpath( | |
216 node, | |
217 XPATH_CI_ORGANIZATION, | |
218 XPathConstants.STRING); | |
219 | |
220 String address = (String) XMLUtils.xpath( | |
221 node, | |
222 XPATH_CI_ADDRESS, | |
223 XPathConstants.STRING); | |
224 | |
225 String postcode = (String) XMLUtils.xpath( | |
226 node, | |
227 XPATH_CI_POSTCODE, | |
228 XPathConstants.STRING); | |
229 | |
230 String city = (String) XMLUtils.xpath( | |
231 node, | |
232 XPATH_CI_CITY, | |
233 XPathConstants.STRING); | |
234 | |
235 String phone = (String) XMLUtils.xpath( | |
236 node, | |
237 XPATH_CI_PHONE, | |
238 XPathConstants.STRING); | |
239 | |
240 String email = (String) XMLUtils.xpath( | |
241 node, | |
242 XPATH_CI_EMAIL, | |
243 XPathConstants.STRING); | |
244 | |
245 ContactInformation ci = new ContactInformation(); | |
246 ci.setPerson(person); | |
247 ci.setOrganization(organization); | |
248 ci.setAddress(address); | |
249 ci.setPostcode(postcode); | |
250 ci.setCity(city); | |
251 ci.setPhone(phone); | |
252 ci.setEmail(email); | |
253 | |
254 return ci; | |
255 } | |
256 | |
257 | |
258 /** | |
259 * @param layersNode | |
260 * @param onlineResource | |
261 * | |
262 * @return | |
263 */ | |
264 protected List<WMSLayer> parseLayers( | |
265 NodeList layersNode, | |
266 String onlineResource | |
267 ) { | |
268 int len = layersNode != null ? layersNode.getLength() : 0; | |
269 | |
270 logger.debug("Node has " + len + " layers."); | |
271 | |
272 List<WMSLayer> layers = new ArrayList<WMSLayer>(len); | |
273 | |
274 for (int i = 0; i < len; i++) { | |
275 layers.add(parseLayer(layersNode.item(i), onlineResource)); | |
276 } | |
277 | |
278 return layers; | |
279 } | |
280 | |
281 | |
282 protected WMSLayer parseLayer(Node layerNode, String onlineResource) { | |
283 String title = (String) XMLUtils.xpath( | |
284 layerNode, | |
285 "Title/text()", | |
286 XPathConstants.STRING); | |
287 | |
288 String name = (String) XMLUtils.xpath( | |
289 layerNode, | |
290 "Name/text()", | |
291 XPathConstants.STRING); | |
292 | |
293 logger.debug("Found layers: " + title + "(" + name + ")"); | |
294 | |
295 NodeList layersNodes = (NodeList) XMLUtils.xpath( | |
296 layerNode, | |
297 "Layer", | |
298 XPathConstants.NODESET); | |
299 | |
300 List<WMSLayer> layers = parseLayers(layersNodes, onlineResource); | |
301 | |
302 return new WMSLayer(onlineResource, title, name, layers); | |
303 } | 28 } |
304 } | 29 } |
305 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 30 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |