root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / WFSProtocolHandler.java @ 8017
History | View | Annotate | Download (13.6 KB)
1 |
package org.gvsig.remoteClient.wfs; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.URL; |
5 |
import java.util.HashMap; |
6 |
import java.util.Hashtable; |
7 |
import java.util.Set; |
8 |
import java.util.Vector; |
9 |
|
10 |
import org.gvsig.remoteClient.OGCProtocolHandler; |
11 |
import org.gvsig.remoteClient.utils.Utilities; |
12 |
import org.gvsig.remoteClient.wms.ICancellable; |
13 |
|
14 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
15 |
*
|
16 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
17 |
*
|
18 |
* This program is free software; you can redistribute it and/or
|
19 |
* modify it under the terms of the GNU General Public License
|
20 |
* as published by the Free Software Foundation; either version 2
|
21 |
* of the License, or (at your option) any later version.
|
22 |
*
|
23 |
* This program is distributed in the hope that it will be useful,
|
24 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
26 |
* GNU General Public License for more details.
|
27 |
*
|
28 |
* You should have received a copy of the GNU General Public License
|
29 |
* along with this program; if not, write to the Free Software
|
30 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
31 |
*
|
32 |
* For more information, contact:
|
33 |
*
|
34 |
* Generalitat Valenciana
|
35 |
* Conselleria d'Infraestructures i Transport
|
36 |
* Av. Blasco Ib??ez, 50
|
37 |
* 46010 VALENCIA
|
38 |
* SPAIN
|
39 |
*
|
40 |
* +34 963862235
|
41 |
* gvsig@gva.es
|
42 |
* www.gvsig.gva.es
|
43 |
*
|
44 |
* or
|
45 |
*
|
46 |
* IVER T.I. S.A
|
47 |
* Salamanca 50
|
48 |
* 46005 Valencia
|
49 |
* Spain
|
50 |
*
|
51 |
* +34 963163400
|
52 |
* dac@iver.es
|
53 |
*/
|
54 |
/* CVS MESSAGES:
|
55 |
*
|
56 |
* $Id: WFSProtocolHandler.java 8017 2006-10-10 12:52:28Z jorpiell $
|
57 |
* $Log$
|
58 |
* Revision 1.8 2006-10-10 12:52:28 jorpiell
|
59 |
* Soporte para features complejas.
|
60 |
*
|
61 |
* Revision 1.7 2006/06/21 12:53:03 jorpiell
|
62 |
* Se tienen en cuanta el n?mero de features
|
63 |
*
|
64 |
* Revision 1.6 2006/06/14 08:46:07 jorpiell
|
65 |
* Se tiene en cuanta la opcion para refrescar las capabilities
|
66 |
*
|
67 |
* Revision 1.5 2006/06/14 07:54:18 jorpiell
|
68 |
* Se parsea el online resource que antes se ignoraba
|
69 |
*
|
70 |
* Revision 1.4 2006/05/30 13:58:03 jaume
|
71 |
* cancelable downloads
|
72 |
*
|
73 |
* Revision 1.3 2006/05/23 13:23:13 jorpiell
|
74 |
* Se ha cambiado el final del bucle de parseado y se tiene en cuenta el online resource
|
75 |
*
|
76 |
* Revision 1.2 2006/04/20 16:39:16 jorpiell
|
77 |
* A?adida la operacion de describeFeatureType y el parser correspondiente.
|
78 |
*
|
79 |
* Revision 1.1 2006/04/19 12:51:35 jorpiell
|
80 |
* A?adidas algunas de las clases del servicio WFS
|
81 |
*
|
82 |
*
|
83 |
*/
|
84 |
/**
|
85 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
86 |
*/
|
87 |
public abstract class WFSProtocolHandler extends OGCProtocolHandler { |
88 |
/**
|
89 |
* Encoding used to parse different xml documents.
|
90 |
*/
|
91 |
protected String encoding = "UTF-8"; |
92 |
/**
|
93 |
* WFS metadata
|
94 |
*/
|
95 |
protected ServiceInformation serviceInfo = new ServiceInformation(); |
96 |
protected Hashtable features = new Hashtable(); |
97 |
protected String currentFeature = null; |
98 |
|
99 |
/**
|
100 |
* <p>Builds a GetCapabilities request that is sent to the WFS
|
101 |
* the response will be parse to extract the data needed by the
|
102 |
* WFS client</p>
|
103 |
*/
|
104 |
public void getCapabilities(WFSStatus status,boolean override, ICancellable cancel) { |
105 |
URL request = null; |
106 |
try {
|
107 |
request = new URL(buildCapabilitiesRequest(status)); |
108 |
} |
109 |
catch(Exception e) { |
110 |
e.printStackTrace(); |
111 |
} |
112 |
try {
|
113 |
if (override){
|
114 |
Utilities.removeURL(request);
|
115 |
} |
116 |
File f = Utilities.downloadFile(request,"wfs_capabilities.xml", cancel); |
117 |
if (f == null) |
118 |
return;
|
119 |
clear(); |
120 |
parseCapabilities(f); |
121 |
} catch(Exception e) { |
122 |
e.printStackTrace(); |
123 |
} |
124 |
} |
125 |
|
126 |
private void clear() { |
127 |
features.clear(); |
128 |
serviceInfo.clear(); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* @return
|
133 |
*/
|
134 |
private String buildCapabilitiesRequest(WFSStatus status) { |
135 |
StringBuffer req = new StringBuffer(); |
136 |
String symbol = null; |
137 |
|
138 |
String onlineResource;
|
139 |
if (status == null || status.getOnlineResource() == null) |
140 |
onlineResource = getHost(); |
141 |
else
|
142 |
onlineResource = status.getOnlineResource(); |
143 |
symbol = getSymbol(onlineResource); |
144 |
|
145 |
req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WFS&");
|
146 |
req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML"); |
147 |
return req.toString();
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* Builds the GetCapabilitiesRequest according to the OGC WFS Specifications
|
152 |
* without a VERSION, to get the highest version than a WFS supports.
|
153 |
*/
|
154 |
public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version) |
155 |
{ |
156 |
String req = new String(); |
157 |
String symbol = getSymbol(_host);
|
158 |
req = req + _host + symbol + "REQUEST=GetCapabilities&SERVICE=WFS&";
|
159 |
if((_version != null) && (_version.length()>0 )) |
160 |
{ |
161 |
req += ("&VERSION=" + _version);
|
162 |
} |
163 |
req += ("&EXCEPTIONS=XML");
|
164 |
|
165 |
return req;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* <p>Builds a describeFeatureType request that is sent to the WFS
|
170 |
* the response will be parse to extract the data needed by the
|
171 |
* WFS client</p>
|
172 |
* @param status
|
173 |
* WFS client status. Contains all the information to create
|
174 |
* the query. In this case, the only the feature name is needed.
|
175 |
*/
|
176 |
public void describeFeatureType(WFSStatus status) { |
177 |
URL request = null; |
178 |
this.currentFeature = status.getFeatureName();
|
179 |
try {
|
180 |
request = new URL(buildDescribeFeatureTypeRequest(status)); |
181 |
} |
182 |
catch(Exception e) { |
183 |
e.printStackTrace(); |
184 |
} |
185 |
try {
|
186 |
File f = Utilities.downloadFile(request, "wfs_describeFeatureType.xml", null); |
187 |
parseDescribeFeatureType(f,status.getNameSpace()); |
188 |
|
189 |
} catch(Exception e) { |
190 |
e.printStackTrace(); |
191 |
} |
192 |
} |
193 |
|
194 |
private String buildDescribeFeatureTypeRequest(WFSStatus status) { |
195 |
StringBuffer req = new StringBuffer(); |
196 |
String symbol = null; |
197 |
|
198 |
String onlineResource;
|
199 |
if(serviceInfo.getOnlineResource(WFSOperation.DESCRIBEFEATURETYPE) != null){ |
200 |
onlineResource = serviceInfo.getOnlineResource(WFSOperation.DESCRIBEFEATURETYPE); |
201 |
}else {
|
202 |
onlineResource = getHost(); |
203 |
} |
204 |
symbol = getSymbol(onlineResource); |
205 |
|
206 |
req.append(onlineResource).append(symbol).append("REQUEST=DescribeFeatureType&SERVICE=WFS&");
|
207 |
req.append("TYPENAME=").append(status.getFeatureName()).append("&"); |
208 |
req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML"); |
209 |
return req.toString();
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* parses the data retrieved by the DescribeCoverage XML document
|
214 |
*/
|
215 |
public abstract boolean parseDescribeFeatureType(File f,String nameSpace); |
216 |
|
217 |
/**
|
218 |
* <p>Builds a getFeature request that is sent to the WFS
|
219 |
* the response will be parse to extract the data needed by the
|
220 |
* WFS client</p>
|
221 |
* @param status
|
222 |
* WFS client status. Contains all the information to create
|
223 |
* the query.
|
224 |
* @return File
|
225 |
* GML file
|
226 |
*/
|
227 |
public File getFeature(WFSStatus status){ |
228 |
URL request = null; |
229 |
try {
|
230 |
request = new URL(buildGetFeatureRequest(status)); |
231 |
} |
232 |
catch(Exception e) { |
233 |
e.printStackTrace(); |
234 |
} |
235 |
try {
|
236 |
File f = Utilities.downloadFile(request, "wfs_getFeature.xml", null); |
237 |
return f;
|
238 |
|
239 |
} catch(Exception e) { |
240 |
e.printStackTrace(); |
241 |
return null; |
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* <p>Builds a getFeature request that is sent to the WFS
|
247 |
* the response will be parse to extract the data needed by the
|
248 |
* WFS client</p>
|
249 |
* @param status
|
250 |
* WFS client status. Contains all the information to create
|
251 |
* the query.
|
252 |
*/
|
253 |
private String buildGetFeatureRequest(WFSStatus status){ |
254 |
StringBuffer req = new StringBuffer(); |
255 |
String symbol = null; |
256 |
|
257 |
String onlineResource;
|
258 |
if(serviceInfo.getOnlineResource(WFSOperation.GETFEATURE) != null){ |
259 |
onlineResource = serviceInfo.getOnlineResource(WFSOperation.GETFEATURE); |
260 |
}else {
|
261 |
onlineResource = getHost(); |
262 |
} |
263 |
symbol = getSymbol(onlineResource); |
264 |
|
265 |
req.append(onlineResource).append(symbol).append("REQUEST=GetFeature&SERVICE=WFS&");
|
266 |
req.append("TYPENAME=").append(status.getFeatureName()).append("&"); |
267 |
String[] fields = status.getFields(); |
268 |
if (fields.length > 0){ |
269 |
req.append("PROPERTYNAME=");
|
270 |
req.append(fields[0]);
|
271 |
for (int i=1 ; i<fields.length ; i++){ |
272 |
req.append("," + fields[i]);
|
273 |
} |
274 |
req.append("&");
|
275 |
} |
276 |
if (status.getFilterQuery() != null){ |
277 |
req.append("FILTER=" + status.getFilterQuery() + "&"); |
278 |
} |
279 |
req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML"); |
280 |
req.append("&MAXFEATURES=").append(status.getBuffer());
|
281 |
return req.toString();
|
282 |
|
283 |
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Just for not repeat code. Gets the correct separator according
|
288 |
* to the server URL
|
289 |
* @param h
|
290 |
* @return
|
291 |
*/
|
292 |
private static String getSymbol(String h) { |
293 |
String symbol;
|
294 |
if (h.indexOf("?")==-1) |
295 |
symbol = "?";
|
296 |
else if (h.indexOf("?")!=h.length()-1) |
297 |
symbol = "&";
|
298 |
else
|
299 |
symbol = "";
|
300 |
return symbol;
|
301 |
} |
302 |
|
303 |
/**
|
304 |
* Returns the service Information
|
305 |
* @return
|
306 |
*/
|
307 |
public ServiceInformation getServiceInformation(){
|
308 |
return serviceInfo;
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* @return Returns the features.
|
313 |
*/
|
314 |
public Hashtable getFeatures() { |
315 |
return features;
|
316 |
} |
317 |
|
318 |
/**
|
319 |
* @return Returns the currentFeature.
|
320 |
*/
|
321 |
public String getCurrentFeature() { |
322 |
return currentFeature;
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Sets the fields of the current feature
|
327 |
* @param fields
|
328 |
*/
|
329 |
public void setFields(Vector fields){ |
330 |
WFSFeature feature = (WFSFeature) features.get(currentFeature); |
331 |
feature.setFields(fields); |
332 |
features.put(feature.getName(),feature); |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Sets the fields of the current feature
|
337 |
* @param fields
|
338 |
*/
|
339 |
public void setFields(Hashtable fields){ |
340 |
WFSFeature feature = (WFSFeature) features.get(currentFeature); |
341 |
Vector vFields = new Vector(); |
342 |
Set keys = fields.keySet();
|
343 |
for (int i=0 ; i<keys.size() ; i++){ |
344 |
vFields.add(fields.get(keys.toArray()[i])); |
345 |
} |
346 |
feature.setFields(vFields); |
347 |
features.put(feature.getName(),feature); |
348 |
} |
349 |
|
350 |
|
351 |
/**
|
352 |
* @param currentFeature The currentFeature to set.
|
353 |
*/
|
354 |
public void setCurrentFeature(String currentFeature) { |
355 |
this.currentFeature = currentFeature;
|
356 |
} |
357 |
|
358 |
public class ServiceInformation { |
359 |
|
360 |
public String online_resource = null; |
361 |
public String version; |
362 |
public String name; |
363 |
public String scope; |
364 |
public String title; |
365 |
public String abstr; |
366 |
public String keywords; |
367 |
public String fees; |
368 |
public String operationsInfo; |
369 |
public String personname; |
370 |
public String organization; |
371 |
public String function; |
372 |
public String addresstype; |
373 |
public String address; |
374 |
public String place; |
375 |
public String province; |
376 |
public String postcode; |
377 |
public String country; |
378 |
public String phone; |
379 |
public String fax; |
380 |
public String email; |
381 |
public Vector formats; |
382 |
public HashMap operations; |
383 |
|
384 |
public ServiceInformation() {
|
385 |
clear(); |
386 |
} |
387 |
|
388 |
public void clear() { |
389 |
version = new String(); |
390 |
name = new String(); |
391 |
scope = new String(); |
392 |
title = new String(); |
393 |
abstr = new String(); |
394 |
keywords = new String(); |
395 |
fees = new String(); |
396 |
operationsInfo = new String(); |
397 |
personname = new String(); |
398 |
organization = new String(); |
399 |
function = new String(); |
400 |
addresstype = new String(); |
401 |
address = new String(); |
402 |
place = new String(); |
403 |
province = new String(); |
404 |
postcode = new String(); |
405 |
country = new String(); |
406 |
phone = new String(); |
407 |
fax = new String(); |
408 |
email = new String(); |
409 |
formats = new Vector(); |
410 |
operations = new HashMap(); |
411 |
} |
412 |
|
413 |
/**
|
414 |
* @return Returns the online_resource.
|
415 |
*/
|
416 |
public String getOnline_resource() { |
417 |
return online_resource;
|
418 |
} |
419 |
|
420 |
public void addOperation(int operation){ |
421 |
operations.put(new Integer(operation),new WFSOperation(operation)); |
422 |
} |
423 |
|
424 |
public void addOperation(int operation,String onlineResource){ |
425 |
operations.put(new Integer(operation),new WFSOperation(operation,onlineResource)); |
426 |
} |
427 |
|
428 |
public String getOnlineResource(int operation){ |
429 |
WFSOperation op = (WFSOperation)operations.get(new Integer(operation)); |
430 |
if ((op == null) || |
431 |
(op.getOnlineResource() == null) ||
|
432 |
(op.getOnlineResource().equals(""))){
|
433 |
return null; |
434 |
} |
435 |
return op.getOnlineResource();
|
436 |
|
437 |
} |
438 |
|
439 |
} |
440 |
|
441 |
public class WFSOperation{ |
442 |
public static final int GETCAPABILITIES = 0; |
443 |
public static final int DESCRIBEFEATURETYPE = 1; |
444 |
public static final int GETFEATURE = 2; |
445 |
|
446 |
private int operationName; |
447 |
private String onlineResource; |
448 |
|
449 |
public WFSOperation(int operationName) { |
450 |
super();
|
451 |
this.operationName = operationName;
|
452 |
} |
453 |
|
454 |
public WFSOperation(int operationName, String onlineResource) { |
455 |
super();
|
456 |
this.operationName = operationName;
|
457 |
this.onlineResource = onlineResource;
|
458 |
} |
459 |
|
460 |
/**
|
461 |
* @return Returns the onlineResource.
|
462 |
*/
|
463 |
public String getOnlineResource() { |
464 |
return onlineResource;
|
465 |
} |
466 |
/**
|
467 |
* @param onlineResource The onlineResource to set.
|
468 |
*/
|
469 |
public void setOnlineResource(String onlineResource) { |
470 |
this.onlineResource = onlineResource;
|
471 |
} |
472 |
/**
|
473 |
* @return Returns the operationName.
|
474 |
*/
|
475 |
public int getOperationName() { |
476 |
return operationName;
|
477 |
} |
478 |
/**
|
479 |
* @param operationName The operationName to set.
|
480 |
*/
|
481 |
public void setOperationName(int operationName) { |
482 |
this.operationName = operationName;
|
483 |
} |
484 |
} |
485 |
|
486 |
|
487 |
} |