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 @ 41301

History | View | Annotate | Download (10.2 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
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
/**
40
 * Represents the class the with the necessary logic to connect to a 
41
 * OGCWFS and interpretate the data 
42
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
43
 */
44
public class WFSClient extends RemoteClient{
45
    
46
        private static Logger logger = LoggerFactory.getLogger(WFSClient.class);
47
        
48
        private WFSProtocolHandler handler;
49

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

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