Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / WFSProtocolHandler.java @ 5536

History | View | Annotate | Download (11 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

    
13
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
14
 *
15
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
30
 *
31
 * For more information, contact:
32
 *
33
 *  Generalitat Valenciana
34
 *   Conselleria d'Infraestructures i Transport
35
 *   Av. Blasco Ib??ez, 50
36
 *   46010 VALENCIA
37
 *   SPAIN
38
 *
39
 *      +34 963862235
40
 *   gvsig@gva.es
41
 *      www.gvsig.gva.es
42
 *
43
 *    or
44
 *
45
 *   IVER T.I. S.A
46
 *   Salamanca 50
47
 *   46005 Valencia
48
 *   Spain
49
 *
50
 *   +34 963163400
51
 *   dac@iver.es
52
 */
53
/* CVS MESSAGES:
54
 *
55
 * $Id: WFSProtocolHandler.java 5536 2006-05-30 13:58:18Z jaume $
56
 * $Log$
57
 * Revision 1.4  2006-05-30 13:58:03  jaume
58
 * cancelable downloads
59
 *
60
 * Revision 1.3  2006/05/23 13:23:13  jorpiell
61
 * Se ha cambiado el final del bucle de parseado y se tiene en cuenta el online resource
62
 *
63
 * Revision 1.2  2006/04/20 16:39:16  jorpiell
64
 * A?adida la operacion de describeFeatureType y el parser correspondiente.
65
 *
66
 * Revision 1.1  2006/04/19 12:51:35  jorpiell
67
 * A?adidas algunas de las clases del servicio WFS
68
 *
69
 *
70
 */
71
/**
72
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
73
 */
74
public abstract class WFSProtocolHandler extends OGCProtocolHandler {
75
        /**
76
         * Encoding used to parse different xml documents.
77
         */
78
        protected String encoding = "UTF-8";
79
        /**
80
     * WFS metadata
81
     */
82
    protected ServiceInformation serviceInfo = new ServiceInformation();
83
        protected Hashtable features = new Hashtable();
84
        protected String currentFeature = null;
85
        
86
        /**
87
     * <p>Builds a GetCapabilities request that is sent to the WFS
88
     * the response will be parse to extract the data needed by the
89
     * WFS client</p>
90
     */
91
    public void getCapabilities(WFSStatus status) {
92
           URL request = null;
93
            try {
94
                request = new URL(buildCapabilitiesRequest(status));
95
            }
96
            catch(Exception e) {
97
                e.printStackTrace();
98
            }
99
            try {
100
                    File f = Utilities.downloadFile(request,"wfs_capabilities.xml", null);
101
                    parseCapabilities(f);
102
            } catch(Exception e) {
103
                    e.printStackTrace();
104
            }
105
    }
106
    
107
    /**
108
     * @return
109
     */
110
    private String buildCapabilitiesRequest(WFSStatus status) {
111
            StringBuffer req = new StringBuffer();
112
                String symbol = null;
113
                
114
                String onlineResource;
115
                if (status == null || status.getOnlineResource() == null)
116
                        onlineResource = getHost();
117
                else 
118
                        onlineResource = status.getOnlineResource();
119
                symbol = getSymbol(onlineResource);
120
                
121
                req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WFS&");
122
                req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML");
123
                return req.toString();
124
    }   
125
    
126
    /**
127
     * Builds the GetCapabilitiesRequest according to the OGC WFS Specifications
128
     * without a VERSION, to get the highest version than a WFS supports.
129
     */
130
    public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version)
131
    {
132
                String req = new String();                
133
        String symbol = getSymbol(_host);
134
        req = req + _host + symbol + "REQUEST=GetCapabilities&SERVICE=WFS&";                
135
        if((_version != null) && (_version.length()>0 ))
136
        {
137
                req += ("&VERSION=" + _version);
138
        }
139
                req += ("&EXCEPTIONS=XML");                
140
                
141
                return req;           
142
    }
143
    
144
        /**
145
     * <p>Builds a describeFeatureType request that is sent to the WFS
146
     * the response will be parse to extract the data needed by the
147
     * WFS client</p>
148
     * @param status
149
         * WFS client status. Contains all the information to create
150
         * the query. In this case, the only the feature name is needed.
151
     */
152
    public void describeFeatureType(WFSStatus status) {
153
        URL request = null;
154
        this.currentFeature = status.getFeatureName();
155
        try {
156
            request = new URL(buildDescribeFeatureTypeRequest(status));
157
        }
158
        catch(Exception e) {
159
            e.printStackTrace();
160
        }
161
        try {
162
                File f = Utilities.downloadFile(request, "wfs_describeFeatureType.xml", null);
163
                parseDescribeFeatureType(f);
164
                
165
        } catch(Exception e) {
166
                e.printStackTrace();
167
        }
168
    }     
169
    
170
    private String buildDescribeFeatureTypeRequest(WFSStatus status) {
171
            StringBuffer req = new StringBuffer();
172
                String symbol = null;
173
                
174
                String onlineResource;
175
                if (status == null || status.getOnlineResource() == null)
176
                        onlineResource = getHost();
177
                else 
178
                        onlineResource = status.getOnlineResource();
179
                symbol = getSymbol(onlineResource);
180
                
181
                req.append(onlineResource).append(symbol).append("REQUEST=DescribeFeatureType&SERVICE=WFS&");
182
                req.append("TYPENAME=").append(status.getFeatureName()).append("&");
183
                req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML");
184
                return req.toString();
185
    }
186
    
187
    /**
188
     * parses the data retrieved by the DescribeCoverage XML document
189
     */
190
    public abstract boolean parseDescribeFeatureType(File f);
191
    
192
    /**
193
     * <p>Builds a getFeature request that is sent to the WFS
194
     * the response will be parse to extract the data needed by the
195
     * WFS client</p>
196
     * @param status
197
         * WFS client status. Contains all the information to create
198
         * the query. 
199
         * @return File
200
         * GML file
201
     */    
202
    public File getFeature(WFSStatus status){
203
            URL request = null;
204
        try {
205
            request = new URL(buildGetFeatureRequest(status));
206
        }
207
        catch(Exception e) {
208
            e.printStackTrace();
209
        }
210
        try {
211
                File f = Utilities.downloadFile(request, "wfs_getFeature.xml", null);
212
                return f;
213
                
214
        } catch(Exception e) {
215
                e.printStackTrace();
216
                return null;
217
        }
218
    }
219
    
220
        /**
221
     * <p>Builds a getFeature request that is sent to the WFS
222
     * the response will be parse to extract the data needed by the
223
     * WFS client</p>
224
     * @param status
225
         * WFS client status. Contains all the information to create
226
         * the query. 
227
     */
228
    private String buildGetFeatureRequest(WFSStatus status){
229
            StringBuffer req = new StringBuffer();
230
                String symbol = null;
231
                
232
                String onlineResource;
233
                if (status == null || status.getOnlineResource() == null)
234
                        onlineResource = getHost();
235
                else 
236
                        onlineResource = status.getOnlineResource();
237
                symbol = getSymbol(onlineResource);
238
                
239
                req.append(onlineResource).append(symbol).append("REQUEST=GetFeature&SERVICE=WFS&");
240
                req.append("TYPENAME=").append(status.getFeatureName()).append("&");
241
                String[] fields = status.getFields();
242
                if (fields.length > 0){
243
                        req.append("PROPERTYNAME=");
244
                        req.append(fields[0]);
245
                        for (int i=1 ; i<fields.length ; i++){
246
                                req.append("," + fields[i]);
247
                        }                
248
                }                
249
                req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML");
250
                return req.toString();
251
            
252

    
253
    }    
254
    
255
    /**
256
     * Just for not repeat code. Gets the correct separator according 
257
     * to the server URL
258
     * @param h
259
     * @return
260
     */
261
    private static String getSymbol(String h) {
262
        String symbol;
263
        if (h.indexOf("?")==-1) 
264
            symbol = "?";
265
        else if (h.indexOf("?")!=h.length()-1)
266
            symbol = "&";
267
        else
268
            symbol = "";
269
        return symbol;
270
    }  
271
    
272
    /**
273
     * Returns the service Information
274
     * @return
275
     */
276
    public ServiceInformation getServiceInformation(){
277
            return serviceInfo;
278
    }
279
    
280
    public class ServiceInformation {
281

    
282
        public String online_resource = null;
283
        public String version;
284
        public String name;
285
        public String scope;
286
        public String title;
287
        public String abstr;
288
        public String keywords;
289
        public String fees;
290
        public String operationsInfo;
291
        public String personname;
292
        public String organization;
293
        public String function;
294
        public String addresstype;
295
        public String address;
296
        public String place;
297
        public String province;
298
        public String postcode;
299
        public String country;
300
        public String phone;
301
        public String fax;
302
        public String email;
303
        public Vector formats;
304
        public HashMap operations; // operations that WFS supports
305
        
306
        public ServiceInformation()
307
        {          
308
            version = new String();
309
            name = new String();
310
            scope = new String();
311
            title = new String();
312
            abstr = new String();
313
            keywords = new String();
314
            fees = new String();
315
            operationsInfo = new String();
316
            personname = new String();
317
            organization = new String();
318
            function = new String();
319
            addresstype = new String();
320
            address = new String();
321
            place = new String();
322
            province = new String();
323
            postcode = new String();
324
            country = new String();
325
            phone = new String();
326
            fax = new String();
327
            email = new String();
328
            formats = new Vector();               
329
            operations = new HashMap();            
330
        }
331

    
332
                /**
333
                 * @return Returns the online_resource.
334
                 */
335
                public String getOnline_resource() {
336
                        return online_resource;
337
                }
338

    
339
     }
340

    
341
        /**
342
         * @return Returns the features.
343
         */
344
        public Hashtable getFeatures() {
345
                return features;
346
        }
347

    
348
        /**
349
         * @return Returns the currentFeature.
350
         */
351
        public String getCurrentFeature() {
352
                return currentFeature;
353
        }
354
        
355
        /**
356
         * Sets the fields of the current feature
357
         * @param fields
358
         */
359
        public void setFields(Vector fields){
360
                WFSFeature feature = (WFSFeature) features.get(currentFeature);
361
                feature.setFields(fields);
362
                features.put(feature.getName(),feature);                
363
        }
364
        
365
        /**
366
         * Sets the fields of the current feature
367
         * @param fields
368
         */
369
        public void setFields(Hashtable fields){
370
                WFSFeature feature = (WFSFeature) features.get(currentFeature);
371
                Vector vFields = new Vector();
372
                Set keys = fields.keySet();
373
                for (int i=0 ; i<keys.size() ; i++){
374
                        vFields.add(fields.get(keys.toArray()[i]));
375
                }
376
                feature.setFields(vFields);
377
                features.put(feature.getName(),feature);                
378
        }
379
        
380

    
381
        /**
382
         * @param currentFeature The currentFeature to set.
383
         */
384
        public void setCurrentFeature(String currentFeature) {
385
                this.currentFeature = currentFeature;
386
        }   
387
    
388
    
389

    
390
}