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