Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_909 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / WFSProtocolHandlerFactory.java @ 11150

History | View | Annotate | Download (7.92 KB)

1
package org.gvsig.remoteClient.wfs;
2

    
3
import java.io.DataInputStream;
4
import java.io.IOException;
5
import java.io.StringReader;
6
import java.net.ConnectException;
7
import java.net.URL;
8
import java.util.ArrayList;
9
import java.util.Iterator;
10

    
11
import org.gvsig.remoteClient.utils.CapabilitiesTags;
12
import org.kxml2.io.KXmlParser;
13
import org.xmlpull.v1.XmlPullParserException;
14

    
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: WFSProtocolHandlerFactory.java 11150 2007-04-12 08:35:23Z  $
58
 * $Log$
59
 * Revision 1.1  2006-04-19 12:51:35  jorpiell
60
 * A?adidas algunas de las clases del servicio WFS
61
 *
62
 *
63
 */
64
/**
65
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
66
 */
67
public class WFSProtocolHandlerFactory {
68
        public WFSProtocolHandler wFSProtocolHandler;
69
        
70
        private static ArrayList supportedVersions = new ArrayList();
71
        
72
        static {
73
                supportedVersions.add("1.0.0");
74
        }
75
        
76
        /**
77
         * M?todo que dada una respuesta de getCapabilities y un iterador sobre una
78
         * coleccion de WFSClient's ordenada descendentemente devuelve el cliente
79
         * cuya version es igual o inmediatamente inferior
80
         *
81
         * @param caps Capabilities con la respuesta del servidor
82
         * @param clients Iterador de conjunto ordenado descendientemente
83
         *
84
         * @return cliente cuya version es igual o inmediatamente inferior
85
         * @throws IllegalAccessException
86
         * @throws InstantiationException
87
         * 
88
         */
89
        private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException {
90
                while (clients.hasNext()) {
91
                        String clientVersion = (String)clients.next();
92
                        int ret = version.compareTo(clientVersion);
93
                        
94
                        if (ret >= 0) {
95
                                return clientVersion;
96
                        }
97
                }
98
                return null;
99
        }
100
        
101
        /**
102
         * Establece la versi?n con la que se comunicar? con el servidor y devuelve
103
         * el objeto Capabilities obtenido con dicha versi?n
104
         *
105
         * @param host maquina con la que se negocia
106
         *
107
         * @return instancia de un cliente capaz de negociar con el host que se
108
         *         pasa como par?metro
109
         */
110
        public static WFSProtocolHandler negotiate(String host) throws ConnectException, IOException {
111
                
112
                if (supportedVersions.size() == 0){
113
                        return null;
114
                }
115
                
116
                try        {                
117
                        String highestVersionSupportedByServer  = getSuitableWFSVersion(host,"");
118
                        if (supportedVersions.contains(highestVersionSupportedByServer))
119
                        {
120
                                // we support the highest version supported by the server
121
                                // this is the best case 
122
                                return createVersionDriver(highestVersionSupportedByServer);
123
                        }
124
                        else
125
                        {
126
                                // in case we dont support the highest version from the server
127
                                // we start the negotiation process in which we have to get the higest version
128
                                // the WFS supports and we are able to read.
129
                                Iterator iVersion = supportedVersions.iterator();
130
                                String wfsVersion;
131
                                String gvSIGVersion;
132
                                
133
                                while (iVersion.hasNext()) { 
134
                                        gvSIGVersion = (String)iVersion.next();
135
                                        wfsVersion = getSuitableWFSVersion(host,gvSIGVersion);
136
                                        //TODO:
137
                                        //compare with the version returned by the WMS!!!!!
138
                                        // send GetCapabilities and read the version to compare.
139
                                        int res = wfsVersion.compareTo(gvSIGVersion);
140
                                        
141
                                        if (res == 0) { //Si es la misma que nuestra version                
142
                                                return createVersionDriver(gvSIGVersion);
143
                                        } else if (res > 0) { //Si es mayor que nuestra version
144
                                                throw new Exception("Server Version too high: " + wfsVersion);
145
                                        } else { //Si es menor que nuestra version
146
                                                //Obtenemos la primera version menor o igual que tengamos
147
                                                String lowerVersion = WFSProtocolHandlerFactory.getDriverVersion(wfsVersion, iVersion);
148
                                                
149
                                                if (lowerVersion == null) { //Si no hay ninguna
150
                                                        throw new Exception("Lowest server version is " + wfsVersion);
151
                                                } else {
152
                                                        if (lowerVersion.equals(wfsVersion)) { 
153
                                                                return createVersionDriver(lowerVersion);
154
                                                        } else { //Si hay una version menor que la que retorno el servidor
155
                                                                //iV = lower;
156
                                                        }
157
                                                }
158
                                        }
159
                                }
160
                        }//case we had to start the negotiation process.
161
                        return null; // if it did not find any suitable version.        
162
                }
163
                catch(ConnectException conEx)
164
                {
165
                        throw conEx;
166
                }
167
                catch(IOException ioEx)
168
                {
169
                        throw ioEx;
170
                }        
171
                catch(Exception e)
172
                {
173
                        e.printStackTrace();
174
                        return null;
175
                }
176
        }
177
        
178
        /**
179
         * Sends a GetCapabilities to the WFS server to get the version
180
         * if the version parameter is null, the WFS will return the highest version supported
181
         * if not it will return the lower highest version than the one requested.
182
         * @param host
183
         * @param version
184
         * @return suitable version supported by the server 
185
         */
186
        private static String getSuitableWFSVersion(String host, String _version) throws ConnectException, IOException {             
187
                String request = WFSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
188
                String version = new String(); 
189
                StringReader reader = null;
190
                //InputStreamReader reader;
191
                //InputStream is = null;
192
                DataInputStream dis = null;
193
                try {           
194
                        URL url = new URL(request);
195
                        byte[] buffer = new byte[1024];//new byte[1024*256];
196
//                        is = url.openStream();            
197
//                        reader = new InputStreamReader(is);            
198
                        //int numberOfBytes = is.read(buffer);            
199
                        //String readed = new String(buffer);
200
                        dis = new DataInputStream(url.openStream()); 
201
                        dis.readFully(buffer);
202
                        
203
                        reader = new StringReader(new String(buffer));
204
                        KXmlParser kxmlParser = null;
205
                        kxmlParser = new KXmlParser();
206
                        kxmlParser.setInput(reader);                
207
                        kxmlParser.nextTag();
208
                        if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {                    
209
                                if ((kxmlParser.getName().compareTo(CapabilitiesTags.WFS_CAPABILITIES_ROOT1_0_0)==0)) {
210
                                        version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
211
                                }
212
                        }
213
                        // do not forget to close the Stream.                    
214
                        reader.close();                    
215
                        dis.close();
216
                        return version;                
217
                } catch(ConnectException conEx) {
218
                        throw new ConnectException(conEx.getMessage());                         
219
                } catch(IOException ioEx) {
220
                        throw new IOException(ioEx.getMessage());         
221
                } catch(XmlPullParserException xmlEx) {
222
                        xmlEx.printStackTrace();
223
                        return "";
224
                } finally {                
225
                        if (reader != null) {
226
                                try {
227
                                        reader.close();
228
                                } catch(Exception ex) {
229
                                        ex.printStackTrace(); 
230
                                }
231
                        } if (dis != null) {
232
                                try {
233
                                        dis.close();
234
                                } catch(Exception ex){
235
                                        ex.printStackTrace(); 
236
                                }
237
                        }
238
                } 
239
        }
240
        
241
        /**
242
         * It creates an instance of a WFSDriver class.
243
         *
244
         * @param String, with the version of the driver to be created
245
         * @return WFSDriver.
246
         */
247
        private static WFSProtocolHandler createVersionDriver(String version) {               
248
                try {
249
                        Class driver;
250
                        version = version.replace('.', '_');
251
                        driver = Class.forName("org.gvsig.remoteClient.wfs.wfs_"+version+".WFSProtocolHandler" + version);
252
                        return (WFSProtocolHandler)driver.newInstance();
253
                } catch (Exception e) {
254
                        e.printStackTrace();
255
                        //throw new Exception("WFSDriverFactory. Unknown driver version " + e);
256
                        return null;
257
                }
258
        }    
259
        
260
        
261
        
262
}