root / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSProtocolHandlerFactory.java @ 33803
History | View | Annotate | Download (8.26 KB)
1 |
|
---|---|
2 |
package org.gvsig.remoteclient.wms; |
3 |
|
4 |
import java.io.DataInputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.StringReader; |
7 |
import java.net.ConnectException; |
8 |
import java.net.URL; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Iterator; |
11 |
|
12 |
import org.kxml2.io.KXmlParser; |
13 |
import org.xmlpull.v1.XmlPullParserException; |
14 |
|
15 |
import org.gvsig.remoteclient.utils.CapabilitiesTags; |
16 |
|
17 |
/**
|
18 |
* <p></p>
|
19 |
*
|
20 |
*/
|
21 |
public class WMSProtocolHandlerFactory { |
22 |
/**
|
23 |
* <p></p>
|
24 |
*
|
25 |
*/
|
26 |
public org.gvsig.remoteclient.wms.WMSProtocolHandler wMSProtocolHandler;
|
27 |
|
28 |
private static ArrayList supportedVersions = new ArrayList(); |
29 |
|
30 |
static {
|
31 |
/*
|
32 |
* Se meten en el array versions las distintas versiones
|
33 |
* del protocolo en orden descendente
|
34 |
*/
|
35 |
//versions.add(WMSProtocolHandler1_3_0.class);
|
36 |
//versions.add(WMSProtocolHandler1_1_1.class);
|
37 |
supportedVersions.add("1.3.0");
|
38 |
supportedVersions.add("1.1.1");
|
39 |
supportedVersions.add("1.1.0");
|
40 |
} |
41 |
|
42 |
/**
|
43 |
* M?todo que dada una respuesta de getCapabilities y un iterador sobre una
|
44 |
* coleccion de WMSClient's ordenada descendentemente devuelve el cliente
|
45 |
* cuya version es igual o inmediatamente inferior
|
46 |
*
|
47 |
* @param caps Capabilities con la respuesta del servidor
|
48 |
* @param clients Iterador de conjunto ordenado descendientemente
|
49 |
*
|
50 |
* @return cliente cuya version es igual o inmediatamente inferior
|
51 |
* @throws IllegalAccessException
|
52 |
* @throws InstantiationException
|
53 |
*
|
54 |
*/
|
55 |
private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException { |
56 |
while (clients.hasNext()) {
|
57 |
String clientVersion = (String)clients.next(); |
58 |
int ret = version.compareTo(clientVersion);
|
59 |
|
60 |
if (ret >= 0) { |
61 |
return clientVersion;
|
62 |
} |
63 |
} |
64 |
return null; |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
69 |
* el objeto Capabilities obtenido con dicha versi?n
|
70 |
*
|
71 |
* @param host maquina con la que se negocia
|
72 |
*
|
73 |
* @return instancia de un cliente capaz de negociar con el host que se
|
74 |
* pasa como par?metro
|
75 |
*/
|
76 |
public static WMSProtocolHandler negotiate(String host) throws ConnectException, IOException { |
77 |
|
78 |
if (supportedVersions.size() == 0) |
79 |
{ |
80 |
return null; |
81 |
} |
82 |
|
83 |
try
|
84 |
{ |
85 |
String highestVersionSupportedByServer = getSuitableWMSVersion(host,""); |
86 |
if (supportedVersions.contains(highestVersionSupportedByServer))
|
87 |
{ |
88 |
//we support the highest version supported by the server
|
89 |
// this is the best case
|
90 |
return createVersionDriver(highestVersionSupportedByServer);
|
91 |
} |
92 |
|
93 |
|
94 |
else
|
95 |
{ |
96 |
// in case we dont support the highest version from the server
|
97 |
// we start the negotiation process in which we have to get the higest version
|
98 |
// the WMS supports and we are able to read.
|
99 |
Iterator iVersion = supportedVersions.iterator();
|
100 |
String wmsVersion;
|
101 |
String gvSIGVersion;
|
102 |
|
103 |
while (iVersion.hasNext()) {
|
104 |
gvSIGVersion = (String)iVersion.next();
|
105 |
wmsVersion = getSuitableWMSVersion(host,gvSIGVersion); |
106 |
//TODO:
|
107 |
//compare with the version returned by the WMS!!!!!
|
108 |
// send GetCapabilities and read the version to compare.
|
109 |
int res = wmsVersion.compareTo(gvSIGVersion);
|
110 |
|
111 |
if (res == 0) { //Si es la misma que nuestra version |
112 |
return createVersionDriver(gvSIGVersion);
|
113 |
} else if (res > 0) { //Si es mayor que nuestra version |
114 |
throw new Exception("Server Version too high: " + wmsVersion); |
115 |
} else { //Si es menor que nuestra version |
116 |
//Obtenemos la primera version menor o igual que tengamos
|
117 |
String lowerVersion = WMSProtocolHandlerFactory.getDriverVersion(wmsVersion, iVersion);
|
118 |
|
119 |
if (lowerVersion == null) { //Si no hay ninguna |
120 |
throw new Exception("Lowest server version is " + wmsVersion); |
121 |
} else {
|
122 |
if (lowerVersion.equals(wmsVersion)) {
|
123 |
return createVersionDriver(lowerVersion);
|
124 |
} else { //Si hay una version menor que la que retorno el servidor |
125 |
//iV = lower;
|
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
}//case we had to start the negotiation process.
|
131 |
return null; // if it did not find any suitable version. |
132 |
} |
133 |
catch(ConnectException conEx) |
134 |
{ |
135 |
throw conEx;
|
136 |
} |
137 |
catch(IOException ioEx) |
138 |
{ |
139 |
throw ioEx;
|
140 |
} |
141 |
catch(Exception e) |
142 |
{ |
143 |
e.printStackTrace(); |
144 |
return null; |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Sends a GetCapabilities to the WMS server to get the version
|
150 |
* if the version parameter is null, the WMS will return the highest version supported
|
151 |
* if not it will return the lower highest version than the one requested.
|
152 |
* @param host
|
153 |
* @param version
|
154 |
* @return suitable version supported by the server
|
155 |
*/
|
156 |
private static String getSuitableWMSVersion(String host, String _version) throws ConnectException, IOException |
157 |
{ |
158 |
String request = WMSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
|
159 |
String version = new String(); |
160 |
StringReader reader = null; |
161 |
//InputStreamReader reader;
|
162 |
//InputStream is = null;
|
163 |
DataInputStream dis = null; |
164 |
try
|
165 |
{ |
166 |
URL url = new URL(request); |
167 |
byte[] buffer = new byte[1024];//new byte[1024*256]; |
168 |
// is = url.openStream();
|
169 |
// reader = new InputStreamReader(is);
|
170 |
//int numberOfBytes = is.read(buffer);
|
171 |
//String readed = new String(buffer);
|
172 |
dis = new DataInputStream(url.openStream()); |
173 |
dis.readFully(buffer); |
174 |
String string = new String(buffer); |
175 |
|
176 |
// patch for ArcIMS + WMS connector > 9.0 bug
|
177 |
int a = string.toLowerCase().indexOf("<?xml"); |
178 |
if (a !=-1) |
179 |
string = string.substring(a, string.length()); |
180 |
// end patch
|
181 |
|
182 |
|
183 |
reader = new StringReader(string); |
184 |
KXmlParser kxmlParser = null;
|
185 |
kxmlParser = new KXmlParser();
|
186 |
kxmlParser.setInput(reader); |
187 |
kxmlParser.nextTag(); |
188 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
189 |
{ |
190 |
if ((kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_0)==0) |
191 |
||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_1)==0)
|
192 |
||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_3_0)==0))
|
193 |
{ |
194 |
version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
|
195 |
} |
196 |
} |
197 |
// do not forget to close the Stream.
|
198 |
reader.close(); |
199 |
dis.close(); |
200 |
return version;
|
201 |
} |
202 |
catch(ConnectException conEx) |
203 |
{ |
204 |
throw new ConnectException(conEx.getMessage()); |
205 |
} |
206 |
catch(IOException ioEx) |
207 |
{ |
208 |
throw new IOException(ioEx.getMessage()); |
209 |
} |
210 |
catch(XmlPullParserException xmlEx)
|
211 |
{ |
212 |
xmlEx.printStackTrace(); |
213 |
return ""; |
214 |
} |
215 |
finally{
|
216 |
if (reader != null) |
217 |
{ |
218 |
try{
|
219 |
reader.close(); |
220 |
}catch(Exception ex){ |
221 |
ex.printStackTrace(); |
222 |
} |
223 |
} |
224 |
if (dis != null) |
225 |
{ |
226 |
try {
|
227 |
dis.close(); |
228 |
} catch(Exception ex) { |
229 |
ex.printStackTrace(); |
230 |
} |
231 |
} |
232 |
} |
233 |
} |
234 |
|
235 |
/**
|
236 |
* It creates an instance of a WMSDriver class.
|
237 |
*
|
238 |
* @param String, with the version of the driver to be created
|
239 |
* @return WMSDriver.
|
240 |
*/
|
241 |
private static WMSProtocolHandler createVersionDriver(String version) |
242 |
{ |
243 |
try
|
244 |
{ |
245 |
Class driver;
|
246 |
version = version.replace('.', '_'); |
247 |
driver = Class.forName("org.gvsig.remoteclient.wms.wms_"+version+".WMSProtocolHandler" + version); |
248 |
return (WMSProtocolHandler)driver.newInstance();
|
249 |
} |
250 |
catch (Exception e) |
251 |
{ |
252 |
e.printStackTrace(); |
253 |
//throw new Exception("WMSDriverFactory. Unknown driver version " + e);
|
254 |
return null; |
255 |
} |
256 |
} |
257 |
|
258 |
} |