svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wmts / WMTSProtocolHandlerFactory.java @ 40769
History | View | Annotate | Download (6.01 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 |
package org.gvsig.remoteclient.wmts; |
25 |
|
26 |
import java.io.DataInputStream; |
27 |
import java.io.IOException; |
28 |
import java.io.StringReader; |
29 |
import java.net.ConnectException; |
30 |
import java.net.URL; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Iterator; |
33 |
|
34 |
import org.kxml2.io.KXmlParser; |
35 |
import org.xmlpull.v1.XmlPullParserException; |
36 |
|
37 |
import org.gvsig.remoteclient.utils.CapabilitiesTags; |
38 |
|
39 |
/**
|
40 |
* This factory builds a new handler depending on the supported protocol.
|
41 |
* This protocol could be negotiated with the server. Now only 1.0.0 version
|
42 |
* is supported.
|
43 |
*
|
44 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
45 |
*/
|
46 |
public class WMTSProtocolHandlerFactory { |
47 |
public org.gvsig.remoteclient.wms.WMSProtocolHandler wMSProtocolHandler;
|
48 |
|
49 |
private static ArrayList supportedVersions = new ArrayList(); |
50 |
|
51 |
static {
|
52 |
supportedVersions.add("1.0.0");
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* M?todo que dada una respuesta de getCapabilities y un iterador sobre una
|
57 |
* coleccion de WMSClient's ordenada descendentemente devuelve el cliente
|
58 |
* cuya version es igual o inmediatamente inferior
|
59 |
*
|
60 |
* @param caps Capabilities con la respuesta del servidor
|
61 |
* @param clients Iterador de conjunto ordenado descendientemente
|
62 |
*
|
63 |
* @return cliente cuya version es igual o inmediatamente inferior
|
64 |
* @throws IllegalAccessException
|
65 |
* @throws InstantiationException
|
66 |
*
|
67 |
*/
|
68 |
private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException { |
69 |
while (clients.hasNext()) {
|
70 |
String clientVersion = (String)clients.next(); |
71 |
int ret = version.compareTo(clientVersion);
|
72 |
|
73 |
if (ret >= 0) { |
74 |
return clientVersion;
|
75 |
} |
76 |
} |
77 |
return null; |
78 |
} |
79 |
|
80 |
/**
|
81 |
* Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
82 |
* el objeto Capabilities obtenido con dicha versi?n
|
83 |
*
|
84 |
* @param host maquina con la que se negocia
|
85 |
*
|
86 |
* @return instancia de un cliente capaz de negociar con el host que se
|
87 |
* pasa como par?metro
|
88 |
*/
|
89 |
public static WMTSProtocolHandler negotiate(String host) throws ConnectException, IOException { |
90 |
String highestVersionSupportedByServer = getSuitableWMTSVersion(host, ""); |
91 |
if (supportedVersions.contains(highestVersionSupportedByServer)) {
|
92 |
//we support the highest version supported by the server
|
93 |
// this is the best case
|
94 |
return createVersionDriver(highestVersionSupportedByServer);
|
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Sends a GetCapabilities to the WMTS server to get the version
|
101 |
* if the version parameter is null, the WMTS will return the highest version supported
|
102 |
* if not it will return the lower highest version than the one requested.
|
103 |
* @param host
|
104 |
* @param version
|
105 |
* @return suitable version supported by the server
|
106 |
*/
|
107 |
private static String getSuitableWMTSVersion(String host, String _version) throws ConnectException, IOException { |
108 |
String request = WMTSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
|
109 |
String version = ""; |
110 |
StringReader reader = null; |
111 |
DataInputStream dis = null; |
112 |
|
113 |
try {
|
114 |
URL url = new URL(request); |
115 |
byte[] buffer = new byte[1024]; |
116 |
dis = new DataInputStream(url.openStream()); |
117 |
dis.readFully(buffer); |
118 |
String string = new String(buffer); |
119 |
|
120 |
int a = string.toLowerCase().indexOf("<?xml"); |
121 |
if (a !=-1) |
122 |
string = string.substring(a, string.length()); |
123 |
|
124 |
reader = new StringReader(string); |
125 |
KXmlParser kxmlParser = null;
|
126 |
kxmlParser = new KXmlParser();
|
127 |
kxmlParser.setInput(reader); |
128 |
kxmlParser.nextTag(); |
129 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
|
130 |
if ((kxmlParser.getName().compareTo(CapabilitiesTags.WMTS_CAPABILITIES) == 0)) { |
131 |
version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
|
132 |
} |
133 |
} |
134 |
// do not forget to close the Stream.
|
135 |
reader.close(); |
136 |
dis.close(); |
137 |
return version;
|
138 |
} catch(XmlPullParserException xmlEx) {
|
139 |
xmlEx.printStackTrace(); |
140 |
return ""; |
141 |
} finally {
|
142 |
if (reader != null) { |
143 |
try {
|
144 |
reader.close(); |
145 |
} catch(Exception ex) { |
146 |
ex.printStackTrace(); |
147 |
} |
148 |
} |
149 |
if (dis != null) { |
150 |
try {
|
151 |
dis.close(); |
152 |
} catch(Exception ex) { |
153 |
ex.printStackTrace(); |
154 |
} |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
/**
|
160 |
* It creates an instance of a WMSDriver class.
|
161 |
*
|
162 |
* @param String, with the version of the driver to be created
|
163 |
* @return WMSDriver.
|
164 |
*/
|
165 |
private static WMTSProtocolHandler createVersionDriver(String version) { |
166 |
try {
|
167 |
Class driver;
|
168 |
version = version.replace('.', '_'); |
169 |
driver = Class.forName("org.gvsig.remoteclient.wmts.wmts_" + version + ".WMTSProtocolHandler" + version); |
170 |
return (WMTSProtocolHandler)driver.newInstance();
|
171 |
} catch (Exception e) { |
172 |
e.printStackTrace(); |
173 |
//throw new Exception("WMSDriverFactory. Unknown driver version " + e);
|
174 |
return null; |
175 |
} |
176 |
} |
177 |
|
178 |
} |