root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSProtocolHandler.java @ 38579
History | View | Annotate | Download (20.7 KB)
1 | 29658 | jpiera | package org.gvsig.remoteclient.wms; |
---|---|---|---|
2 | 3323 | ldiaz | |
3 | 3798 | ldiaz | import java.io.ByteArrayInputStream; |
4 | 3323 | ldiaz | import java.io.File; |
5 | 8559 | ldiaz | import java.io.FileReader; |
6 | 3377 | ldiaz | import java.io.IOException; |
7 | 3323 | ldiaz | import java.io.InputStream; |
8 | import java.net.URL; |
||
9 | 3776 | jaume | import java.net.URLConnection; |
10 | 3798 | ldiaz | import java.util.ArrayList; |
11 | 17849 | jmvivo | import java.util.StringTokenizer; |
12 | 3323 | ldiaz | import java.util.TreeMap; |
13 | 3776 | jaume | |
14 | 33738 | jpiera | import org.gvsig.compat.net.ICancellable; |
15 | 29658 | jpiera | import org.gvsig.remoteclient.exceptions.ServerErrorException; |
16 | import org.gvsig.remoteclient.exceptions.WMSException; |
||
17 | import org.gvsig.remoteclient.ogc.OGCProtocolHandler; |
||
18 | import org.gvsig.remoteclient.ogc.OGCServiceInformation; |
||
19 | import org.gvsig.remoteclient.utils.CapabilitiesTags; |
||
20 | import org.gvsig.remoteclient.utils.ExceptionTags; |
||
21 | import org.gvsig.remoteclient.utils.Utilities; |
||
22 | 30324 | jpiera | import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest; |
23 | 29658 | jpiera | import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest; |
24 | import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest; |
||
25 | import org.gvsig.remoteclient.wms.request.WMSGetMapRequest; |
||
26 | 34305 | nbrodin | import org.kxml2.io.KXmlParser; |
27 | import org.xmlpull.v1.XmlPullParserException; |
||
28 | 3323 | ldiaz | |
29 | /**
|
||
30 | * <p> Abstract class that represents handlers to comunicate via WMS protocol.
|
||
31 | * </p>
|
||
32 | 11381 | jaume | *
|
33 | 3323 | ldiaz | */
|
34 | 38530 | nbrodin | public abstract class WMSProtocolHandler extends OGCProtocolHandler { |
35 | 3483 | jaume | /**
|
36 | 4500 | jaume | * Encoding used to parse different xml documents.
|
37 | */
|
||
38 | protected String encoding = "UTF-8"; |
||
39 | 3483 | jaume | /**
|
40 | * WMS metadata
|
||
41 | */
|
||
42 | 27881 | jpiera | protected WMSServiceInformation serviceInfo;
|
43 | 3323 | ldiaz | public TreeMap layers; |
44 | 3483 | jaume | public WMSLayer rootLayer;
|
45 | 11381 | jaume | |
46 | 3323 | ldiaz | /**
|
47 | 5239 | ldiaz | * returns the alfanumeric information of the layers at the specified point.
|
48 | * the diference between the other getfeatureInfo method is that this will
|
||
49 | * be implemented by each specific version because the XML from the server will be
|
||
50 | * parsed and presented by a well known structure.
|
||
51 | 11381 | jaume | */
|
52 | 3323 | ldiaz | |
53 | 11381 | jaume | public String getName() { |
54 | 3323 | ldiaz | return name;
|
55 | 11381 | jaume | } |
56 | 3323 | ldiaz | |
57 | 27881 | jpiera | /*
|
58 | * (non-Javadoc)
|
||
59 | * @see org.gvsig.remoteClient.ogc.OGCProtocolHandler#getServiceInformation()
|
||
60 | */
|
||
61 | public OGCServiceInformation getServiceInformation() {
|
||
62 | 3323 | ldiaz | return serviceInfo;
|
63 | 11381 | jaume | } |
64 | 17849 | jmvivo | |
65 | 3323 | ldiaz | /**
|
66 | 3853 | ldiaz | * <p>Builds a GetCapabilities request that is sent to the WMS
|
67 | 5239 | ldiaz | * the response will be parse to extract the data needed by the
|
68 | 3853 | ldiaz | * WMS client</p>
|
69 | 4355 | jaume | * @param override, if true the previous downloaded data will be overridden
|
70 | 3853 | ldiaz | */
|
71 | 38530 | nbrodin | public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) { |
72 | URL request = null; |
||
73 | try {
|
||
74 | request = new URL(buildCapabilitiesRequest(status)); |
||
75 | } catch(Exception e) { |
||
76 | e.printStackTrace(); |
||
77 | } |
||
78 | try {
|
||
79 | if (override)
|
||
80 | Utilities.removeURL(request);
|
||
81 | File f = Utilities.downloadFile(request,"wms_capabilities.xml", cancel); |
||
82 | //WMSGetCapabilitiesRequest request = createGetCapabilitiesRequest(status);
|
||
83 | //File f = request.sendRequest();
|
||
84 | 5409 | jaume | if (f == null) |
85 | return;
|
||
86 | 5343 | jaume | clear(); |
87 | 27881 | jpiera | parseCapabilities(f); |
88 | 38530 | nbrodin | } catch(Exception e) { |
89 | 3853 | ldiaz | e.printStackTrace(); |
90 | } |
||
91 | } |
||
92 | |||
93 | 5343 | jaume | private void clear() { |
94 | layers.clear(); |
||
95 | serviceInfo.clear(); |
||
96 | } |
||
97 | |||
98 | /**
|
||
99 | 3853 | ldiaz | * <p>It will send a GetFeatureInfo request to the WMS
|
100 | * Parsing the response and redirecting the info to the WMS client</p>
|
||
101 | 8559 | ldiaz | * TODO: return a stored file instead a String.
|
102 | 3853 | ldiaz | */
|
103 | 8559 | ldiaz | public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) |
104 | 3853 | ldiaz | { |
105 | StringBuffer output = new StringBuffer(); |
||
106 | String outputFormat = new String(); |
||
107 | 11381 | jaume | String ServiceException = "ServiceExceptionReport"; |
108 | 3853 | ldiaz | StringBuffer sb = new StringBuffer(); |
109 | sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
|
||
110 | try
|
||
111 | { |
||
112 | 27881 | jpiera | WMSGetFeatureInfoRequest request = createGetFeatureInfoRequest(status, x, y); |
113 | URL url = request.getURL();
|
||
114 | outputFormat = url.openConnection().getContentType(); |
||
115 | 33983 | nbrodin | File f = request.sendRequest(cancel);
|
116 | 27881 | jpiera | if (f == null){ |
117 | 8559 | ldiaz | return ""; |
118 | 27881 | jpiera | } |
119 | 11381 | jaume | |
120 | 8559 | ldiaz | FileReader fReader = new FileReader(f); |
121 | char[] buffer = new char[1024*256]; |
||
122 | for (int i = fReader.read(buffer); i>0; i = fReader.read(buffer)) |
||
123 | { |
||
124 | String str = new String(buffer,0,i); |
||
125 | 11381 | jaume | output.append(str); |
126 | 8559 | ldiaz | } |
127 | 3853 | ldiaz | if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1) |
128 | ||output.toString().toLowerCase().startsWith("<?xml")
|
||
129 | ||(outputFormat.indexOf("gml") != -1)) |
||
130 | { |
||
131 | int tag;
|
||
132 | KXmlParser kxmlParser = null;
|
||
133 | 11381 | jaume | kxmlParser = new KXmlParser();
|
134 | //kxmlParser.setInput(new StringReader(output.toString()));
|
||
135 | 8559 | ldiaz | kxmlParser.setInput(new FileReader(f)); |
136 | 11381 | jaume | |
137 | tag = kxmlParser.nextTag(); |
||
138 | 4222 | jaume | if (kxmlParser.getName().compareTo(ServiceException)==0) |
139 | { |
||
140 | sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>"); |
||
141 | 11381 | jaume | return sb.toString();
|
142 | 4222 | jaume | } |
143 | else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0) |
||
144 | { |
||
145 | return output.toString();
|
||
146 | } |
||
147 | 11381 | jaume | else
|
148 | 4222 | jaume | { |
149 | 11381 | jaume | return output.toString();
|
150 | 4222 | jaume | } |
151 | 3853 | ldiaz | } |
152 | else
|
||
153 | 27881 | jpiera | { |
154 | 5428 | ldiaz | //Para que funcione con el GetFeatureInfo Viewer generico hay que devolver:
|
155 | 5838 | ldiaz | return output.toString();
|
156 | 3853 | ldiaz | } |
157 | } |
||
158 | catch(XmlPullParserException parserEx)
|
||
159 | { |
||
160 | if (output.toString().toLowerCase().indexOf("xml") != -1) |
||
161 | { |
||
162 | return output.toString().trim();
|
||
163 | } |
||
164 | else
|
||
165 | { |
||
166 | 5536 | jaume | sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
167 | 3853 | ldiaz | return sb.toString();
|
168 | } |
||
169 | } |
||
170 | catch(Exception e) |
||
171 | { |
||
172 | e.printStackTrace(); |
||
173 | 5536 | jaume | sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
174 | 3853 | ldiaz | return sb.toString();
|
175 | |||
176 | } |
||
177 | } |
||
178 | /**
|
||
179 | 3323 | ldiaz | * <p>Builds a GetMap request that is sent to the WMS
|
180 | * the response (image) will be redirect to the
|
||
181 | * WMS client</p>
|
||
182 | 11381 | jaume | */
|
183 | 4222 | jaume | public byte[] _getMap(WMSStatus status) throws ServerErrorException, WMSException |
184 | 11381 | jaume | { |
185 | 27881 | jpiera | try
|
186 | 3351 | ldiaz | { |
187 | //TODO:
|
||
188 | //pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
|
||
189 | 27881 | jpiera | WMSGetMapRequest request = createGetMapRequest(status); |
190 | URL url = request.getURL();
|
||
191 | |||
192 | URLConnection conn = url.openConnection();
|
||
193 | 3776 | jaume | System.out.println(request.toString());
|
194 | String type = conn.getContentType();
|
||
195 | 11381 | jaume | |
196 | |||
197 | 3516 | jaume | byte[] imageBytes = null; |
198 | 3377 | ldiaz | byte[] buffer = new byte[1024*256]; |
199 | 3776 | jaume | InputStream is = conn.getInputStream();
|
200 | 3377 | ldiaz | int readed = 0; |
201 | 11381 | jaume | |
202 | 3377 | ldiaz | for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
203 | 3483 | jaume | // Creates a new buffer to contain the previous readed bytes and the next bunch of bytes
|
204 | 3377 | ldiaz | byte[] buffered = new byte[readed+i]; |
205 | for (int j = 0; j < buffered.length; j++) { |
||
206 | if (j<readed){
|
||
207 | 3483 | jaume | // puts the previously downloaded bytes into the image buffer
|
208 | 3377 | ldiaz | buffered[j] = imageBytes[j]; |
209 | } |
||
210 | else {
|
||
211 | 3483 | jaume | // appends the recently downloaded bytes to the image buffer.
|
212 | 3377 | ldiaz | buffered[j] = buffer[j-readed]; |
213 | } |
||
214 | } |
||
215 | imageBytes = (byte[]) buffered.clone(); |
||
216 | 11381 | jaume | readed += i; |
217 | 3377 | ldiaz | } |
218 | 11381 | jaume | |
219 | if ((type !=null && !type.subSequence(0,5).equals("image")) |
||
220 | 3824 | ldiaz | ||(Utilities.isTextData(imageBytes)))
|
221 | 11381 | jaume | { |
222 | 3853 | ldiaz | WMSException wmsEx = null;
|
223 | 11381 | jaume | |
224 | 3776 | jaume | String exceptionMessage = parseException(imageBytes);
|
225 | 3853 | ldiaz | if (exceptionMessage==null) |
226 | 3824 | ldiaz | { |
227 | 3853 | ldiaz | String error = new String(imageBytes); |
228 | 3824 | ldiaz | int pos = error.indexOf("<?xml"); |
229 | if (pos!= -1) |
||
230 | { |
||
231 | String xml = error.substring(pos,error.length());
|
||
232 | exceptionMessage = parseException(xml.getBytes()); |
||
233 | 3853 | ldiaz | if (exceptionMessage == null) |
234 | exceptionMessage = new String(imageBytes); |
||
235 | 3824 | ldiaz | } |
236 | } |
||
237 | 4222 | jaume | wmsEx = new WMSException(exceptionMessage);
|
238 | 3853 | ldiaz | wmsEx.setWMSMessage(new String(imageBytes)); |
239 | throw wmsEx;
|
||
240 | 3776 | jaume | } |
241 | 11381 | jaume | return imageBytes;
|
242 | 3351 | ldiaz | } |
243 | 3516 | jaume | catch(IOException e) |
244 | 3351 | ldiaz | { |
245 | 3405 | jaume | e.printStackTrace(); |
246 | 3516 | jaume | throw new ServerErrorException(); |
247 | 3351 | ldiaz | } |
248 | 11381 | jaume | } |
249 | |||
250 | 7945 | ldiaz | public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException |
251 | 7835 | ldiaz | { |
252 | 27881 | jpiera | try
|
253 | 7835 | ldiaz | { |
254 | 27881 | jpiera | WMSGetLegendGraphicRequest request = createGetLegendGraphicRequest(status, layerName); |
255 | 33983 | nbrodin | File f = request.sendRequest(cancel);
|
256 | 7835 | ldiaz | if (f== null) |
257 | return null; |
||
258 | if (Utilities.isTextFile(f)) { |
||
259 | 33738 | jpiera | |
260 | byte[] data = fileToBytes(f); |
||
261 | 11381 | jaume | |
262 | 7835 | ldiaz | WMSException wmsEx = null;
|
263 | 11381 | jaume | |
264 | 7835 | ldiaz | String exceptionMessage = parseException(data);
|
265 | if (exceptionMessage==null) |
||
266 | { |
||
267 | String error = new String(data); |
||
268 | int pos = error.indexOf("<?xml"); |
||
269 | if (pos!= -1) |
||
270 | { |
||
271 | String xml = error.substring(pos,error.length());
|
||
272 | exceptionMessage = parseException(xml.getBytes()); |
||
273 | 11381 | jaume | } |
274 | 7835 | ldiaz | if (exceptionMessage == null) |
275 | exceptionMessage = new String(data); |
||
276 | 11381 | jaume | |
277 | 7835 | ldiaz | } |
278 | wmsEx = new WMSException(exceptionMessage);
|
||
279 | wmsEx.setWMSMessage(new String(data)); |
||
280 | 33738 | jpiera | downloader.removeURL(request); |
281 | 7835 | ldiaz | throw wmsEx;
|
282 | } |
||
283 | 11381 | jaume | return f;
|
284 | 7835 | ldiaz | } |
285 | catch(IOException e) |
||
286 | { |
||
287 | e.printStackTrace(); |
||
288 | throw new ServerErrorException(); |
||
289 | } |
||
290 | 11381 | jaume | } |
291 | 35758 | nbrodin | |
292 | 35928 | nbrodin | public URL getMapURL(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
293 | 35758 | nbrodin | try {
|
294 | WMSGetMapRequest request = createGetMapRequest(status); |
||
295 | return request.getURL();
|
||
296 | } catch(IOException e) { |
||
297 | e.printStackTrace(); |
||
298 | throw new ServerErrorException(); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /**
|
||
303 | * Returns the exception message if the file is a XML instead of a image.
|
||
304 | * @param file3
|
||
305 | * @return
|
||
306 | * @throws IOException
|
||
307 | */
|
||
308 | public String getExceptionMessage(File f) throws IOException { |
||
309 | if (f == null) |
||
310 | return null; |
||
311 | |||
312 | if (Utilities.isTextFile(f)) { |
||
313 | byte[] data = fileToBytes(f); |
||
314 | 11381 | jaume | |
315 | 35758 | nbrodin | String exceptionMessage = parseException(data);
|
316 | if (exceptionMessage == null) { |
||
317 | String error = new String(data); |
||
318 | int pos = error.indexOf("<?xml"); |
||
319 | if (pos!= -1) { |
||
320 | String xml = error.substring(pos,error.length());
|
||
321 | exceptionMessage = parseException(xml.getBytes()); |
||
322 | } |
||
323 | if (exceptionMessage == null) |
||
324 | exceptionMessage = new String(data); |
||
325 | } |
||
326 | return exceptionMessage;
|
||
327 | } |
||
328 | return null; |
||
329 | } |
||
330 | |||
331 | public File getMap(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
||
332 | try {
|
||
333 | 27881 | jpiera | WMSGetMapRequest request = createGetMapRequest(status); |
334 | 33983 | nbrodin | File f = request.sendRequest(cancel);
|
335 | 27881 | jpiera | |
336 | if (f== null) |
||
337 | 5720 | ldiaz | return null; |
338 | 4222 | jaume | if (Utilities.isTextFile(f)) { |
339 | 33738 | jpiera | byte[] data = fileToBytes(f); |
340 | 11381 | jaume | |
341 | 4222 | jaume | WMSException wmsEx = null;
|
342 | 11381 | jaume | |
343 | 4222 | jaume | String exceptionMessage = parseException(data);
|
344 | 35758 | nbrodin | if (exceptionMessage==null) { |
345 | 4222 | jaume | String error = new String(data); |
346 | int pos = error.indexOf("<?xml"); |
||
347 | 35758 | nbrodin | if (pos!= -1) { |
348 | 4222 | jaume | String xml = error.substring(pos,error.length());
|
349 | exceptionMessage = parseException(xml.getBytes()); |
||
350 | // if (exceptionMessage == null)
|
||
351 | // exceptionMessage = new String(data);
|
||
352 | 11381 | jaume | } |
353 | 4222 | jaume | if (exceptionMessage == null) |
354 | exceptionMessage = new String(data); |
||
355 | 11381 | jaume | |
356 | 4222 | jaume | } |
357 | wmsEx = new WMSException(exceptionMessage);
|
||
358 | wmsEx.setWMSMessage(new String(data)); |
||
359 | 11381 | jaume | |
360 | 4222 | jaume | // Since it is an error file, It must be deleted from the cache
|
361 | 33738 | jpiera | downloader.removeURL(request); |
362 | 4222 | jaume | throw wmsEx;
|
363 | } |
||
364 | 11381 | jaume | return f;
|
365 | 35758 | nbrodin | } catch(IOException e) { |
366 | 4222 | jaume | e.printStackTrace(); |
367 | throw new ServerErrorException(); |
||
368 | } |
||
369 | 11381 | jaume | } |
370 | |||
371 | |||
372 | 3798 | ldiaz | /* (non-Javadoc)
|
373 | * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
|
||
374 | 3516 | jaume | */
|
375 | 3798 | ldiaz | protected String parseException(byte[] data) { |
376 | ArrayList errors = new ArrayList(); |
||
377 | KXmlParser kxmlParser = new KXmlParser();
|
||
378 | try
|
||
379 | { |
||
380 | 11381 | jaume | kxmlParser.setInput(new ByteArrayInputStream(data), encoding); |
381 | 3798 | ldiaz | kxmlParser.nextTag(); |
382 | int tag;
|
||
383 | 11381 | jaume | if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
384 | { |
||
385 | kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
|
||
386 | 3798 | ldiaz | tag = kxmlParser.nextTag(); |
387 | while(tag != KXmlParser.END_DOCUMENT)
|
||
388 | { |
||
389 | switch(tag)
|
||
390 | { |
||
391 | case KXmlParser.START_TAG:
|
||
392 | if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){ |
||
393 | String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE); |
||
394 | errorCode = (errorCode != null) ? "["+errorCode+"] " : ""; |
||
395 | String errorMessage = kxmlParser.nextText();
|
||
396 | errors.add(errorCode+errorMessage); |
||
397 | } |
||
398 | break;
|
||
399 | 11381 | jaume | case KXmlParser.END_TAG:
|
400 | 3798 | ldiaz | break;
|
401 | 11381 | jaume | |
402 | 3798 | ldiaz | } |
403 | tag = kxmlParser.nextTag(); |
||
404 | } |
||
405 | //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
|
||
406 | } |
||
407 | } |
||
408 | 11381 | jaume | catch(XmlPullParserException parser_ex){
|
409 | 3798 | ldiaz | parser_ex.printStackTrace(); |
410 | } |
||
411 | 11381 | jaume | catch (IOException ioe) { |
412 | ioe.printStackTrace(); |
||
413 | 3798 | ldiaz | } |
414 | String message = errors.size()>0? "" : null; |
||
415 | for (int i = 0; i < errors.size(); i++) { |
||
416 | message += (String) errors.get(i)+"\n"; |
||
417 | } |
||
418 | return message;
|
||
419 | } |
||
420 | 3345 | ldiaz | /**
|
421 | * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
||
422 | 3687 | ldiaz | * without a VERSION, to get the highest version than a WMS supports.
|
423 | 3345 | ldiaz | */
|
424 | 3687 | ldiaz | public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version) |
425 | { |
||
426 | 17849 | jmvivo | int index = _host.indexOf('?'); |
427 | |||
428 | if (index > -1) { |
||
429 | String host = _host.substring(0, index + 1); |
||
430 | String query = _host.substring(index + 1, _host.length()); |
||
431 | |||
432 | StringTokenizer tokens = new StringTokenizer(query, "&"); |
||
433 | String newQuery = "", token; |
||
434 | |||
435 | // If there is a field or a value with spaces, (and then it's on different tokens) -> unify them
|
||
436 | while (tokens.hasMoreTokens()) {
|
||
437 | token = tokens.nextToken().trim(); |
||
438 | |||
439 | if (token.toUpperCase().compareTo("REQUEST=GETCAPABILITIES") == 0) |
||
440 | continue;
|
||
441 | |||
442 | if (token.toUpperCase().compareTo("SERVICE=WMS") == 0) |
||
443 | continue;
|
||
444 | |||
445 | if ((_version != null) && (_version.length() > 0)) { |
||
446 | if (token.toUpperCase().compareTo("VERSION=" + _version) == 0) |
||
447 | continue;
|
||
448 | } |
||
449 | |||
450 | if (token.toUpperCase().compareTo("EXCEPTIONS=XML") == 0) |
||
451 | continue;
|
||
452 | |||
453 | newQuery += token + "&";
|
||
454 | } |
||
455 | |||
456 | _host = host + newQuery; |
||
457 | } |
||
458 | else {
|
||
459 | _host += "?";
|
||
460 | } |
||
461 | |||
462 | if ((_version != null) && (_version.compareTo("") != 0)) |
||
463 | 30324 | jpiera | _host += "REQUEST=GetCapabilities&SERVICE=WMS&VERSION=" + _version;
|
464 | 17849 | jmvivo | else
|
465 | 30324 | jpiera | _host += "REQUEST=GetCapabilities&SERVICE=WMS";
|
466 | 17849 | jmvivo | |
467 | return _host;
|
||
468 | 3687 | ldiaz | } |
469 | 11381 | jaume | |
470 | 3687 | ldiaz | /**
|
471 | * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
||
472 | 4222 | jaume | * @param WMSStatus
|
473 | 3687 | ldiaz | */
|
474 | 5239 | ldiaz | private String buildCapabilitiesRequest(WMSStatus status) |
475 | 3345 | ldiaz | { |
476 | 4222 | jaume | StringBuffer req = new StringBuffer(); |
477 | String symbol = null; |
||
478 | 11381 | jaume | |
479 | 4222 | jaume | String onlineResource;
|
480 | if (status == null || status.getOnlineResource() == null) |
||
481 | onlineResource = getHost(); |
||
482 | 11381 | jaume | else
|
483 | 4222 | jaume | onlineResource = status.getOnlineResource(); |
484 | symbol = getSymbol(onlineResource); |
||
485 | 11381 | jaume | |
486 | 4222 | jaume | req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WMS&");
|
487 | 30324 | jpiera | req.append("VERSION=").append(getVersion());
|
488 | 4222 | jaume | return req.toString();
|
489 | 27881 | jpiera | } |
490 | |||
491 | public void close() { |
||
492 | // your code here
|
||
493 | } |
||
494 | |||
495 | /**
|
||
496 | * @param status
|
||
497 | * The WMS status
|
||
498 | * @param protocolHandler
|
||
499 | * The handler to parse the requests
|
||
500 | * @return an object to send the GetMap requests
|
||
501 | */
|
||
502 | protected abstract WMSGetMapRequest createGetMapRequest(WMSStatus status); |
||
503 | |||
504 | protected abstract WMSGetFeatureInfoRequest createGetFeatureInfoRequest(WMSStatus status, int x, int y); |
||
505 | |||
506 | protected abstract WMSGetLegendGraphicRequest createGetLegendGraphicRequest(WMSStatus status, String layerName); |
||
507 | 11381 | jaume | |
508 | 30324 | jpiera | protected abstract WMSGetCapabilitiesRequest createGetCapabilitiesRequest(WMSStatus status); |
509 | 27881 | jpiera | |
510 | /**
|
||
511 | * <p>Parses the Request tag </p>
|
||
512 | */
|
||
513 | protected void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
514 | { |
||
515 | int currentTag;
|
||
516 | boolean end = false; |
||
517 | |||
518 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
|
||
519 | currentTag = parser.next(); |
||
520 | |||
521 | while (!end)
|
||
522 | { |
||
523 | switch(currentTag)
|
||
524 | { |
||
525 | case KXmlParser.START_TAG:
|
||
526 | if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0) |
||
527 | { |
||
528 | parserDcpType(parser, CapabilitiesTags.GETCAPABILITIES); |
||
529 | } |
||
530 | else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0) |
||
531 | { |
||
532 | parseGetMapTag(parser); |
||
533 | } |
||
534 | else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0) |
||
535 | { |
||
536 | parseGetFeatureInfoTag(parser); |
||
537 | } |
||
538 | else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0) |
||
539 | { |
||
540 | parserDcpType(parser, CapabilitiesTags.DESCRIBELAYER); |
||
541 | } |
||
542 | else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0) |
||
543 | { |
||
544 | parseGetLegendGraphicTag(parser); |
||
545 | } |
||
546 | break;
|
||
547 | case KXmlParser.END_TAG:
|
||
548 | if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0) |
||
549 | end = true;
|
||
550 | break;
|
||
551 | case KXmlParser.TEXT:
|
||
552 | break;
|
||
553 | } |
||
554 | if(!end)
|
||
555 | currentTag = parser.next(); |
||
556 | } |
||
557 | // TODO: does not get such a tag when arrives here!!!!!!
|
||
558 | //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
|
||
559 | } |
||
560 | /**
|
||
561 | * <p>Parses the GetMap tag </p>
|
||
562 | */
|
||
563 | protected void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
564 | { |
||
565 | int currentTag;
|
||
566 | boolean end = false; |
||
567 | |||
568 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
|
||
569 | currentTag = parser.next(); |
||
570 | |||
571 | while (!end)
|
||
572 | { |
||
573 | switch(currentTag)
|
||
574 | { |
||
575 | case KXmlParser.START_TAG:
|
||
576 | if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
||
577 | { |
||
578 | serviceInfo.formats.add(parser.nextText()); |
||
579 | } |
||
580 | else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
||
581 | { |
||
582 | parserDcpType(parser, CapabilitiesTags.GETMAP); |
||
583 | } |
||
584 | break;
|
||
585 | case KXmlParser.END_TAG:
|
||
586 | if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0) |
||
587 | end = true;
|
||
588 | break;
|
||
589 | case KXmlParser.TEXT:
|
||
590 | break;
|
||
591 | } |
||
592 | if(!end)
|
||
593 | currentTag = parser.next(); |
||
594 | } |
||
595 | } |
||
596 | |||
597 | 3345 | ldiaz | /**
|
598 | 27881 | jpiera | * <p>Parses the GetFeatureInfo tag </p>
|
599 | */
|
||
600 | protected void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
601 | { |
||
602 | int currentTag;
|
||
603 | boolean end = false; |
||
604 | |||
605 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
|
||
606 | currentTag = parser.next(); |
||
607 | |||
608 | while (!end)
|
||
609 | { |
||
610 | switch(currentTag)
|
||
611 | { |
||
612 | case KXmlParser.START_TAG:
|
||
613 | if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
||
614 | { |
||
615 | 38579 | nbrodin | serviceInfo.infoformats.add(parser.nextText()); |
616 | 27881 | jpiera | } |
617 | else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
||
618 | { |
||
619 | parserDcpType(parser, CapabilitiesTags.GETFEATUREINFO); |
||
620 | } |
||
621 | break;
|
||
622 | case KXmlParser.END_TAG:
|
||
623 | if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0) |
||
624 | end = true;
|
||
625 | break;
|
||
626 | case KXmlParser.TEXT:
|
||
627 | break;
|
||
628 | } |
||
629 | if(!end)
|
||
630 | currentTag = parser.next(); |
||
631 | } |
||
632 | } |
||
633 | |||
634 | 3351 | ldiaz | /**
|
635 | 27881 | jpiera | * <p>Parses the GetLegendGraphic tag </p>
|
636 | */
|
||
637 | protected void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
638 | { |
||
639 | int currentTag;
|
||
640 | boolean end = false; |
||
641 | |||
642 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
|
||
643 | currentTag = parser.next(); |
||
644 | |||
645 | while (!end)
|
||
646 | { |
||
647 | switch(currentTag)
|
||
648 | { |
||
649 | case KXmlParser.START_TAG:
|
||
650 | if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
||
651 | { |
||
652 | //TODO:
|
||
653 | // add the supported formats by the GetLegendGraphic request
|
||
654 | //serviceInfo.formats.add(parser.nextText());
|
||
655 | } |
||
656 | else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
||
657 | { |
||
658 | parserDcpType(parser, CapabilitiesTags.GETLEGENDGRAPHIC); |
||
659 | } |
||
660 | break;
|
||
661 | case KXmlParser.END_TAG:
|
||
662 | if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0) |
||
663 | end = true;
|
||
664 | break;
|
||
665 | case KXmlParser.TEXT:
|
||
666 | break;
|
||
667 | } |
||
668 | if(!end)
|
||
669 | currentTag = parser.next(); |
||
670 | } |
||
671 | 7835 | ldiaz | } |
672 | 3323 | ldiaz | } |