gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.remoteclient / src / main / java / org / gvsig / remoteclient / wms / WMSProtocolHandler.java @ 4174
History | View | Annotate | Download (25.4 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 |
if (f == null) |
114 |
return;
|
115 |
clear(); |
116 |
parseCapabilities(f); |
117 |
} catch(Exception e) { |
118 |
logger.warn("Can't get capabilities.",e);
|
119 |
} |
120 |
} |
121 |
|
122 |
private void clear() { |
123 |
layers.clear(); |
124 |
serviceInfo.clear(); |
125 |
} |
126 |
|
127 |
/**
|
128 |
* <p>It will send a GetFeatureInfo request to the WMS
|
129 |
* Parsing the response and redirecting the info to the WMS client</p>
|
130 |
* TODO: return a stored file instead a String.
|
131 |
*/
|
132 |
public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel) |
133 |
{ |
134 |
StringBuffer output = new StringBuffer(); |
135 |
String outputFormat = new String(); |
136 |
String ServiceException = "ServiceExceptionReport"; |
137 |
StringBuffer sb = new StringBuffer(); |
138 |
sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
|
139 |
try
|
140 |
{ |
141 |
WMSGetFeatureInfoRequest request = createGetFeatureInfoRequest(status, x, y); |
142 |
URL url = request.getURL();
|
143 |
outputFormat = url.openConnection().getContentType(); |
144 |
File f = request.sendRequest(cancel);
|
145 |
if (f == null){ |
146 |
return ""; |
147 |
} |
148 |
|
149 |
FileReader fReader = new FileReader(f); |
150 |
char[] buffer = new char[1024*256]; |
151 |
for (int i = fReader.read(buffer); i>0; i = fReader.read(buffer)) |
152 |
{ |
153 |
String str = new String(buffer,0,i); |
154 |
output.append(str); |
155 |
} |
156 |
if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1) |
157 |
||output.toString().toLowerCase().startsWith("<?xml")
|
158 |
||(outputFormat.indexOf("gml") != -1)) |
159 |
{ |
160 |
int tag;
|
161 |
KXmlParser kxmlParser = null;
|
162 |
kxmlParser = new KXmlParser();
|
163 |
//kxmlParser.setInput(new StringReader(output.toString()));
|
164 |
kxmlParser.setInput(new FileReader(f)); |
165 |
|
166 |
tag = kxmlParser.nextTag(); |
167 |
if (kxmlParser.getName().compareTo(ServiceException)==0) |
168 |
{ |
169 |
sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>"); |
170 |
return sb.toString();
|
171 |
} |
172 |
else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0) |
173 |
{ |
174 |
return output.toString();
|
175 |
} |
176 |
else
|
177 |
{ |
178 |
return output.toString();
|
179 |
} |
180 |
} |
181 |
else
|
182 |
{ |
183 |
//Para que funcione con el GetFeatureInfo Viewer generico hay que devolver:
|
184 |
return output.toString();
|
185 |
} |
186 |
} |
187 |
catch(XmlPullParserException parserEx)
|
188 |
{ |
189 |
if (output.toString().toLowerCase().indexOf("xml") != -1) |
190 |
{ |
191 |
return output.toString().trim();
|
192 |
} |
193 |
else
|
194 |
{ |
195 |
sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
196 |
return sb.toString();
|
197 |
} |
198 |
} |
199 |
catch(Exception e) |
200 |
{ |
201 |
logger.warn("Can't get information by point.",e);
|
202 |
sb.append("<INFO>").append("Info format not supported").append("</INFO>"); |
203 |
return sb.toString();
|
204 |
|
205 |
} |
206 |
} |
207 |
/**
|
208 |
* <p>Builds a GetMap request that is sent to the WMS
|
209 |
* the response (image) will be redirect to the
|
210 |
* WMS client</p>
|
211 |
*/
|
212 |
public byte[] _getMap(WMSStatus status) throws ServerErrorException, WMSException |
213 |
{ |
214 |
try
|
215 |
{ |
216 |
//TODO:
|
217 |
//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
|
218 |
WMSGetMapRequest request = createGetMapRequest(status); |
219 |
URL url = request.getURL();
|
220 |
|
221 |
URLConnection conn = url.openConnection();
|
222 |
logger.info(request.toString()); |
223 |
String type = conn.getContentType();
|
224 |
|
225 |
|
226 |
byte[] imageBytes = null; |
227 |
byte[] buffer = new byte[1024*256]; |
228 |
InputStream is = conn.getInputStream();
|
229 |
int readed = 0; |
230 |
|
231 |
for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
232 |
// Creates a new buffer to contain the previous readed bytes and the next bunch of bytes
|
233 |
byte[] buffered = new byte[readed+i]; |
234 |
for (int j = 0; j < buffered.length; j++) { |
235 |
if (j<readed){
|
236 |
// puts the previously downloaded bytes into the image buffer
|
237 |
buffered[j] = imageBytes[j]; |
238 |
} |
239 |
else {
|
240 |
// appends the recently downloaded bytes to the image buffer.
|
241 |
buffered[j] = buffer[j-readed]; |
242 |
} |
243 |
} |
244 |
imageBytes = (byte[]) buffered.clone(); |
245 |
readed += i; |
246 |
} |
247 |
|
248 |
if ((type !=null && !type.subSequence(0,5).equals("image")) |
249 |
||(Utilities.isTextData(imageBytes)))
|
250 |
{ |
251 |
WMSException wmsEx = null;
|
252 |
|
253 |
String exceptionMessage = parseException(imageBytes);
|
254 |
if (exceptionMessage==null) |
255 |
{ |
256 |
String error = new String(imageBytes); |
257 |
int pos = error.indexOf("<?xml"); |
258 |
if (pos!= -1) |
259 |
{ |
260 |
String xml = error.substring(pos,error.length());
|
261 |
exceptionMessage = parseException(xml.getBytes()); |
262 |
if (exceptionMessage == null) |
263 |
exceptionMessage = new String(imageBytes); |
264 |
} |
265 |
} |
266 |
wmsEx = new WMSException(exceptionMessage);
|
267 |
wmsEx.setWMSMessage(new String(imageBytes)); |
268 |
throw wmsEx;
|
269 |
} |
270 |
return imageBytes;
|
271 |
} |
272 |
catch(IOException e) |
273 |
{ |
274 |
logger.warn("Can't build map request.",e);
|
275 |
throw new ServerErrorException(); |
276 |
} |
277 |
} |
278 |
|
279 |
public File getLegendGraphic_old(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException |
280 |
{ |
281 |
try
|
282 |
{ |
283 |
WMSGetLegendGraphicRequest request = createGetLegendGraphicRequest(status, layerName); |
284 |
File f = request.sendRequest(cancel);
|
285 |
if (f== null) |
286 |
return null; |
287 |
if (Utilities.isTextFile(f)) { |
288 |
|
289 |
byte[] data = fileToBytes(f); |
290 |
|
291 |
WMSException wmsEx = null;
|
292 |
|
293 |
String exceptionMessage = parseException(data);
|
294 |
if (exceptionMessage==null) |
295 |
{ |
296 |
String error = new String(data); |
297 |
int pos = error.indexOf("<?xml"); |
298 |
if (pos!= -1) |
299 |
{ |
300 |
String xml = error.substring(pos,error.length());
|
301 |
exceptionMessage = parseException(xml.getBytes()); |
302 |
} |
303 |
if (exceptionMessage == null) |
304 |
exceptionMessage = new String(data); |
305 |
|
306 |
} |
307 |
wmsEx = new WMSException(exceptionMessage);
|
308 |
wmsEx.setWMSMessage(new String(data)); |
309 |
downloader.removeURL(request); |
310 |
throw wmsEx;
|
311 |
} |
312 |
return f;
|
313 |
} |
314 |
catch(IOException e) |
315 |
{ |
316 |
logger.warn("Can't get legend graphics.",e);
|
317 |
throw new ServerErrorException(); |
318 |
} |
319 |
} |
320 |
|
321 |
public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) |
322 |
throws ServerErrorException, WMSException {
|
323 |
URL request = null; |
324 |
try {
|
325 |
status.setOnlineResource(status.getOnlineResource().replaceAll(" ", "%20")); |
326 |
request = new URL(buildGetLegendGraphicRequest(status, layerName)); |
327 |
logger.info(request.toString()); |
328 |
File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel); |
329 |
if (f== null) |
330 |
return null; |
331 |
if (Utilities.isTextFile(f)) { |
332 |
FileInputStream fis = new FileInputStream(f); |
333 |
FileChannel fc = fis.getChannel();
|
334 |
byte[] data = new byte[(int)fc.size()]; |
335 |
ByteBuffer bb = ByteBuffer.wrap(data); |
336 |
fc.read(bb); |
337 |
|
338 |
WMSException wmsEx = null;
|
339 |
|
340 |
String exceptionMessage = parseException(data);
|
341 |
if (exceptionMessage == null) { |
342 |
String error = new String(data); |
343 |
int pos = error.indexOf("<?xml"); |
344 |
if (pos!= -1) { |
345 |
String xml = error.substring(pos,error.length());
|
346 |
exceptionMessage = parseException(xml.getBytes()); |
347 |
} |
348 |
if (exceptionMessage == null) |
349 |
exceptionMessage = new String(data); |
350 |
|
351 |
} |
352 |
wmsEx = new WMSException(exceptionMessage);
|
353 |
wmsEx.setWMSMessage(new String(data)); |
354 |
Utilities.removeURL(request);
|
355 |
throw wmsEx;
|
356 |
} |
357 |
return f;
|
358 |
} catch(IOException e) { |
359 |
logger.warn("Can't get legend graphics",e);
|
360 |
throw new ServerErrorException(); |
361 |
} |
362 |
} |
363 |
|
364 |
/**
|
365 |
* @return string that represents the url for getting the wms legend
|
366 |
* If the layer has the object layer-->style-->legendurl that url will be returned
|
367 |
* otherwise builds the getLegendGraphic according to the OGC WMS Specifications
|
368 |
*
|
369 |
*/
|
370 |
private String buildGetLegendGraphicRequest(WMSStatus status, String layerName) { |
371 |
//TODO: deal with more than one layer
|
372 |
WMSLayer lyr = (WMSLayer) this.layers.get(layerName);
|
373 |
WMSStyle sty =null;
|
374 |
if (lyr != null){ |
375 |
Iterator it = lyr.getStyles().iterator();
|
376 |
while (it.hasNext()){
|
377 |
sty = (WMSStyle) it.next(); |
378 |
if (sty.getName().equals(status.getStyles().get(0).toString())){ |
379 |
return sty.getLegendURLOnlineResourceHRef().replaceAll(" ", "%20"); |
380 |
} |
381 |
} |
382 |
} |
383 |
//TODO: pass by parameter the legend output format?
|
384 |
StringBuffer req = new StringBuffer(); |
385 |
String symbol = null; |
386 |
String onlineResource = null; |
387 |
|
388 |
if (status.getOnlineResource() == null) |
389 |
onlineResource = getHost(); |
390 |
else
|
391 |
onlineResource = status.getOnlineResource(); |
392 |
symbol = getSymbol(onlineResource); |
393 |
|
394 |
req.append(onlineResource + symbol + "REQUEST=GetLegendGraphic&SERVICE=WMS&VERSION=").append(getVersion());
|
395 |
req.append("&LAYER=" + layerName).append("&TRANSPARENT=TRUE").append("&FORMAT=image/png"); |
396 |
String aux = req.toString().replaceAll(" ", "%20"); |
397 |
logger.info("GetLegendGraphic url:" + aux);
|
398 |
return aux;
|
399 |
} |
400 |
|
401 |
public URL getMapURL(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
402 |
try {
|
403 |
WMSGetMapRequest request = createGetMapRequest(status); |
404 |
return request.getURL();
|
405 |
} catch(IOException e) { |
406 |
logger.warn("Can't get map URL",e);
|
407 |
throw new ServerErrorException(); |
408 |
} |
409 |
} |
410 |
|
411 |
/**
|
412 |
* Returns the exception message if the file is a XML instead of a image.
|
413 |
* @param file3
|
414 |
* @return
|
415 |
* @throws IOException
|
416 |
*/
|
417 |
public String getExceptionMessage(File f) throws IOException { |
418 |
if (f == null) |
419 |
return null; |
420 |
|
421 |
if (Utilities.isTextFile(f)) { |
422 |
byte[] data = fileToBytes(f); |
423 |
|
424 |
String exceptionMessage = parseException(data);
|
425 |
if (exceptionMessage == null) { |
426 |
String error = new String(data); |
427 |
int pos = error.indexOf("<?xml"); |
428 |
if (pos!= -1) { |
429 |
String xml = error.substring(pos,error.length());
|
430 |
exceptionMessage = parseException(xml.getBytes()); |
431 |
} |
432 |
if (exceptionMessage == null) |
433 |
exceptionMessage = new String(data); |
434 |
} |
435 |
return exceptionMessage;
|
436 |
} |
437 |
return null; |
438 |
} |
439 |
|
440 |
public File getMap(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException { |
441 |
try {
|
442 |
WMSGetMapRequest request = createGetMapRequest(status); |
443 |
File f = request.sendRequest(cancel);
|
444 |
|
445 |
if (f== null) |
446 |
return null; |
447 |
if (Utilities.isTextFile(f)) { |
448 |
byte[] data = fileToBytes(f); |
449 |
|
450 |
WMSException wmsEx = null;
|
451 |
|
452 |
String exceptionMessage = parseException(data);
|
453 |
if (exceptionMessage==null) { |
454 |
String error = new String(data); |
455 |
int pos = error.indexOf("<?xml"); |
456 |
if (pos!= -1) { |
457 |
String xml = error.substring(pos,error.length());
|
458 |
exceptionMessage = parseException(xml.getBytes()); |
459 |
// if (exceptionMessage == null)
|
460 |
// exceptionMessage = new String(data);
|
461 |
} |
462 |
if (exceptionMessage == null) |
463 |
exceptionMessage = new String(data); |
464 |
|
465 |
} |
466 |
wmsEx = new WMSException(exceptionMessage);
|
467 |
wmsEx.setWMSMessage(new String(data)); |
468 |
|
469 |
// Since it is an error file, It must be deleted from the cache
|
470 |
downloader.removeURL(request); |
471 |
throw wmsEx;
|
472 |
} |
473 |
return f;
|
474 |
} catch(IOException e) { |
475 |
throw new ServerErrorException(e.getMessage(), e); |
476 |
} |
477 |
} |
478 |
|
479 |
|
480 |
/* (non-Javadoc)
|
481 |
* @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
|
482 |
*/
|
483 |
protected String parseException(byte[] data) { |
484 |
ArrayList errors = new ArrayList(); |
485 |
KXmlParser kxmlParser = new KXmlParser();
|
486 |
try
|
487 |
{ |
488 |
kxmlParser.setInput(new ByteArrayInputStream(data), encoding); |
489 |
kxmlParser.nextTag(); |
490 |
int tag;
|
491 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
492 |
{ |
493 |
kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
|
494 |
tag = kxmlParser.nextTag(); |
495 |
while(tag != KXmlParser.END_DOCUMENT)
|
496 |
{ |
497 |
switch(tag)
|
498 |
{ |
499 |
case KXmlParser.START_TAG:
|
500 |
if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){ |
501 |
String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE); |
502 |
errorCode = (errorCode != null) ? "["+errorCode+"] " : ""; |
503 |
String errorMessage = kxmlParser.nextText();
|
504 |
errors.add(errorCode+errorMessage); |
505 |
} |
506 |
break;
|
507 |
case KXmlParser.END_TAG:
|
508 |
break;
|
509 |
|
510 |
} |
511 |
tag = kxmlParser.nextTag(); |
512 |
} |
513 |
//kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
|
514 |
} |
515 |
} |
516 |
catch(XmlPullParserException parser_ex){
|
517 |
logger.warn("",parser_ex);
|
518 |
} |
519 |
catch (IOException ioe) { |
520 |
logger.warn("",ioe);
|
521 |
} |
522 |
String message = errors.size()>0? "" : null; |
523 |
for (int i = 0; i < errors.size(); i++) { |
524 |
message += (String) errors.get(i)+"\n"; |
525 |
} |
526 |
return message;
|
527 |
} |
528 |
/**
|
529 |
* Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
530 |
* without a VERSION, to get the highest version than a WMS supports.
|
531 |
*/
|
532 |
public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version) |
533 |
{ |
534 |
int index = _host.indexOf('?'); |
535 |
|
536 |
if (index > -1) { |
537 |
String host = _host.substring(0, index + 1); |
538 |
String query = _host.substring(index + 1, _host.length()); |
539 |
|
540 |
StringTokenizer tokens = new StringTokenizer(query, "&"); |
541 |
String newQuery = "", token; |
542 |
|
543 |
// If there is a field or a value with spaces, (and then it's on different tokens) -> unify them
|
544 |
while (tokens.hasMoreTokens()) {
|
545 |
token = tokens.nextToken().trim(); |
546 |
|
547 |
if (token.toUpperCase().compareTo("REQUEST=GETCAPABILITIES") == 0) |
548 |
continue;
|
549 |
|
550 |
if (token.toUpperCase().compareTo("SERVICE=WMS") == 0) |
551 |
continue;
|
552 |
|
553 |
if ((_version != null) && (_version.length() > 0)) { |
554 |
if (token.toUpperCase().compareTo("VERSION=" + _version) == 0) |
555 |
continue;
|
556 |
} |
557 |
|
558 |
if (token.toUpperCase().compareTo("EXCEPTIONS=XML") == 0) |
559 |
continue;
|
560 |
|
561 |
newQuery += token + "&";
|
562 |
} |
563 |
|
564 |
_host = host + newQuery; |
565 |
} |
566 |
else {
|
567 |
_host += "?";
|
568 |
} |
569 |
|
570 |
if ((_version != null) && (_version.compareTo("") != 0)) |
571 |
_host += "REQUEST=GetCapabilities&SERVICE=WMS&VERSION=" + _version;
|
572 |
else
|
573 |
_host += "REQUEST=GetCapabilities&SERVICE=WMS";
|
574 |
|
575 |
return _host;
|
576 |
} |
577 |
|
578 |
/**
|
579 |
* Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
580 |
* @param WMSStatus
|
581 |
*/
|
582 |
private String buildCapabilitiesRequest(WMSStatus status) |
583 |
{ |
584 |
StringBuffer req = new StringBuffer(); |
585 |
String symbol = null; |
586 |
|
587 |
String onlineResource;
|
588 |
if (status == null || status.getOnlineResource() == null) |
589 |
onlineResource = getHost(); |
590 |
else
|
591 |
onlineResource = status.getOnlineResource(); |
592 |
symbol = getSymbol(onlineResource); |
593 |
|
594 |
req.append(onlineResource).append(symbol); |
595 |
if(req.toString().toLowerCase().lastIndexOf("request=getcapabilities") == -1 ){ |
596 |
req.append("REQUEST=GetCapabilities");
|
597 |
} |
598 |
if(req.toString().toLowerCase().lastIndexOf("&service=wms") == -1){ |
599 |
req.append("&SERVICE=WMS");
|
600 |
} |
601 |
if(req.toString().toLowerCase().lastIndexOf("&version=") == -1){ |
602 |
req.append("&VERSION=").append(getVersion());
|
603 |
} |
604 |
return req.toString();
|
605 |
} |
606 |
|
607 |
public void close() { |
608 |
// your code here
|
609 |
} |
610 |
|
611 |
/**
|
612 |
* @param status
|
613 |
* The WMS status
|
614 |
* @param protocolHandler
|
615 |
* The handler to parse the requests
|
616 |
* @return an object to send the GetMap requests
|
617 |
*/
|
618 |
protected abstract WMSGetMapRequest createGetMapRequest(WMSStatus status); |
619 |
|
620 |
protected abstract WMSGetFeatureInfoRequest createGetFeatureInfoRequest(WMSStatus status, int x, int y); |
621 |
|
622 |
protected abstract WMSGetLegendGraphicRequest createGetLegendGraphicRequest(WMSStatus status, String layerName); |
623 |
|
624 |
protected abstract WMSGetCapabilitiesRequest createGetCapabilitiesRequest(WMSStatus status); |
625 |
|
626 |
/**
|
627 |
* <p>Parses the Request tag </p>
|
628 |
*/
|
629 |
protected void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException |
630 |
{ |
631 |
int currentTag;
|
632 |
boolean end = false; |
633 |
|
634 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
|
635 |
currentTag = parser.next(); |
636 |
|
637 |
while (!end)
|
638 |
{ |
639 |
switch(currentTag)
|
640 |
{ |
641 |
case KXmlParser.START_TAG:
|
642 |
if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0) |
643 |
{ |
644 |
parserDcpType(parser, CapabilitiesTags.GETCAPABILITIES); |
645 |
} |
646 |
else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0) |
647 |
{ |
648 |
parseGetMapTag(parser); |
649 |
} |
650 |
else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0) |
651 |
{ |
652 |
parseGetFeatureInfoTag(parser); |
653 |
} |
654 |
else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0) |
655 |
{ |
656 |
parserDcpType(parser, CapabilitiesTags.DESCRIBELAYER); |
657 |
} |
658 |
else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0) |
659 |
{ |
660 |
parseGetLegendGraphicTag(parser); |
661 |
} |
662 |
break;
|
663 |
case KXmlParser.END_TAG:
|
664 |
if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0) |
665 |
end = true;
|
666 |
break;
|
667 |
case KXmlParser.TEXT:
|
668 |
break;
|
669 |
} |
670 |
if(!end)
|
671 |
currentTag = parser.next(); |
672 |
} |
673 |
// TODO: does not get such a tag when arrives here!!!!!!
|
674 |
//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
|
675 |
} |
676 |
/**
|
677 |
* <p>Parses the GetMap tag </p>
|
678 |
*/
|
679 |
protected void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException |
680 |
{ |
681 |
int currentTag;
|
682 |
boolean end = false; |
683 |
|
684 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
|
685 |
currentTag = parser.next(); |
686 |
|
687 |
while (!end)
|
688 |
{ |
689 |
switch(currentTag)
|
690 |
{ |
691 |
case KXmlParser.START_TAG:
|
692 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
693 |
{ |
694 |
serviceInfo.formats.add(parser.nextText()); |
695 |
} |
696 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
697 |
{ |
698 |
parserDcpType(parser, CapabilitiesTags.GETMAP); |
699 |
} |
700 |
break;
|
701 |
case KXmlParser.END_TAG:
|
702 |
if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0) |
703 |
end = true;
|
704 |
break;
|
705 |
case KXmlParser.TEXT:
|
706 |
break;
|
707 |
} |
708 |
if(!end)
|
709 |
currentTag = parser.next(); |
710 |
} |
711 |
} |
712 |
|
713 |
/**
|
714 |
* <p>Parses the GetFeatureInfo tag </p>
|
715 |
*/
|
716 |
protected void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException |
717 |
{ |
718 |
int currentTag;
|
719 |
boolean end = false; |
720 |
|
721 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
|
722 |
currentTag = parser.next(); |
723 |
|
724 |
while (!end)
|
725 |
{ |
726 |
switch(currentTag)
|
727 |
{ |
728 |
case KXmlParser.START_TAG:
|
729 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
730 |
{ |
731 |
serviceInfo.infoformats.add(parser.nextText()); |
732 |
} |
733 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
734 |
{ |
735 |
parserDcpType(parser, CapabilitiesTags.GETFEATUREINFO); |
736 |
} |
737 |
break;
|
738 |
case KXmlParser.END_TAG:
|
739 |
if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0) |
740 |
end = true;
|
741 |
break;
|
742 |
case KXmlParser.TEXT:
|
743 |
break;
|
744 |
} |
745 |
if(!end)
|
746 |
currentTag = parser.next(); |
747 |
} |
748 |
} |
749 |
|
750 |
/**
|
751 |
* <p>Parses the GetLegendGraphic tag </p>
|
752 |
*/
|
753 |
protected void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException |
754 |
{ |
755 |
int currentTag;
|
756 |
boolean end = false; |
757 |
|
758 |
parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
|
759 |
currentTag = parser.next(); |
760 |
|
761 |
while (!end)
|
762 |
{ |
763 |
switch(currentTag)
|
764 |
{ |
765 |
case KXmlParser.START_TAG:
|
766 |
if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0) |
767 |
{ |
768 |
//TODO:
|
769 |
// add the supported formats by the GetLegendGraphic request
|
770 |
//serviceInfo.formats.add(parser.nextText());
|
771 |
} |
772 |
else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0) |
773 |
{ |
774 |
parserDcpType(parser, CapabilitiesTags.GETLEGENDGRAPHIC); |
775 |
} |
776 |
break;
|
777 |
case KXmlParser.END_TAG:
|
778 |
if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0) |
779 |
end = true;
|
780 |
break;
|
781 |
case KXmlParser.TEXT:
|
782 |
break;
|
783 |
} |
784 |
if(!end)
|
785 |
currentTag = parser.next(); |
786 |
} |
787 |
} |
788 |
} |