svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wcs / WCSProtocolHandlerFactory.java @ 33910
History | View | Annotate | Download (8.07 KB)
1 | 4293 | jaume | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | /*
|
||
42 | 6422 | jaume | * $Id$
|
43 | 4293 | jaume | * $Log$
|
44 | 6422 | jaume | * Revision 1.4 2006-07-18 11:41:05 jaume
|
45 | * no hay nada nuevo pero el team synchronize est? pesado
|
||
46 | *
|
||
47 | * Revision 1.3 2006/04/25 06:47:50 jaume
|
||
48 | 4926 | jaume | * clean up unnecessary imports
|
49 | *
|
||
50 | * Revision 1.2 2006/03/15 08:54:42 jaume
|
||
51 | 4293 | jaume | * *** empty log message ***
|
52 | 4427 | jaume | *
|
53 | * Revision 1.1.2.1 2006/03/08 09:08:31 jaume
|
||
54 | * *** empty log message ***
|
||
55 | *
|
||
56 | * Revision 1.1 2006/03/06 15:18:32 jaume
|
||
57 | * *** empty log message ***
|
||
58 | 6422 | jaume | *
|
59 | 4293 | jaume | */
|
60 | 29649 | jpiera | package org.gvsig.remoteclient.wcs; |
61 | 4293 | jaume | |
62 | import java.io.DataInputStream; |
||
63 | import java.io.IOException; |
||
64 | import java.io.StringReader; |
||
65 | import java.net.ConnectException; |
||
66 | import java.net.URL; |
||
67 | import java.util.ArrayList; |
||
68 | import java.util.Iterator; |
||
69 | |||
70 | import org.kxml2.io.KXmlParser; |
||
71 | import org.xmlpull.v1.XmlPullParserException; |
||
72 | |||
73 | 33738 | jpiera | import org.gvsig.remoteclient.utils.CapabilitiesTags; |
74 | |||
75 | 4293 | jaume | /**
|
76 | 6422 | jaume | *
|
77 | 4293 | jaume | * @author jaume dominguez faus
|
78 | *
|
||
79 | */
|
||
80 | public class WCSProtocolHandlerFactory { |
||
81 | 6422 | jaume | |
82 | 29649 | jpiera | public org.gvsig.remoteclient.wcs.WCSProtocolHandler wCSProtocolHandler;
|
83 | 6422 | jaume | |
84 | 4293 | jaume | private static ArrayList supportedVersions = new ArrayList(); |
85 | 6422 | jaume | |
86 | 4293 | jaume | static {
|
87 | supportedVersions.add("1.0.0");
|
||
88 | } |
||
89 | 6422 | jaume | |
90 | 4293 | jaume | /**
|
91 | * M?todo que dada una respuesta de getCapabilities y un iterador sobre una
|
||
92 | * coleccion de WCSClient's ordenada descendentemente devuelve el cliente
|
||
93 | * cuya version es igual o inmediatamente inferior
|
||
94 | *
|
||
95 | * @param caps Capabilities con la respuesta del servidor
|
||
96 | * @param clients Iterador de conjunto ordenado descendientemente
|
||
97 | *
|
||
98 | * @return cliente cuya version es igual o inmediatamente inferior
|
||
99 | * @throws IllegalAccessException
|
||
100 | * @throws InstantiationException
|
||
101 | 6422 | jaume | *
|
102 | 4293 | jaume | */
|
103 | private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException { |
||
104 | while (clients.hasNext()) {
|
||
105 | String clientVersion = (String)clients.next(); |
||
106 | int ret = version.compareTo(clientVersion);
|
||
107 | 6422 | jaume | |
108 | 4293 | jaume | if (ret >= 0) { |
109 | return clientVersion;
|
||
110 | } |
||
111 | } |
||
112 | return null; |
||
113 | } |
||
114 | 6422 | jaume | |
115 | 4293 | jaume | /**
|
116 | * Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
||
117 | * el objeto Capabilities obtenido con dicha versi?n
|
||
118 | *
|
||
119 | * @param host maquina con la que se negocia
|
||
120 | *
|
||
121 | * @return instancia de un cliente capaz de negociar con el host que se
|
||
122 | * pasa como par?metro
|
||
123 | */
|
||
124 | public static WCSProtocolHandler negotiate(String host) throws ConnectException, IOException { |
||
125 | 6422 | jaume | |
126 | if (supportedVersions.size() == 0) |
||
127 | 4293 | jaume | { |
128 | return null; |
||
129 | } |
||
130 | 6422 | jaume | |
131 | 4293 | jaume | try
|
132 | 6422 | jaume | { |
133 | 4293 | jaume | String highestVersionSupportedByServer = getSuitableWCSVersion(host,""); |
134 | if (supportedVersions.contains(highestVersionSupportedByServer))
|
||
135 | { |
||
136 | // we support the highest version supported by the server
|
||
137 | 6422 | jaume | // this is the best case
|
138 | 4293 | jaume | return createVersionDriver(highestVersionSupportedByServer);
|
139 | } |
||
140 | else
|
||
141 | { |
||
142 | // in case we dont support the highest version from the server
|
||
143 | // we start the negotiation process in which we have to get the higest version
|
||
144 | // the WCS supports and we are able to read.
|
||
145 | Iterator iVersion = supportedVersions.iterator();
|
||
146 | String wcsVersion;
|
||
147 | String gvSIGVersion;
|
||
148 | 6422 | jaume | |
149 | while (iVersion.hasNext()) {
|
||
150 | 4293 | jaume | gvSIGVersion = (String)iVersion.next();
|
151 | wcsVersion = getSuitableWCSVersion(host,gvSIGVersion); |
||
152 | //TODO:
|
||
153 | //compare with the version returned by the WMS!!!!!
|
||
154 | // send GetCapabilities and read the version to compare.
|
||
155 | int res = wcsVersion.compareTo(gvSIGVersion);
|
||
156 | 6422 | jaume | |
157 | if (res == 0) { //Si es la misma que nuestra version |
||
158 | 4293 | jaume | return createVersionDriver(gvSIGVersion);
|
159 | } else if (res > 0) { //Si es mayor que nuestra version |
||
160 | throw new Exception("Server Version too high: " + wcsVersion); |
||
161 | } else { //Si es menor que nuestra version |
||
162 | //Obtenemos la primera version menor o igual que tengamos
|
||
163 | String lowerVersion = WCSProtocolHandlerFactory.getDriverVersion(wcsVersion, iVersion);
|
||
164 | 6422 | jaume | |
165 | 4293 | jaume | if (lowerVersion == null) { //Si no hay ninguna |
166 | throw new Exception("Lowest server version is " + wcsVersion); |
||
167 | } else {
|
||
168 | 6422 | jaume | if (lowerVersion.equals(wcsVersion)) {
|
169 | 4293 | jaume | return createVersionDriver(lowerVersion);
|
170 | } else { //Si hay una version menor que la que retorno el servidor |
||
171 | //iV = lower;
|
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | }//case we had to start the negotiation process.
|
||
177 | 6422 | jaume | return null; // if it did not find any suitable version. |
178 | 4293 | jaume | } |
179 | catch(ConnectException conEx) |
||
180 | { |
||
181 | throw conEx;
|
||
182 | } |
||
183 | catch(IOException ioEx) |
||
184 | { |
||
185 | throw ioEx;
|
||
186 | 6422 | jaume | } |
187 | 4293 | jaume | catch(Exception e) |
188 | { |
||
189 | 6422 | jaume | // e.printStackTrace();
|
190 | 4293 | jaume | return null; |
191 | } |
||
192 | } |
||
193 | 6422 | jaume | |
194 | 4293 | jaume | /**
|
195 | * Sends a GetCapabilities to the WCS server to get the version
|
||
196 | * if the version parameter is null, the WCS will return the highest version supported
|
||
197 | * if not it will return the lower highest version than the one requested.
|
||
198 | * @param host
|
||
199 | * @param version
|
||
200 | 6422 | jaume | * @return suitable version supported by the server
|
201 | 4293 | jaume | */
|
202 | 6422 | jaume | private static String getSuitableWCSVersion(String host, String _version) throws ConnectException, IOException { |
203 | 4293 | jaume | String request = WCSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
|
204 | 6422 | jaume | String version = new String(); |
205 | 4293 | jaume | StringReader reader = null; |
206 | //InputStreamReader reader;
|
||
207 | //InputStream is = null;
|
||
208 | DataInputStream dis = null; |
||
209 | 6422 | jaume | try {
|
210 | 4293 | jaume | URL url = new URL(request); |
211 | byte[] buffer = new byte[1024];//new byte[1024*256]; |
||
212 | 6422 | jaume | // is = url.openStream();
|
213 | // reader = new InputStreamReader(is);
|
||
214 | //int numberOfBytes = is.read(buffer);
|
||
215 | 4293 | jaume | //String readed = new String(buffer);
|
216 | 6422 | jaume | dis = new DataInputStream(url.openStream()); |
217 | 4293 | jaume | dis.readFully(buffer); |
218 | 6422 | jaume | |
219 | 4293 | jaume | reader = new StringReader(new String(buffer)); |
220 | KXmlParser kxmlParser = null;
|
||
221 | kxmlParser = new KXmlParser();
|
||
222 | 6422 | jaume | kxmlParser.setInput(reader); |
223 | 4293 | jaume | kxmlParser.nextTag(); |
224 | 6422 | jaume | if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
|
225 | 4427 | jaume | if ((kxmlParser.getName().compareTo(CapabilitiesTags.WCS_CAPABILITIES_ROOT1_0_0)==0)) { |
226 | 4293 | jaume | version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
|
227 | } |
||
228 | } |
||
229 | 6422 | jaume | // do not forget to close the Stream.
|
230 | reader.close(); |
||
231 | 4293 | jaume | dis.close(); |
232 | 6422 | jaume | return version;
|
233 | 4293 | jaume | } catch(ConnectException conEx) { |
234 | 6422 | jaume | throw new ConnectException(conEx.getMessage()); |
235 | 4293 | jaume | } catch(IOException ioEx) { |
236 | 6422 | jaume | throw new IOException(ioEx.getMessage()); |
237 | 4293 | jaume | } catch(XmlPullParserException xmlEx) {
|
238 | xmlEx.printStackTrace(); |
||
239 | return ""; |
||
240 | 6422 | jaume | } finally {
|
241 | 4293 | jaume | if (reader != null) { |
242 | try {
|
||
243 | reader.close(); |
||
244 | } catch(Exception ex) { |
||
245 | 6422 | jaume | ex.printStackTrace(); |
246 | 4293 | jaume | } |
247 | } if (dis != null) { |
||
248 | try {
|
||
249 | dis.close(); |
||
250 | } catch(Exception ex){ |
||
251 | 6422 | jaume | ex.printStackTrace(); |
252 | 4293 | jaume | } |
253 | } |
||
254 | 6422 | jaume | } |
255 | 4293 | jaume | } |
256 | 6422 | jaume | |
257 | 4293 | jaume | /**
|
258 | * It creates an instance of a WCSDriver class.
|
||
259 | *
|
||
260 | * @param String, with the version of the driver to be created
|
||
261 | * @return WCSDriver.
|
||
262 | */
|
||
263 | 6422 | jaume | private static WCSProtocolHandler createVersionDriver(String version) { |
264 | 4293 | jaume | try {
|
265 | Class driver;
|
||
266 | version = version.replace('.', '_'); |
||
267 | 29649 | jpiera | driver = Class.forName("org.gvsig.remoteclient.wcs.wcs_"+version+".WCSProtocolHandler" + version); |
268 | 4293 | jaume | return (WCSProtocolHandler)driver.newInstance();
|
269 | } catch (Exception e) { |
||
270 | e.printStackTrace(); |
||
271 | //throw new Exception("WCSDriverFactory. Unknown driver version " + e);
|
||
272 | return null; |
||
273 | } |
||
274 | 6422 | jaume | } |
275 | |||
276 | |||
277 | 4293 | jaume | } |