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