Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wfs / WFSClient.java @ 34026

History | View | Annotate | Download (10.9 KB)

1
package org.gvsig.remoteclient.wfs;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.net.ConnectException;
6
import java.util.ArrayList;
7
import java.util.Hashtable;
8
import java.util.Iterator;
9

    
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

    
13
import org.gvsig.compat.net.ICancellable;
14
import org.gvsig.remoteclient.RemoteClient;
15
import org.gvsig.remoteclient.wfs.exceptions.WFSException;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57
/* CVS MESSAGES:
58
 *
59
 * $Id: WFSClient.java 34026 2010-11-08 13:06:02Z jpiera $
60
 * $Log$
61
 * Revision 1.10  2007-09-20 09:30:12  jaume
62
 * removed unnecessary imports
63
 *
64
 * Revision 1.9  2007/02/09 14:11:01  jorpiell
65
 * Primer piloto del soporte para WFS 1.1 y para WFS-T
66
 *
67
 * Revision 1.8  2006/06/14 08:46:07  jorpiell
68
 * Se tiene en cuanta la opcion para refrescar las capabilities
69
 *
70
 * Revision 1.7  2006/05/25 10:28:25  jorpiell
71
 * Se ha a?adido un atributo al m?todo connect
72
 *
73
 * Revision 1.6  2006/05/23 13:23:22  jorpiell
74
 * Se ha cambiado el final del bucle de parseado y se tiene en cuenta el online resource
75
 *
76
 * Revision 1.4  2006/04/20 16:39:16  jorpiell
77
 * A?adida la operacion de describeFeatureType y el parser correspondiente.
78
 *
79
 * Revision 1.3  2006/04/19 12:51:35  jorpiell
80
 * A?adidas algunas de las clases del servicio WFS
81
 *
82
 *
83
 */
84
/**
85
 * Represents the class the with the necessary logic to connect to a 
86
 * OGCWFS and interpretate the data 
87
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
88
 */
