gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.ogc / org.gvsig.raster.wmts.ogc.impl / src / main / java / org / gvsig / raster / wmts / ogc / impl / base / WMTSProtocolHandlerFactory.java @ 2485
History | View | Annotate | Download (4.95 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.raster.wmts.ogc.impl.base; |
23 |
|
24 |
import java.io.DataInputStream; |
25 |
import java.io.IOException; |
26 |
import java.io.StringReader; |
27 |
import java.net.ConnectException; |
28 |
import java.net.URL; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
|
32 |
import org.gvsig.raster.wmts.ogc.impl.Tags; |
33 |
import org.kxml2.io.KXmlParser; |
34 |
import org.xmlpull.v1.XmlPullParserException; |
35 |
|
36 |
/**
|
37 |
* This factory builds a new handler depending on the supported protocol.
|
38 |
* This protocol could be negotiated with the server. Now only 1.0.0 version
|
39 |
* is supported.
|
40 |
*
|
41 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
42 |
*/
|
43 |
public class WMTSProtocolHandlerFactory { |
44 |
|
45 |
private static List<String> supportedVersions = new ArrayList<String>(); |
46 |
|
47 |
static {
|
48 |
supportedVersions.add("1.0.0");
|
49 |
} |
50 |
|
51 |
/**
|
52 |
* Establece la versi?n con la que se comunicar? con el servidor y devuelve
|
53 |
* el objeto Capabilities obtenido con dicha versi?n
|
54 |
*
|
55 |
* @param host maquina con la que se negocia
|
56 |
*
|
57 |
* @return instancia de un cliente capaz de negociar con el host que se
|
58 |
* pasa como par?metro
|
59 |
*/
|
60 |
public static WMTSProtocolHandler negotiate(String host) throws ConnectException, IOException { |
61 |
String highestVersionSupportedByServer = getSuitableWMTSVersion(host, ""); |
62 |
if (supportedVersions.contains(highestVersionSupportedByServer)) {
|
63 |
//we support the highest version supported by the server
|
64 |
// this is the best case
|
65 |
return createVersionDriver(highestVersionSupportedByServer);
|
66 |
} |
67 |
return null; |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Sends a GetCapabilities to the WMTS server to get the version
|
72 |
* if the version parameter is null, the WMTS will return the highest version supported
|
73 |
* if not it will return the lower highest version than the one requested.
|
74 |
* @param host
|
75 |
* @param version
|
76 |
* @return suitable version supported by the server
|
77 |
*/
|
78 |
private static String getSuitableWMTSVersion(String host, String _version) throws ConnectException, IOException { |
79 |
String request = WMTSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
|
80 |
String version = ""; |
81 |
StringReader reader = null; |
82 |
DataInputStream dis = null; |
83 |
|
84 |
try {
|
85 |
URL url = new URL(request); |
86 |
byte[] buffer = new byte[1024]; |
87 |
dis = new DataInputStream(url.openStream()); |
88 |
dis.readFully(buffer); |
89 |
String string = new String(buffer); |
90 |
|
91 |
int a = string.toLowerCase().indexOf("<?xml"); |
92 |
if (a !=-1) |
93 |
string = string.substring(a, string.length()); |
94 |
|
95 |
reader = new StringReader(string); |
96 |
KXmlParser kxmlParser = null;
|
97 |
kxmlParser = new KXmlParser();
|
98 |
kxmlParser.setInput(reader); |
99 |
kxmlParser.nextTag(); |
100 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
|
101 |
if ((kxmlParser.getName().compareTo(Tags.WMTS_CAPABILITIES) == 0)) { |
102 |
version = kxmlParser.getAttributeValue("", Tags.VERSION);
|
103 |
} |
104 |
} |
105 |
// do not forget to close the Stream.
|
106 |
reader.close(); |
107 |
dis.close(); |
108 |
return version;
|
109 |
} catch(XmlPullParserException xmlEx) {
|
110 |
xmlEx.printStackTrace(); |
111 |
return ""; |
112 |
} finally {
|
113 |
if (reader != null) { |
114 |
try {
|
115 |
reader.close(); |
116 |
} catch(Exception ex) { |
117 |
ex.printStackTrace(); |
118 |
} |
119 |
} |
120 |
if (dis != null) { |
121 |
try {
|
122 |
dis.close(); |
123 |
} catch(Exception ex) { |
124 |
ex.printStackTrace(); |
125 |
} |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* It creates an instance of a WMSDriver class.
|
132 |
*
|
133 |
* @param String, with the version of the driver to be created
|
134 |
* @return WMSDriver.
|
135 |
*/
|
136 |
private static WMTSProtocolHandler createVersionDriver(String version) { |
137 |
try {
|
138 |
Class<?> driver;
|
139 |
version = version.replace('.', '_'); |
140 |
driver = Class.forName("org.gvsig.raster.wmts.ogc.impl.wmts_" + version + ".WMTSProtocolHandler" + version); |
141 |
return (WMTSProtocolHandler)driver.newInstance();
|
142 |
} catch (Exception e) { |
143 |
e.printStackTrace(); |
144 |
//throw new Exception("WMSDriverFactory. Unknown driver version " + e);
|
145 |
return null; |
146 |
} |
147 |
} |
148 |
|
149 |
} |