root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wcs / WCSProtocolHandlerFactory.java @ 33738
History | View | Annotate | Download (8.07 KB)
1 |
/* 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 |
* $Id: WCSProtocolHandlerFactory.java 33738 2010-10-21 11:54:20Z jpiera $
|
43 |
* $Log$
|
44 |
* 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 |
* clean up unnecessary imports
|
49 |
*
|
50 |
* Revision 1.2 2006/03/15 08:54:42 jaume
|
51 |
* *** empty log message ***
|
52 |
*
|
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 |
*
|
59 |
*/
|
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.kxml2.io.KXmlParser; |
71 |
import org.xmlpull.v1.XmlPullParserException; |
72 |
|
73 |
import org.gvsig.remoteclient.utils.CapabilitiesTags; |
74 |
|
75 |
/**
|
76 |
*
|
77 |
* @author jaume dominguez faus
|
78 |
*
|
79 |
*/
|
80 |
public class WCSProtocolHandlerFactory { |
81 |
|
82 |
public org.gvsig.remoteclient.wcs.WCSProtocolHandler wCSProtocolHandler;
|
83 |
|
84 |
private static ArrayList supportedVersions = new ArrayList(); |
85 |
|
86 |
static {
|
87 |
supportedVersions.add("1.0.0");
|
88 |
} |
89 |
|
90 |
/**
|
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 |
*
|
102 |
*/
|
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 |
|
108 |
if (ret >= 0) { |
109 |
return clientVersion;
|
110 |
} |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/**
|
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 |
|
126 |
if (supportedVersions.size() == 0) |
127 |
{ |
128 |
return null; |
129 |
} |
130 |
|
131 |
try
|
132 |
{ |
133 |
String highestVersionSupportedByServer = getSuitableWCSVersion(host,""); |
134 |
if (supportedVersions.contains(highestVersionSupportedByServer))
|
135 |
{ |
136 |
// we support the highest version supported by the server
|
137 |
// this is the best case
|
138 |
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 |
|
149 |
while (iVersion.hasNext()) {
|
150 |
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 |
|
157 |
if (res == 0) { //Si es la misma que nuestra version |
158 |
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 |
|
165 |
if (lowerVersion == null) { //Si no hay ninguna |
166 |
throw new Exception("Lowest server version is " + wcsVersion); |
167 |
} else {
|
168 |
if (lowerVersion.equals(wcsVersion)) {
|
169 |
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 |
return null; // if it did not find any suitable version. |
178 |
} |
179 |
catch(ConnectException conEx) |
180 |
{ |
181 |
throw conEx;
|
182 |
} |
183 |
catch(IOException ioEx) |
184 |
{ |
185 |
throw ioEx;
|
186 |
} |
187 |
catch(Exception e) |
188 |
{ |
189 |
// e.printStackTrace();
|
190 |
return null; |
191 |
} |
192 |
} |
193 |
|
194 |
/**
|
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 |
* @return suitable version supported by the server
|
201 |
*/
|
202 |
private static String getSuitableWCSVersion(String host, String _version) throws ConnectException, IOException { |
203 |
String request = WCSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
|
204 |
String version = new String(); |
205 |
StringReader reader = null; |
206 |
//InputStreamReader reader;
|
207 |
//InputStream is = null;
|
208 |
DataInputStream dis = null; |
209 |
try {
|
210 |
URL url = new URL(request); |
211 |
byte[] buffer = new byte[1024];//new byte[1024*256]; |
212 |
// is = url.openStream();
|
213 |
// reader = new InputStreamReader(is);
|
214 |
//int numberOfBytes = is.read(buffer);
|
215 |
//String readed = new String(buffer);
|
216 |
dis = new DataInputStream(url.openStream()); |
217 |
dis.readFully(buffer); |
218 |
|
219 |
reader = new StringReader(new String(buffer)); |
220 |
KXmlParser kxmlParser = null;
|
221 |
kxmlParser = new KXmlParser();
|
222 |
kxmlParser.setInput(reader); |
223 |
kxmlParser.nextTag(); |
224 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
|
225 |
if ((kxmlParser.getName().compareTo(CapabilitiesTags.WCS_CAPABILITIES_ROOT1_0_0)==0)) { |
226 |
version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
|
227 |
} |
228 |
} |
229 |
// do not forget to close the Stream.
|
230 |
reader.close(); |
231 |
dis.close(); |
232 |
return version;
|
233 |
} catch(ConnectException conEx) { |
234 |
throw new ConnectException(conEx.getMessage()); |
235 |
} catch(IOException ioEx) { |
236 |
throw new IOException(ioEx.getMessage()); |
237 |
} catch(XmlPullParserException xmlEx) {
|
238 |
xmlEx.printStackTrace(); |
239 |
return ""; |
240 |
} finally {
|
241 |
if (reader != null) { |
242 |
try {
|
243 |
reader.close(); |
244 |
} catch(Exception ex) { |
245 |
ex.printStackTrace(); |
246 |
} |
247 |
} if (dis != null) { |
248 |
try {
|
249 |
dis.close(); |
250 |
} catch(Exception ex){ |
251 |
ex.printStackTrace(); |
252 |
} |
253 |
} |
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
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 |
private static WCSProtocolHandler createVersionDriver(String version) { |
264 |
try {
|
265 |
Class driver;
|
266 |
version = version.replace('.', '_'); |
267 |
driver = Class.forName("org.gvsig.remoteclient.wcs.wcs_"+version+".WCSProtocolHandler" + version); |
268 |
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 |
} |
275 |
|
276 |
|
277 |
} |