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