Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / WFSClient.java @ 40769

History | View | Annotate | Download (9.68 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.wfs;
25

    
26
import java.io.File;
27
import java.io.IOException;
28
import java.net.ConnectException;
29
import java.util.ArrayList;
30
import java.util.Hashtable;
31
import java.util.Iterator;
32

    
33
import org.gvsig.compat.net.ICancellable;
34
import org.gvsig.remoteclient.RemoteClient;
35
import org.gvsig.remoteclient.wfs.exceptions.WFSException;
36

    
37
/**
38
 * Represents the class the with the necessary logic to connect to a 
39
 * OGCWFS and interpretate the data 
40
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
41
 */
42
public class WFSClient extends RemoteClient{
43
    
44
        private WFSProtocolHandler handler;
45

    
46
        //private Logger LOG = LoggerFactory.getLogger(WFSClient.class);
47
        
48
        /**
49
         * Constructor.
50
         * the parameter host, indicates the WFS host to connect.
51
         * @throws ConnectException,IOException 
52
         *
53
         */
54
        public WFSClient(String host) throws ConnectException,IOException {
55
                setHost(host);
56
                
57
                handler = WFSProtocolHandlerFactory.negotiate(host);
58
                handler.setHost(host);  
59
        }
60
        
61
        public WFSClient(String host, String version) throws IOException {
62
            
63
                setHost(host);
64

    
65
                if (version == null) {
66
                        try {                
67
                                handler = WFSProtocolHandlerFactory.negotiate(host);                                  
68
                        } catch(ConnectException conE) {
69
                                conE.printStackTrace();
70
                                throw conE; 
71
                        } catch(IOException ioE) {
72
                                ioE.printStackTrace();
73
                                throw ioE; 
74
                        } catch(Exception e) {
75
                                e.printStackTrace();               
76
                        } 
77
                }else{
78
                        handler = WFSProtocolHandlerFactory.createVersionDriver(version);
79
                }
80
                
81
                if (handler == null){
82
                        throw new UnsupportedOperationException("Unsupported version");
83
                }
84
                handler.setHost(host);                        
85
        }
86
        
87
        /**
88
         * Every OGC Web Service (OWS), including a Web Feature Service,
89
         * must have the ability to describe its capabilities by returning
90
         * service metadata in response to a GetCapabilities request.
91
         * @param status
92
         * WFS client status. Contains all the information to create
93
         * the query
94
         */
95
        public void getCapabilities(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
96
                handler.getCapabilities(status,override,cancel);                
97
        }
98
        
99
        /**
100
         * The function of the DescribeFeatureType operation is to 
101
         * generate a schema description of feature types serviced 
102
         * by a WFS implementation. The schema descriptions define 
103
         * how a WFS implementation expects feature instances to 
104
         * be encoded on input (via Insert and Update requests) 
105
         * and how feature instances will be generated on output 
106
         * (in response to GetFeature and GetGmlObject requests). 
107
         * @param status
108
         * WFS client status. Contains all the information to create
109
         * the query
110
         */
111
        public File describeFeatureType(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
112
                return handler.describeFeatureType(status,override,cancel);                
113
        }
114
        
115
        /**
116
         * The GetFeature operation allows retrieval of features from a 
117
         * web feature service. A GetFeature request is processed by
118
         * a WFS and when the value of the outputFormat attribute is 
119
         * set to text/gml a GML instance document, containing the 
120
         * result set, is returned to the client.
121
         * @param status
122
         * WFS client status. Contains all the information to create
123
         * the query
124
         * @return File
125
         * GML File
126
         */
127
        public File getFeature(WFSStatus status, boolean override, ICancellable cancel) throws WFSException{
128
            if ((status.getNamespaceLocation() != null) && (status.getNamespacePrefix() == null)){
129
                String namespacePrefix = getServiceInformation().getNamespacePrefix(status.getNamespaceLocation());
130
                if (namespacePrefix != null){
131
                    status.setNamespacePrefix(namespacePrefix);
132
                }
133
        }
134
            return handler.getFeature(status,override,cancel);
135
        }
136
        
137
        /**
138
         * The Transaction operation is used to describe data transformation 
139
         * operations that are to be applied to web accessible feature 
140
         * instances. A web feature service may process a Transaction 
141
         * operation directly or possibly translate it into the language 
142
         * of a target datastore to which it is connected and then have the
143
         * datastore execute the transaction. When the transaction has been 
144
         * completed, a web feature service will generate an XML response 
145
         * document indicating the completion status of the transaction.
146
         * @param status
147
         * WFS client status. Contains all the information to create
148
         * the query
149
         */
150
        public void transaction(WFSStatus status, boolean override, ICancellable cancel) throws WFSException{
151
                handler.transaction(status,override,cancel);
152
        }
153
        
154
        /**
155
         * Web connections are inherently stateless. As a consequence 
156
         * of this, the semantics of serializable transactions are not 
157
         * preserved. To understand the issue, consider an update operation.
158
         * The client fetches a feature instance. The feature is then 
159
         * modified on the client side, and submitted back to the database 
160
         * via a Transaction request for update. Serializability is lost 
161
         * since there is nothing to guarantee that while the feature was 
162
         * being modified on the client side, another client did not come 
163
         * along and update that same feature in the database.
164
         * One way to ensure serializability is to require that access to
165
         * data be done in a mutually exclusive manner; that is while one 
166
         * transaction accesses a data item, no other transaction can modify 
167
         * the same data item. This can be accomplished by using locks that 
168
         * control access to the data.
169
         * The purpose of the LockFeature operation is to expose a long 
170
         * term feature locking mechanism to ensure consistency. The lock
171
         * is considered long term because network latency would make 
172
         * feature locks last relatively longer than native commercial 
173
         * database locks.
174
         * The LockFeature operation is optional and does not need to be 
175
         * implemented for a WFS implementation to conform to this 
176
         * specification. If a WFS implements the LockFeature operation, 
177
         * this fact must be advertised in the capabilities document
178
         * @param status
179
         */
180
        public void lockFeature(WFSStatus status, boolean override, ICancellable cancel)throws WFSException {
181
                handler.lockFeature(status,override,cancel);
182
        }
183
        
184
        
185
        /**
186
         * <p>Checks the connection to de remote WFS and requests its 
187
         * capabilities.</p>
188
         * 
189
         */
190
        public boolean connect(boolean override, ICancellable cancel) 
191
        {
192
                try {
193
                        if (handler == null) {
194
                                if (getHost().trim().length() > 0) {                   
195
                                        handler = WFSProtocolHandlerFactory.negotiate(getHost());
196
                                        handler.setHost(getHost());
197
                                } else {
198
                                        // must to specify host first!!!!
199
                                        return false;
200
                                }
201
                        }
202
                        getCapabilities(null,override,cancel);                        
203
                        
204
                        return true;
205
                        
206
                } catch (Exception e) {
207
                        e.printStackTrace();
208
                        return false;
209
                }
210
        }
211
        
212
        /*
213
         *  (non-Javadoc)
214
         * @see org.gvsig.remoteClient.RemoteClient#connect(org.gvsig.remoteClient.wms.ICancellable)
215
         */
216
        public boolean connect(ICancellable cancel) {
217
                return connect(false, cancel);
218
        }
219
        
220
        /*
221
         *  (non-Javadoc)
222
         * @see org.gvsig.remoteClient.RemoteClient#close()
223
         */
224
        public void close() {
225
                // TODO Auto-generated method stub
226
                
227
        }
228
        
229
         public String getVersion()
230
         {
231
                 return handler.getVersion();
232
         }         
233
         
234
         /**
235
     * Gets the Service information included in the Capabilities
236
     */
237
    
238
    public WFSServiceInformation getServiceInformation(){
239
        return (WFSServiceInformation)handler.getServiceInformation();  
240
    }
241
    
242
    /**
243
     * Returns the features list
244
     * @return
245
     */
246
    public Hashtable getFeatures()
247
    {
248
        return handler.getFeatures();  
249
    }
250
    
251
    public WFSFeature getFeature(String nameSpace, String localName){
252
        Iterator it = handler.getFeatures().keySet().iterator();
253
        String fullName = localName;
254
        String prefix = getServiceInformation().getNamespacePrefix(nameSpace);
255
        if (prefix != null){
256
            fullName = prefix + ":" + localName;
257
        }
258
        while (it.hasNext()){
259
            String featureName = (String)it.next();
260
            if (localName.equals(featureName)){
261
                return (WFSFeature)handler.getFeatures().get(localName);
262
            }
263
            if (fullName.equals(featureName)){
264
                return (WFSFeature)handler.getFeatures().get(fullName);
265
            }
266
        }
267
        return null;
268
    }
269
    
270
        /**
271
         * @return the lastWfsRequestInformation
272
         */
273
        public WFSRequestInformation getLastWfsRequestInformation() {
274
                return handler.getLastWfsRequestInformation();
275
        }
276
        
277
        /**
278
         * @return the number requests
279
         */
280
        public int getWFSRequestInformationCount(){
281
                return handler.getWfsRequestInformations().size();
282
        }
283
        
284
        /**
285
         * Return the information of a request
286
         * @param index
287
         * The position of the request
288
         * @return
289
         */
290
        public WFSRequestInformation getWFSRequestInformationAt(int index){
291
                ArrayList wfsRequestInformations = handler.getWfsRequestInformations();
292
                if (index < wfsRequestInformations.size()){
293
                        return (WFSRequestInformation)wfsRequestInformations.get(index);
294
                }
295
                return null;
296
        }
297
        
298
}