Statistics
| Revision:

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

History | View | Annotate | Download (7.38 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.wcs;
25

    
26
import java.io.DataInputStream;
27
import java.io.IOException;
28
import java.io.StringReader;
29
import java.net.ConnectException;
30
import java.net.URL;
31
import java.util.ArrayList;
32
import java.util.Iterator;
33

    
34
import org.kxml2.io.KXmlParser;
35
import org.xmlpull.v1.XmlPullParserException;
36

    
37
import org.gvsig.remoteclient.utils.CapabilitiesTags;
38

    
39
/**
40
 *
41
 * @author jaume dominguez faus
42
 *
43
 */
44
public class WCSProtocolHandlerFactory {
45

    
46
        public org.gvsig.remoteclient.wcs.WCSProtocolHandler wCSProtocolHandler;
47

    
48
        private static ArrayList supportedVersions = new ArrayList();
49

    
50
        static {
51
                supportedVersions.add("1.0.0");
52
        }
53

    
54
        /**
55
         * M?todo que dada una respuesta de getCapabilities y un iterador sobre una
56
         * coleccion de WCSClient's ordenada descendentemente devuelve el cliente
57
         * cuya version es igual o inmediatamente inferior
58
         *
59
         * @param caps Capabilities con la respuesta del servidor
60
         * @param clients Iterador de conjunto ordenado descendientemente
61
         *
62
         * @return cliente cuya version es igual o inmediatamente inferior
63
         * @throws IllegalAccessException
64
         * @throws InstantiationException
65
         *
66
         */
67
        private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException {
68
                while (clients.hasNext()) {
69
                        String clientVersion = (String)clients.next();
70
                        int ret = version.compareTo(clientVersion);
71

    
72
                        if (ret >= 0) {
73
                                return clientVersion;
74
                        }
75
                }
76
                return null;
77
        }
78

    
79
        /**
80
         * Establece la versi?n con la que se comunicar? con el servidor y devuelve
81
         * el objeto Capabilities obtenido con dicha versi?n
82
         *
83
         * @param host maquina con la que se negocia
84
         *
85
         * @return instancia de un cliente capaz de negociar con el host que se
86
         *         pasa como par?metro
87
         */
88
        public static WCSProtocolHandler negotiate(String host) throws ConnectException, IOException {
89

    
90
                if (supportedVersions.size() == 0)
91
                {
92
                        return null;
93
                }
94

    
95
                try
96
                {
97
                        String highestVersionSupportedByServer  = getSuitableWCSVersion(host,"");
98
                        if (supportedVersions.contains(highestVersionSupportedByServer))
99
                        {
100
                                // we support the highest version supported by the server
101
                                // this is the best case
102
                                return createVersionDriver(highestVersionSupportedByServer);
103
                        }
104
                        else
105
                        {
106
                                // in case we dont support the highest version from the server
107
                                // we start the negotiation process in which we have to get the higest version
108
                                // the WCS supports and we are able to read.
109
                                Iterator iVersion = supportedVersions.iterator();
110
                                String wcsVersion;
111
                                String gvSIGVersion;
112

    
113
                                while (iVersion.hasNext()) {
114
                                        gvSIGVersion = (String)iVersion.next();
115
                                        wcsVersion = getSuitableWCSVersion(host,gvSIGVersion);
116
                                        //TODO:
117
                                        //compare with the version returned by the WMS!!!!!
118
                                        // send GetCapabilities and read the version to compare.
119
                                        int res = wcsVersion.compareTo(gvSIGVersion);
120

    
121
                                        if (res == 0) { //Si es la misma que nuestra version
122
                                                return createVersionDriver(gvSIGVersion);
123
                                        } else if (res > 0) { //Si es mayor que nuestra version
124
                                                throw new Exception("Server Version too high: " + wcsVersion);
125
                                        } else { //Si es menor que nuestra version
126
                                                //Obtenemos la primera version menor o igual que tengamos
127
                                                String lowerVersion = WCSProtocolHandlerFactory.getDriverVersion(wcsVersion, iVersion);
128

    
129
                                                if (lowerVersion == null) { //Si no hay ninguna
130
                                                        throw new Exception("Lowest server version is " + wcsVersion);
131
                                                } else {
132
                                                        if (lowerVersion.equals(wcsVersion)) {
133
                                                                return createVersionDriver(lowerVersion);
134
                                                        } else { //Si hay una version menor que la que retorno el servidor
135
                                                                //iV = lower;
136
                                                        }
137
                                                }
138
                                        }
139
                                }
140
                        }//case we had to start the negotiation process.
141
                        return null; // if it did not find any suitable version.
142
                }
143
                catch(ConnectException conEx)
144
                {
145
                        throw conEx;
146
                }
147
                catch(IOException ioEx)
148
                {
149
                        throw ioEx;
150
                }
151
                catch(Exception e)
152
                {
153
//                        e.printStackTrace();
154
                        return null;
155
                }
156
        }
157

    
158
        /**
159
         * Sends a GetCapabilities to the WCS server to get the version
160
         * if the version parameter is null, the WCS will return the highest version supported
161
         * if not it will return the lower highest version than the one requested.
162
         * @param host
163
         * @param version
164
         * @return suitable version supported by the server
165
         */
166
        private static String getSuitableWCSVersion(String host, String _version) throws ConnectException, IOException {
167
                String request = WCSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
168
                String version = new String();
169
                StringReader reader = null;
170
                //InputStreamReader reader;
171
                //InputStream is = null;
172
                DataInputStream dis = null;
173
                try {
174
                        URL url = new URL(request);
175
                        byte[] buffer = new byte[1024];//new byte[1024*256];
176
//                        is = url.openStream();
177
//                        reader = new InputStreamReader(is);
178
                        //int numberOfBytes = is.read(buffer);
179
                        //String readed = new String(buffer);
180
                        dis = new DataInputStream(url.openStream());
181
                        dis.readFully(buffer);
182

    
183
                        reader = new StringReader(new String(buffer));
184
                        KXmlParser kxmlParser = null;
185
                        kxmlParser = new KXmlParser();
186
                        kxmlParser.setInput(reader);
187
                        kxmlParser.nextTag();
188
                        if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
189
                                if ((kxmlParser.getName().compareTo(CapabilitiesTags.WCS_CAPABILITIES_ROOT1_0_0)==0)) {
190
                                        version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
191
                                }
192
                        }
193
                        // do not forget to close the Stream.
194
                        reader.close();
195
                        dis.close();
196
                        return version;
197
                } catch(ConnectException conEx) {
198
                        throw new ConnectException(conEx.getMessage());
199
                } catch(IOException ioEx) {
200
                        throw new IOException(ioEx.getMessage());
201
                } catch(XmlPullParserException xmlEx) {
202
                        xmlEx.printStackTrace();
203
                        return "";
204
                } finally {
205
                        if (reader != null) {
206
                                try {
207
                                        reader.close();
208
                                } catch(Exception ex) {
209
                                        ex.printStackTrace();
210
                                }
211
                        } if (dis != null) {
212
                                try {
213
                                        dis.close();
214
                                } catch(Exception ex){
215
                                        ex.printStackTrace();
216
                                }
217
                        }
218
                }
219
        }
220

    
221
        /**
222
         * It creates an instance of a WCSDriver class.
223
         *
224
         * @param String, with the version of the driver to be created
225
         * @return WCSDriver.
226
         */
227
        private static WCSProtocolHandler createVersionDriver(String version) {
228
                try {
229
                        Class driver;
230
                        version = version.replace('.', '_');
231
                        driver = Class.forName("org.gvsig.remoteclient.wcs.wcs_"+version+".WCSProtocolHandler" + version);
232
                        return (WCSProtocolHandler)driver.newInstance();
233
                } catch (Exception e) {
234
                        e.printStackTrace();
235
                        //throw new Exception("WCSDriverFactory. Unknown driver version " + e);
236
                        return null;
237
                }
238
        }
239

    
240

    
241
}