89
public class WFSClient extends RemoteClient{
90
        private WFSProtocolHandler handler;
91
        private Logger LOG = LoggerFactory.getLogger(WFSClient.class);
92
        
93
        /**
94
         * Constructor.
95
         * the parameter host, indicates the WFS host to connect.
96
         * @throws ConnectException,IOException 
97
         *
98
         */
99
        public WFSClient(String host) throws ConnectException,IOException {
100
                setHost(host);
101
                
102
                try {                
103
                        handler = WFSProtocolHandlerFactory.negotiate(host);
104
                        handler.setHost(host);        
105
                } catch(Exception e) {
106
                    LOG.error("Error connecting with the server", e);
107
                    throw new ConnectException("Error connecting with the server");           
108
                } 
109
        }
110
        
111
        public WFSClient(String host, String version) throws IOException {
112
                setHost(host);
113
                
114
                if (version == null){
115
                        try {                
116
                                handler = WFSProtocolHandlerFactory.negotiate(host);                                  
117
                        } catch(ConnectException conE) {
118
                                conE.printStackTrace();
119
                                throw conE; 
120
                        } catch(IOException ioE) {
121
                                ioE.printStackTrace();
122
                                throw ioE; 
123
                        } catch(Exception e) {
124
                                e.printStackTrace();               
125
                        } 
126
                }else{
127
                        handler = WFSProtocolHandlerFactory.createVersionDriver(version);
128
                }
129
                
130
                if (handler == null){
131
                        throw new UnsupportedOperationException("Unsupported version");
132
                }
133
                handler.setHost(host);                        
134
        }
135
        
136
        /**
137
         * Every OGC Web Service (OWS), including a Web Feature Service,
138
         * must have the ability to describe its capabilities by returning
139
         * service metadata in response to a GetCapabilities request.
140
         * @param status
141
         * WFS client status. Contains all the information to create
142
         * the query
143
         */
144
        public void getCapabilities(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
145
                handler.getCapabilities(status,override,cancel);                
146
        }
147
        
148
        /**
149
         * The function of the DescribeFeatureType operation is to 
150
         * generate a schema description of feature types serviced 
151
         * by a WFS implementation. The schema descriptions define 
152
         * how a WFS implementation expects feature instances to 
153
         * be encoded on input (via Insert and Update requests) 
154
         * and how feature instances will be generated on output 
155
         * (in response to GetFeature and GetGmlObject requests). 
156
         * @param status
157
         * WFS client status. Contains all the information to create
158
         * the query
159
         */
160
        public File describeFeatureType(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
161
                return handler.describeFeatureType(status,override,cancel);                
162
        }
163
        
164
        /**
165
         * The GetFeature operation allows retrieval of features from a 
166
         * web feature service. A GetFeature request is processed by
167
         * a WFS and when the value of the outputFormat attribute is 
168
         * set to text/gml a GML instance document, containing the 
169
         * result set, is returned to the client.
170
         * @param status
171
         * WFS client status. Contains all the information to create
172
         * the query
173
         * @return File
174
         * GML File
175
         */
176
        public File getFeature(WFSStatus status, boolean override, ICancellable cancel) throws WFSException{
177
            if ((status.getNamespaceLocation() != null) && (status.getNamespacePrefix() == null)){
178
                String namespacePrefix = getServiceInformation().getNamespacePrefix(status.getNamespaceLocation());
179
                if (namespacePrefix != null){
180
                    status.setNamespacePrefix(namespacePrefix);
181
                }
182
        }
183
            return handler.getFeature(status,override,cancel);
184
        }
185
        
186
        /**
187
         * The Transaction operation is used to describe data transformation 
188
         * operations that are to be applied to web accessible feature 
189
         * instances. A web feature service may process a Transaction 
190
         * operation directly or possibly translate it into the language 
191
         * of a target datastore to which it is connected and then have the
192
         * datastore execute the transaction. When the transaction has been 
193
         * completed, a web feature service will generate an XML response 
194
         * document indicating the completion status of the transaction.
195
         * @param status
196
         * WFS client status. Contains all the information to create
197
         * the query
198
         */
199
        public void transaction(WFSStatus status, boolean override, ICancellable cancel) throws WFSException{
200
                handler.transaction(status,override,cancel);
201
        }
202
        
203
        /**
204
         * Web connections are inherently stateless. As a consequence 
205
         * of this, the semantics of serializable transactions are not 
206
         * preserved. To understand the issue, consider an update operation.
207
         * The client fetches a feature instance. The feature is then 
208
         * modified on the client side, and submitted back to the database 
209
         * via a Transaction request for update. Serializability is lost 
210
         * since there is nothing to guarantee that while the feature was 
211
         * being modified on the client side, another client did not come 
212
         * along and update that same feature in the database.
213
         * One way to ensure serializability is to require that access to
214
         * data be done in a mutually exclusive manner; that is while one 
215
         * transaction accesses a data item, no other transaction can modify 
216
         * the same data item. This can be accomplished by using locks that 
217
         * control access to the data.
218
         * The purpose of the LockFeature operation is to expose a long 
219
         * term feature locking mechanism to ensure consistency. The lock
220
         * is considered long term because network latency would make 
221
         * feature locks last relatively longer than native commercial 
222
         * database locks.
223
         * The LockFeature operation is optional and does not need to be 
224
         * implemented for a WFS implementation to conform to this 
225
         * specification. If a WFS implements the LockFeature operation, 
226
         * this fact must be advertised in the capabilities document
227
         * @param status
228
         */
229
        public void lockFeature(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
230
                handler.lockFeature(status,override,cancel);
231
        }
232
        
233
        
234
        /**
235
         * <p>Checks the connection to de remote WFS and requests its 
236
         * capabilities.</p>
237
         * 
238
         */
239
        public boolean connect(boolean override,ICancellable cancel) 
240
        {
241
                try {
242
                        if (handler == null) {
243
                                if (getHost().trim().length() > 0) {                   
244
                                        handler = WFSProtocolHandlerFactory.negotiate(getHost());
245
                                        handler.setHost(getHost());
246
                                } else {
247
                                        // must to specify host first!!!!
248
                                        return false;
249
                                }
250
                        }
251
                        getCapabilities(null,override,cancel);                        
252
                        
253
                        return true;
254
                        
255
                } catch (Exception e) {
256
                        e.printStackTrace();
257
                        return false;
258
                }
259
        }
260
        
261
        /*
262
         *  (non-Javadoc)
263
         * @see org.gvsig.remoteClient.RemoteClient#connect(org.gvsig.remoteClient.wms.ICancellable)
264
         */
265
        public boolean connect(ICancellable cancel) {
266
                return connect(false, cancel);
267
        }
268
        
269
        /*
270
         *  (non-Javadoc)
271
         * @see org.gvsig.remoteClient.RemoteClient#close()
272
         */
273
        public void close() {
274
                // TODO Auto-generated method stub
275
                
276
        }
277
        
278
         public String getVersion()
279
         {
280
                 return handler.getVersion();
281
         }         
282
         
283
         /**
284
     * Gets the Service information included in the Capabilities
285
     */
286
    
287
    public WFSServiceInformation getServiceInformation(){
288
        return (WFSServiceInformation)handler.getServiceInformation();  
289
    }
290
    
291
    /**
292
     * Returns the features list
293
     * @return
294
     */
295
    public Hashtable getFeatures()
296
    {
297
        return handler.getFeatures();  
298
    }
299
    
300
    public WFSFeature getFeature(String nameSpace, String localName){
301
        Iterator it = handler.getFeatures().keySet().iterator();
302
        String fullName = localName;
303
        String prefix = getServiceInformation().getNamespacePrefix(nameSpace);
304
        if (prefix != null){
305
            fullName = prefix + ":" + localName;
306
        }
307
        while (it.hasNext()){
308
            String featureName = (String)it.next();
309
            if (localName.equals(featureName)){
310
                return (WFSFeature)handler.getFeatures().get(localName);
311
            }
312
            if (fullName.equals(featureName)){
313
                return (WFSFeature)handler.getFeatures().get(fullName);
314
            }
315
        }
316
        return null;
317
    }
318
    
319
        /**
320
         * @return the lastWfsRequestInformation
321
         */
322
        public WFSRequestInformation getLastWfsRequestInformation() {
323
                return handler.getLastWfsRequestInformation();
324
        }
325
        
326
        /**
327
         * @return the number requests
328
         */
329
        public int getWFSRequestInformationCount(){
330
                return handler.getWfsRequestInformations().size();
331
        }
332
        
333
        /**
334
         * Return the information of a request
335
         * @param index
336
         * The position of the request
337
         * @return
338
         */
339
        public WFSRequestInformation getWFSRequestInformationAt(int index){
340
                ArrayList wfsRequestInformations = handler.getWfsRequestInformations();
341
                if (index < wfsRequestInformations.size()){
342
                        return (WFSRequestInformation)wfsRequestInformations.get(index);
343
                }
344
                return null;
345
        }
346
        
347
}