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