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