svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wms / WMSProtocolHandler.java @ 41285
History | View | Annotate | Download (25.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.remoteclient.wms; |
25 |
|
26 |
import java.io.ByteArrayInputStream; |
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.FileReader; |
30 |
import java.io.IOException; |
31 |
import java.io.InputStream; |
32 |
import java.net.URL; |
33 |
import java.net.URLConnection; |
34 |
import java.nio.ByteBuffer; |
35 |
import java.nio.channels.FileChannel; |
36 |
import java.util.ArrayList; |
37 |
import java.util.Iterator; |
38 |
import java.util.StringTokenizer; |
39 |
import java.util.TreeMap; |
40 |
|
41 |
import org.gvsig.compat.net.ICancellable; |
42 |
import org.gvsig.remoteclient.exceptions.ServerErrorException; |
43 |
import org.gvsig.remoteclient.exceptions.WMSException; |
44 |
import org.gvsig.remoteclient.ogc.OGCProtocolHandler; |
45 |
import org.gvsig.remoteclient.ogc.OGCServiceInformation; |
46 |
import org.gvsig.remoteclient.utils.CapabilitiesTags; |
47 |
import org.gvsig.remoteclient.utils.ExceptionTags; |
48 |
import org.gvsig.remoteclient.utils.Utilities; |
49 |
import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest; |
50 |
import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest; |
51 |
import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest; |
52 |
import org.gvsig.remoteclient.wms.request.WMSGetMapRequest; |
53 |
import org.kxml2.io.KXmlParser; |
54 |
import org.slf4j.Logger; |
55 |
import org.slf4j.LoggerFactory; |
56 |
import org.xmlpull.v1.XmlPullParserException; |
57 |
|
58 |
/**
|
59 |
* <p> Abstract class that represents handlers to comunicate via WMS protocol.
|
60 |
* </p>
|
61 |
*
|
62 |
*/
|
63 |
public abstract class WMSProtocolHandler extends OGCProtocolHandler { |
64 |
|
65 |
private static final Logger logger = LoggerFactory.getLogger(WMSProtocolHandler.class); |
66 |
/**
|
67 |
* Encoding used to parse different xml documents.
|
68 |
*/
|
69 |
protected String encoding = "UTF-8"; |
70 |
/**
|
71 |
* WMS metadata
|
72 |
*/
|
73 |
protected WMSServiceInformation serviceInfo;
|
74 |
public TreeMap layers; |
75 |
public WMSLayer rootLayer;
|
76 |
|
77 |
/**
|
78 |
* returns the alfanumeric information of the layers at the specified point.
|
79 |
* the diference between the other getfeatureInfo method is that this will
|
80 |
* be implemented by each specific version because the XML from the server will be
|
81 |
* parsed and presented by a well known structure.
|
82 |
*/
|
83 |
|
84 |
public String getName() { |
85 |
return name;
|
86 |
} |
87 |
|
88 |
/*
|
89 |
* (non-Javadoc)
|
90 |
* @see org.gvsig.remoteClient.ogc.OGCProtocolHandler#getServiceInformation()
|
91 |
*/
|
92 |
public OGCServiceInformation getServiceInformation() {
|
93 |
return serviceInfo;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* <p>Builds a GetCapabilities request that is sent to the WMS
|
98 |
* the response will be parse to extract the data needed by the
|
99 |
* WMS client</p>
|
100 |
* @param override, if true the previous downloaded data will be overridden
|
101 |
*/
|
102 |
public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) { |
103 |
URL request = null; |
104 |
try {
|
105 |
request = new URL(buildCapabilitiesRequest(status)); |
106 |
} catch(Exception e) { |
107 |
logger.warn("Can't get capabilities, error building url.",e);
|
108 |
} |
109 |
try {
|
110 |
if (override)
|
111 |
Utilities.removeURL(request);
|
112 |
File f = Utilities.downloadFile(request,"wms_capabilities.xml", cancel); |
113 |
//WMSGetCapabilitiesRequest request = createGetCapabilitiesRequest(status);
|
114 |
//File f = request.sendRequest();
|
115 |
if (f == null) |
116 |
return;
|
117 |
clear(); |
118 |
parseCapabilities(f); |
119 |
} catch(Exception e) { |
120 |
logger.warn("Can't get capabilities.",e);
|
121 |
} |
122 |
} |
123 |
|
124 |
private void clear() { |
125 |
layers.clear(); |
126 |
serviceInfo.clear(); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* <p>It will send a GetFeatureInfo request to the WMS
|
131 |
* Parsing the response and redirecting the info to the WMS client</p>
|
132 |
* TODO: return a stored file instead a String.
|
133 |
*/
|
134 |
public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) |
135 |
{ |
136 |
StringBuffer output = new StringBuffer(); |
137 |
String outputFormat = new String(); |
138 |
String ServiceException = "ServiceExceptionReport"; |
139 |
StringBuffer sb = new StringBuffer(); |
140 |
sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
|
141 |
try
|
142 |
{ |
143 |
WMSGetFeatureInfoRequest request = createGetFeatureInfoRequest(status, x, y); |
144 |
URL url = request.getURL();
|
145 |
outputFormat = url.openConnection().getContentType(); |
146 |
File f = request.sendRequest(cancel);
|
147 |
if (f == null){ |
148 |
return ""; |
149 |
} |
150 |
|
151 |
FileReader fReader = new FileReader(f); |
152 |
char[] buffer = new char[1024*256]; |
153 |
for (int i = fReader.read(buffer); i>0; i = fReader.read(buffer)) |
154 |
{ |
155 |
String str = new String(buffer,0,i); |
156 |
output.append(str); |
157 |
} |
158 |
if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1) |
159 |
||output.toString().toLowerCase().startsWith("<?xml")
|
160 |
||(outputFormat.indexOf("gml") != -1)) |
161 |
{ |
162 |
int tag;
|
163 |
KXmlParser kxmlParser = null;
|
164 |
kxmlParser = new KXmlParser();
|
165 |
//kxmlParser.setInput(new StringReader(output.toString()));
|
166 |
kxmlParser.setInput(new FileReader(f)); |
167 |
|
168 |
tag = kxmlParser.nextTag(); |
169 |
if (kxmlParser.getName().compareTo(ServiceException)==0) |
170 |
{ |
171 |
sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>"); |
172 |
return sb.toString();
|
173 |
} |
174 |
else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0) |
175 |
{ |
176 |
return output.toString();
|
177 |
} |
178 |
else
|
179 |
{ |
180 |
return output.toString();
|
181 |
} |
182 |
} |
183 |
else
|
184 |
{ |
185 |
//Para que funcione con el GetFeatureInfo Viewer generico hay que devolver:
|
186 |
return output.toString();
|
187 |
} |
188 |
} |
189 |
catch(XmlPullParserException parserEx)
|
190 |
{ |
191 |
if (output.toString().toLowerCase().indexOf("xml") != -1) |
192 |
{ |
193 |
return output.toString().trim();
|
194 |
} |
195 |
else
|
196 |
{ |
197 |
sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
198 |
return sb.toString();
|
199 |
} |
200 |
} |
201 |
catch(Exception e) |
202 |
{ |
203 |
logger.warn("Can't get information by point.",e);
|
204 |
sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
205 |
return sb.toString();
|
206 |
|
207 |
} |
208 |
} |
209 |
/**
|
210 |
* <p>Builds a GetMap request that is sent to the WMS
|
211 |
* the response (image) will be redirect to the
|
212 |
* WMS client</p>
|
213 |
*/
|
214 |
public byte[] _getMap(WMSStatus status) throws ServerErrorException, WMSException |
215 |
{ |
216 |
try
|
217 |
{ |
218 |
//TODO:
|
219 |
//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
|
220 |
WMSGetMapRequest request = createGetMapRequest(status); |
221 |
URL url = request.getURL();
|
222 |
|
223 |
URLConnection conn = url.openConnection();
|
224 |
logger.info(request.toString()); |
225 |
String type = conn.getContentType();
|
226 |
|
227 |
|
228 |
byte[] imageBytes = null; |
229 |
byte[] buffer = new byte[1024*256]; |
230 |
InputStream is = conn.getInputStream();
|
231 |
int readed = 0; |
232 |
|
233 |
for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
234 |
// Creates a new buffer to contain the previous readed bytes and the next bunch of bytes
|
235 |
byte[] buffered = new byte[readed+i]; |
236 |
for (int j = 0; j < buffered.length; j++) { |
237 |
if (j<readed){
|
238 |
// puts the previously downloaded bytes into the image buffer
|
239 |
buffered[j] = imageBytes[j]; |
240 |
} |
241 |
else {
|
242 |
// appends the recently downloaded bytes to the image buffer.
|
243 |
buffered[j] = buffer[j-readed]; |
244 |
} |
245 |
} |
246 |
imageBytes = (byte[]) buffered.clone(); |
247 |
readed += i; |
248 |
} |
249 |
|
250 |
if ((type !=null && !type.subSequence(0,5).equals("image")) |
251 |
||(Utilities.isTextData(imageBytes)))
|
252 |
{ |
253 |
WMSException wmsEx = null;
|
254 |
|
255 |
String exceptionMessage = parseException(imageBytes);
|
256 |
if (exceptionMessage==null) |
257 |
{ |
258 |
String error = new String(imageBytes); |
259 |
int pos = error.indexOf("<?xml"); |
260 |
if (pos!= -1) |
261 |
{ |
262 |
String xml = error.substring(pos,error.length());
|
263 |
exceptionMessage = parseException(xml.getBytes()); |
264 |
if (exceptionMessage == null) |
265 |
exceptionMessage = new String(imageBytes); |
266 |
} |
267 |
} |
268 |
wmsEx = new WMSException(exceptionMessage);
|
269 |
wmsEx.setWMSMessage(new String(imageBytes)); |
270 |
throw wmsEx;
|
271 |
} |
272 |
return imageBytes;
|
273 |
} |
274 |
catch(IOException e) |
275 |
{ |
276 |
logger.warn("Can't build map request.",e);
|
277 |
throw new ServerErrorException(); |
278 |
} |
279 |
} |
280 |
|
281 |
public File getLegendGraphic_old(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException |
282 |
{ |
283 |
try
|
284 |
{ |
285 |
WMSGetLegendGraphicRequest request = createGetLegendGraphicRequest(status, layerName); |
286 |
File f = request.sendRequest(cancel);
|
287 |
if (f== null) |
288 |
return null; |
289 |
if (Utilities.isTextFile(f)) { |
290 |
|
291 |
byte[] data = fileToBytes(f); |
292 |
|
293 |
WMSException wmsEx = null;
|
294 |
|
295 |
String exceptionMessage = parseException(data);
|
296 |
if (exceptionMessage==null) |
297 |
{ |
298 |
String error = new String(data); |
299 |
int pos = error.indexOf("<?xml"); |
300 |
if (pos!= -1) |
301 |
{ |
302 |
String xml = error.substring(pos,error.length());
|
303 |
exceptionMessage = parseException(xml.getBytes()); |
304 |
} |
305 |
if (exceptionMessage == null) |
306 |
exceptionMessage = new String(data); |
307 |
|
308 |
} |
309 |
wmsEx = new WMSException(exceptionMessage);
|
310 |
wmsEx.setWMSMessage(new String(data)); |
311 |
downloader.removeURL(request); |
312 |
throw wmsEx;
|
313 |
} |
314 |
return f;
|
315 |
} |
316 |
catch(IOException e) |
317 |
{ |
318 |
logger.warn("Can't get legend graphics.",e);
|
319 |
throw new ServerErrorException(); |
320 |
} |
321 |
} |
322 |
|
323 |
public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) |
324 |
throws ServerErrorException, WMSException {
|
325 |
URL request = null; |
326 |
try {
|
327 |
request = new URL(buildGetLegendGraphicRequest(status, layerName)); |
328 |
logger.info(request.toString()); |
329 |
File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel); |
330 |
if (f== null) |
331 |
return null; |
332 |
if (Utilities.isTextFile(f)) { |
333 |
FileInputStream fis = new FileInputStream(f); |
334 |
FileChannel fc = fis.getChannel();
|
335 |
byte[] data = new byte[(int)fc.size()]; |
336 |
ByteBuffer bb = ByteBuffer.wrap(data); |
337 |
fc.read(bb); |
338 |
|
339 |
WMSException wmsEx = null;
|
340 |
|
341 |
String exceptionMessage = parseException(data);
|
342 |
if (exceptionMessage == null) { |
343 |
String error = new String(data); |
344 |
int pos = error.indexOf("<?xml"); |
345 |
if (pos!= -1) { |
346 |
String xml = error.substring(pos,error.length());
|
347 |
exceptionMessage = parseException(xml.getBytes()); |
348 |
} |
349 |
if (exceptionMessage == null) |
350 |
exceptionMessage = new String(data); |
351 |
|
352 |
} |
353 |
wmsEx = new WMSException(exceptionMessage);
|
354 |
wmsEx.setWMSMessage(new String(data)); |
355 |
Utilities.removeURL(request);
|
356 |
throw wmsEx;
|
357 |
} |
358 |
return f;
|
359 |
} catch(IOException e) { |
360 |
logger.warn("Can't get legend graphics",e);
|
361 |
throw new ServerErrorException(); |
362 |
} |
363 |
} |
364 |
|
365 |
/**
|
366 |
* @return string that represents the url for getting the wms legend
|
367 |
* If the layer has the object layer-->style-->legendurl that url will be returned
|
368 |
* otherwise builds the getLegendGraphic according to the OGC WMS Specifications
|
369 |
*
|
370 |
*/
|
371 |
private String buildGetLegendGraphicRequest(WMSStatus status, String layerName) { |
372 |
//TODO: deal with more than one layer
|
373 |
WMSLayer lyr = (WMSLayer) this.layers.get(layerName);
|
374 |
WMSStyle sty =null;
|
375 |
if (lyr != null){ |
376 |
Iterator it = lyr.getStyles().iterator();
|
377 |
while (it.hasNext()){
|
378 |
sty = (WMSStyle) it.next(); |
379 |
if (sty.getName().equals(status.getStyles().get(0).toString())){ |
380 |
return sty.getLegendURLOnlineResourceHRef();
|
381 |
} |
382 |
} |
383 |
} |
384 |
//TODO: pass by parameter the legend output format?
|
385 |
StringBuffer req = new StringBuffer(); |
386 |
String symbol = null; |
387 |
String onlineResource = null; |
388 |
|
389 |
if (status.getOnlineResource() == null) |
390 |
onlineResource = getHost(); |
391 |
else
|
392 |
onlineResource = status.getOnlineResource(); |
393 |
symbol = getSymbol(onlineResource); |
394 |
|
395 |
req.append(onlineResource + symbol + "REQUEST=GetLegendGraphic&SERVICE=WMS&VERSION=").append(getVersion());
|
396 |
req.append("&LAYER=" + layerName).append("&TRANSPARENT=TRUE").append("&FORMAT=image/png"); |
397 |
String aux = req.toString().replaceAll(" ", "%20"); |
398 |
logger.info("GetLegendGraphic url:" + aux);
|
399 |
return aux;
|
400 |
} |
401 |
|
402 |
public URL getMapURL(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
403 |
try {
|
404 |
WMSGetMapRequest request = createGetMapRequest(status); |
405 |
return request.getURL();
|
406 |
} catch(IOException e) { |
407 |
logger.warn("Can't get map URL",e);
|
408 |
throw new ServerErrorException(); |
409 |
} |
410 |
} |
411 |
|
412 |
/**
|
413 |
* Returns the exception message if the file is a XML instead of a image.
|
414 |
* @param file3
|
415 |
* @return
|
416 |
* @throws IOException
|
417 |
*/
|
418 |
public String getExceptionMessage(File f) throws IOException { |
419 |
if (f == null) |
420 |
return null; |
421 |
|
422 |
if (Utilities.isTextFile(f)) { |
423 |
byte[] data = fileToBytes(f); |
424 |
|
425 |
String exceptionMessage = parseException(data);
|
426 |
if (exceptionMessage == null) { |
427 |
String error = new String(data); |
428 |
int pos = error.indexOf("<?xml"); |
429 |
if (pos!= -1) { |
430 |
String xml = error.substring(pos,error.length());
|
431 |
exceptionMessage = parseException(xml.getBytes()); |
432 |
} |
433 |
if (exceptionMessage == null) |
434 |
exceptionMessage = new String(data); |
435 |
} |
436 |
return exceptionMessage;
|
437 |
} |
438 |
return null; |
439 |
} |
440 |
|
441 |
public File getMap(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
442 |
try {
|
443 |
WMSGetMapRequest request = createGetMapRequest(status); |
444 |
File f = request.sendRequest(cancel);
|
445 |
|
446 |
if (f== null) |
447 |
return null; |
448 |
if (Utilities.isTextFile(f)) { |
449 |
byte[] data = fileToBytes(f); |
450 |
|
451 |
WMSException wmsEx = null;
|
452 |
|
453 |
String exceptionMessage = parseException(data);
|
454 |
if (exceptionMessage==null) { |
455 |
String error = new String(data); |
456 |
int pos = error.indexOf("<?xml"); |
457 |
if (pos!= -1) { |
458 |
String xml = error.substring(pos,error.length());
|
459 |
exceptionMessage = parseException(xml.getBytes()); |
460 |
// if (exceptionMessage == null)
|
461 |
// exceptionMessage = new String(data);
|
462 |
} |
463 |
if (exceptionMessage == null) |
464 |
exceptionMessage = new String(data); |
465 |
|
466 |
} |
467 |
wmsEx = new WMSException(exceptionMessage);
|
468 |
wmsEx.setWMSMessage(new String(data)); |
469 |
|
470 |
// Since it is an error file, It must be deleted from the cache
|
471 |
downloader.removeURL(request); |
472 |
throw wmsEx;
|
473 |
} |
474 |
return f;
|
475 |
} catch(IOException e) { |
476 |
throw new ServerErrorException(e.getMessage(), e); |
477 |
} |
478 |
} |
479 |
|
480 |
|
481 |
/* (non-Javadoc)
|
482 |
* @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
|
483 |
*/
|
484 |
protected String parseException(byte[] data) { |
485 |
ArrayList errors = new ArrayList(); |
486 |
KXmlParser kxmlParser = new KXmlParser();
|
487 |
try
|
488 |
{ |
489 |
kxmlParser.setInput(new ByteArrayInputStream(data), encoding); |
490 |
kxmlParser.nextTag(); |
491 |
int tag;
|
492 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
493 |
{ |
494 |
kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
|
495 |
tag = kxmlParser.nextTag(); |
496 |
while(tag != KXmlParser.END_DOCUMENT)
|
497 |
{ |
498 |
switch(tag)
|
499 |
{ |
500 |
case KXmlParser.START_TAG:
|
501 |
if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){ |
502 |
String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE); |
503 |
errorCode = (errorCode != null) ? "["+errorCode+"] " : ""; |
504 |
String errorMessage = kxmlParser.nextText();
|
505 |
errors.add(errorCode+errorMessage); |
506 |
} |
507 |
break;
|
508 |
case KXmlParser.END_TAG:
|
509 |
break;
|
510 |
|
511 |
} |
512 |
tag = kxmlParser.nextTag(); |
513 |
} |
514 |
//kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
|
515 |
} |
516 |
} |
517 |
catch(XmlPullParserException parser_ex){
|
518 |
logger.warn("",parser_ex);
|
519 |
} |
520 |
catch (IOException ioe) { |
521 |
logger.warn("",ioe);
|
522 |
} |
523 |
String message = errors.size()>0? "" : null; |
524 |
for (int i = 0; i < errors.size(); i++) { |
525 |
message += (String) errors.get(i)+"\n"; |
526 |
} |
527 |
return message;
|
528 |
} |
529 |
/**
|
530 |
* Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
531 |
* without a VERSION, to get the highest version than a WMS supports.
|
532 |
*/
|
533 |
public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version) |
534 |
{ |
535 |
int index = _host.indexOf('?'); |
536 |
|
537 |
if (index > -1) { |
538 |
String host = _host.substring(0, index + 1); |
539 |
String query = _host.substring(index + 1, _host.length()); |
540 |
|
541 |
StringTokenizer tokens = new StringTokenizer(query, "&"); |
542 |
String newQuery = "", token; |
543 |
|
544 |
// If there is a field or a value with spaces, (and then it's on different tokens) -> unify them
|
545 |
while (tokens.hasMoreTokens()) {
|
546 |
token = tokens.nextToken().trim(); |
547 |
|
548 |
if (token.toUpperCase().compareTo("REQUEST=GETCAPABILITIES") == 0) |
549 |
continue;
|
550 |
|
551 |
if (token.toUpperCase().compareTo("SERVICE=WMS") == 0) |
552 |
continue;
|
553 |
|
554 |
if ((_version != null) && (_version.length() > 0)) { |
555 |
if (token.toUpperCase().compareTo("VERSION=" + _version) == 0) |
556 |
continue;
|
557 |
} |
558 |
|
559 |
if (token.toUpperCase().compareTo("EXCEPTIONS=XML") == 0) |
560 |
continue;
|
561 |
|
562 |
newQuery += token + "&";
|
563 |
} |
564 |
|
565 |
_host = host + newQuery; |
566 |
} |
567 |
else {
|
568 |
_host += "?";
|
569 |
} |
570 |
|
571 |
if ((_version != null) && (_version.compareTo("") != 0)) |
572 |
_host += "REQUEST=GetCapabilities&SERVICE=WMS&VERSION=" + _version;
|
573 |
else
|
574 |
_host += "REQUEST=GetCapabilities&SERVICE=WMS";
|
575 |
|
576 |
return _host;
|
577 |
} |
578 |
|
579 |
/**
|
580 |
* Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
581 |
* @param WMSStatus
|
582 |
*/
|
583 |
private String buildCapabilitiesRequest(WMSStatus status) |
584 |
{ |
585 |
StringBuffer req = new StringBuffer(); |
586 |
String symbol = null; |
587 |
|
588 |
String onlineResource;
|
589 |
if (status == null || status.getOnlineResource() == null) |
590 |
onlineResource = getHost(); |
591 |
else
|
592 |
onlineResource = status.getOnlineResource(); |
593 |
symbol = getSymbol(onlineResource); |
594 |
|
595 |
req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WMS&");
|
596 |
req.append("VERSION=").append(getVersion());
|
597 |
return req.toString();
|
598 |
} |
599 |
|
600 |
public void close() { |
601 |
// your code here
|
602 |
} |
603 |
|
604 |
/**
|
605 |
* @param status
|
606 |
* The WMS status
|
607 |
* @param protocolHandler
|
608 |
* The handler to parse the requests
|
609 |
* @return an object to send the GetMap requests
|
610 |
*/
|
611 |
protected abstract WMSGetMapRequest createGetMapRequest(WMSStatus status); |
612 |
|
613 |
protected abstract WMSGetFeatureInfoRequest createGetFeatureInfoRequest(WMSStatus status, int x, int y); |
614 |
|
615 |
protected abstract WMSGetLegendGraphicRequest createGetLegendGraphicRequest(WMSStatus status, String layerName); |
616 |
|
617 |
protected abstract WMSGetCapabilitiesRequest createGetCapabilitiesRequest(WMSStatus status); |
618 |
|
619 |
/**
|
620 |
* <p>Parses the Request tag </p>
|
621 |
*/
|
622 |
protected void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException |
623 |
{ |
624 |
int currentTag;
|
625 |
boolean end = false; |
626 |
|
627 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
|
628 |
currentTag = parser.next(); |
629 |
|
630 |
while (!end)
|
631 |
{ |
632 |
switch(currentTag)
|
633 |
{ |
634 |
case KXmlParser.START_TAG:
|
635 |
if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0) |
636 |
{ |
637 |
parserDcpType(parser, CapabilitiesTags.GETCAPABILITIES); |
638 |
} |
639 |
else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0) |
640 |
{ |
641 |
parseGetMapTag(parser); |
642 |
} |
643 |
else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0) |
644 |
{ |
645 |
parseGetFeatureInfoTag(parser); |
646 |
} |
647 |
else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0) |
648 |
{ |
649 |
parserDcpType(parser, CapabilitiesTags.DESCRIBELAYER); |
650 |
} |
651 |
else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0) |
652 |
{ |
653 |
parseGetLegendGraphicTag(parser); |
654 |
} |
655 |
break;
|
656 |
case KXmlParser.END_TAG:
|
657 |
if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0) |
658 |
end = true;
|
659 |
break;
|
660 |
case KXmlParser.TEXT:
|
661 |
break;
|
662 |
} |
663 |
if(!end)
|
664 |
currentTag = parser.next(); |
665 |
} |
666 |
// TODO: does not get such a tag when arrives here!!!!!!
|
667 |
//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
|
668 |
} |
669 |
/**
|
670 |
* <p>Parses the GetMap tag </p>
|
671 |
*/
|
672 |
protected void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException |
673 |
{ |
674 |
int currentTag;
|
675 |
boolean end = false; |
676 |
|
677 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
|
678 |
currentTag = parser.next(); |
679 |
|
680 |
while (!end)
|
681 |
{ |
682 |
switch(currentTag)
|
683 |
{ |
684 |
case KXmlParser.START_TAG:
|
685 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
686 |
{ |
687 |
serviceInfo.formats.add(parser.nextText()); |
688 |
} |
689 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
690 |
{ |
691 |
parserDcpType(parser, CapabilitiesTags.GETMAP); |
692 |
} |
693 |
break;
|
694 |
case KXmlParser.END_TAG:
|
695 |
if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0) |
696 |
end = true;
|
697 |
break;
|
698 |
case KXmlParser.TEXT:
|
699 |
break;
|
700 |
} |
701 |
if(!end)
|
702 |
currentTag = parser.next(); |
703 |
} |
704 |
} |
705 |
|
706 |
/**
|
707 |
* <p>Parses the GetFeatureInfo tag </p>
|
708 |
*/
|
709 |
protected void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException |
710 |
{ |
711 |
int currentTag;
|
712 |
boolean end = false; |
713 |
|
714 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
|
715 |
currentTag = parser.next(); |
716 |
|
717 |
while (!end)
|
718 |
{ |
719 |
switch(currentTag)
|
720 |
{ |
721 |
case KXmlParser.START_TAG:
|
722 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
723 |
{ |
724 |
serviceInfo.infoformats.add(parser.nextText()); |
725 |
} |
726 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
727 |
{ |
728 |
parserDcpType(parser, CapabilitiesTags.GETFEATUREINFO); |
729 |
} |
730 |
break;
|
731 |
case KXmlParser.END_TAG:
|
732 |
if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0) |
733 |
end = true;
|
734 |
break;
|
735 |
case KXmlParser.TEXT:
|
736 |
break;
|
737 |
} |
738 |
if(!end)
|
739 |
currentTag = parser.next(); |
740 |
} |
741 |
} |
742 |
|
743 |
/**
|
744 |
* <p>Parses the GetLegendGraphic tag </p>
|
745 |
*/
|
746 |
protected void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException |
747 |
{ |
748 |
int currentTag;
|
749 |
boolean end = false; |
750 |
|
751 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
|
752 |
currentTag = parser.next(); |
753 |
|
754 |
while (!end)
|
755 |
{ |
756 |
switch(currentTag)
|
757 |
{ |
758 |
case KXmlParser.START_TAG:
|
759 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
760 |
{ |
761 |
//TODO:
|
762 |
// add the supported formats by the GetLegendGraphic request
|
763 |
//serviceInfo.formats.add(parser.nextText());
|
764 |
} |
765 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
766 |
{ |
767 |
parserDcpType(parser, CapabilitiesTags.GETLEGENDGRAPHIC); |
768 |
} |
769 |
break;
|
770 |
case KXmlParser.END_TAG:
|
771 |
if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0) |
772 |
end = true;
|
773 |
break;
|
774 |
case KXmlParser.TEXT:
|
775 |
break;
|
776 |
} |
777 |
if(!end)
|
778 |
currentTag = parser.next(); |
779 |
} |
780 |
} |
781 |
} |