root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / WMTSProtocolHandler.java @ 34305
History | View | Annotate | Download (7.68 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.remoteclient.wmts; |
23 |
|
24 |
import java.io.ByteArrayInputStream; |
25 |
import java.io.File; |
26 |
import java.io.IOException; |
27 |
import java.util.ArrayList; |
28 |
import java.util.StringTokenizer; |
29 |
|
30 |
import org.gvsig.compat.net.ICancellable; |
31 |
import org.gvsig.remoteclient.exceptions.ServerErrorException; |
32 |
import org.gvsig.remoteclient.ogc.OGCProtocolHandler; |
33 |
import org.gvsig.remoteclient.ogc.OGCServiceInformation; |
34 |
import org.gvsig.remoteclient.utils.ExceptionTags; |
35 |
import org.gvsig.remoteclient.utils.Utilities; |
36 |
import org.gvsig.remoteclient.wms.WMSServiceInformation; |
37 |
import org.gvsig.remoteclient.wmts.exception.WMTSException; |
38 |
import org.gvsig.remoteclient.wmts.request.WMTSGetCapabilitiesRequest; |
39 |
import org.gvsig.remoteclient.wmts.request.WMTSGetTileRequest; |
40 |
import org.kxml2.io.KXmlParser; |
41 |
import org.xmlpull.v1.XmlPullParserException; |
42 |
|
43 |
/**
|
44 |
* <p> Abstract class that represents handlers to comunicate via WMS protocol.</p>
|
45 |
*
|
46 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
47 |
*/
|
48 |
public abstract class WMTSProtocolHandler extends OGCProtocolHandler { |
49 |
/**
|
50 |
* Encoding used to parse different xml documents.
|
51 |
*/
|
52 |
protected String encoding = "UTF-8"; |
53 |
protected WMSServiceInformation serviceInfo = null; |
54 |
|
55 |
protected abstract WMTSGetTileRequest createGetTileRequest(WMTSStatus status); |
56 |
|
57 |
protected abstract WMTSGetCapabilitiesRequest createGetCapabilitiesRequest(); |
58 |
|
59 |
/**
|
60 |
* Sets the status object
|
61 |
* @param status
|
62 |
*/
|
63 |
protected abstract void setStatus(WMTSServerDescription status); |
64 |
|
65 |
public File getTile(WMTSStatus status, ICancellable cancel) throws ServerErrorException, WMTSException { |
66 |
try {
|
67 |
WMTSGetTileRequest request = createGetTileRequest(status); |
68 |
File f = request.sendRequest(cancel);
|
69 |
|
70 |
if (f== null) |
71 |
return null; |
72 |
if (Utilities.isTextFile(f)) { |
73 |
byte[] data = fileToBytes(f); |
74 |
|
75 |
WMTSException wmsEx = null;
|
76 |
|
77 |
String exceptionMessage = parseException(data);
|
78 |
if (exceptionMessage == null) { |
79 |
String error = new String(data); |
80 |
int pos = error.indexOf("<?xml"); |
81 |
if (pos!= -1) { |
82 |
String xml = error.substring(pos,error.length());
|
83 |
exceptionMessage = parseException(xml.getBytes()); |
84 |
} |
85 |
if (exceptionMessage == null) |
86 |
exceptionMessage = new String(data); |
87 |
|
88 |
} |
89 |
wmsEx = new WMTSException(exceptionMessage);
|
90 |
|
91 |
// Since it is an error file, It must be deleted from the cache
|
92 |
downloader.removeURL(request); |
93 |
throw wmsEx;
|
94 |
} |
95 |
return f;
|
96 |
} |
97 |
catch(IOException e) { |
98 |
e.printStackTrace(); |
99 |
throw new ServerErrorException(); |
100 |
} |
101 |
} |
102 |
|
103 |
/**
|
104 |
* <p>Builds a GetCapabilities request that is sent to the WMS
|
105 |
* the response will be parse to extract the data needed by the
|
106 |
* WMS client</p>
|
107 |
* @param override, if true the previous downloaded data will be overridden
|
108 |
*/
|
109 |
public void getCapabilities(WMTSServerDescription status, boolean override, ICancellable cancel) { |
110 |
try {
|
111 |
setStatus(status); |
112 |
WMTSGetCapabilitiesRequest request = createGetCapabilitiesRequest(); |
113 |
File f = request.sendRequest(cancel);
|
114 |
|
115 |
if (f == null) |
116 |
return;
|
117 |
parseCapabilities(f); |
118 |
} catch(Exception e) { |
119 |
e.printStackTrace(); |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
/**
|
125 |
* Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
|
126 |
* without a VERSION, to get the highest version than a WMS supports.
|
127 |
*/
|
128 |
public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version) { |
129 |
int index = _host.indexOf('?'); |
130 |
|
131 |
if (index > -1) { |
132 |
String host = _host.substring(0, index + 1); |
133 |
String query = _host.substring(index + 1, _host.length()); |
134 |
|
135 |
StringTokenizer tokens = new StringTokenizer(query, "&"); |
136 |
String newQuery = "", token; |
137 |
|
138 |
// If there is a field or a value with spaces, (and then it's on different tokens) -> unify them
|
139 |
while (tokens.hasMoreTokens()) {
|
140 |
token = tokens.nextToken().trim(); |
141 |
|
142 |
if (token.toUpperCase().compareTo("REQUEST=GETCAPABILITIES") == 0) |
143 |
continue;
|
144 |
|
145 |
if (token.toUpperCase().compareTo("SERVICE=WMTS") == 0) |
146 |
continue;
|
147 |
|
148 |
if ((_version != null) && (_version.length() > 0)) { |
149 |
if (token.toUpperCase().compareTo("VERSION=" + _version) == 0) |
150 |
continue;
|
151 |
} |
152 |
|
153 |
if (token.toUpperCase().compareTo("EXCEPTIONS=XML") == 0) |
154 |
continue;
|
155 |
|
156 |
newQuery += token + "&";
|
157 |
} |
158 |
|
159 |
_host = host + newQuery; |
160 |
} |
161 |
else {
|
162 |
_host += "?";
|
163 |
} |
164 |
|
165 |
if ((_version != null) && (_version.compareTo("") != 0)) |
166 |
_host += "REQUEST=GetCapabilities&SERVICE=WMTS&VERSION=" + _version;
|
167 |
else
|
168 |
_host += "REQUEST=GetCapabilities&SERVICE=WMTS";
|
169 |
|
170 |
return _host;
|
171 |
} |
172 |
|
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
* @see org.gvsig.remoteclient.ogc.OGCProtocolHandler#getServiceInformation()
|
176 |
*/
|
177 |
public OGCServiceInformation getServiceInformation() {
|
178 |
return serviceInfo;
|
179 |
} |
180 |
|
181 |
@SuppressWarnings("unchecked") |
182 |
protected String parseException(byte[] data) { |
183 |
ArrayList errors = new ArrayList(); |
184 |
KXmlParser kxmlParser = new KXmlParser();
|
185 |
try
|
186 |
{ |
187 |
kxmlParser.setInput(new ByteArrayInputStream(data), encoding); |
188 |
kxmlParser.nextTag(); |
189 |
int tag;
|
190 |
if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
191 |
{ |
192 |
kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
|
193 |
tag = kxmlParser.nextTag(); |
194 |
while(tag != KXmlParser.END_DOCUMENT)
|
195 |
{ |
196 |
switch(tag)
|
197 |
{ |
198 |
case KXmlParser.START_TAG:
|
199 |
if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){ |
200 |
String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE); |
201 |
errorCode = (errorCode != null) ? "["+errorCode+"] " : ""; |
202 |
String errorMessage = kxmlParser.nextText();
|
203 |
errors.add(errorCode+errorMessage); |
204 |
} |
205 |
break;
|
206 |
case KXmlParser.END_TAG:
|
207 |
break;
|
208 |
|
209 |
} |
210 |
tag = kxmlParser.nextTag(); |
211 |
} |
212 |
//kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
|
213 |
} |
214 |
} |
215 |
catch(XmlPullParserException parser_ex){
|
216 |
parser_ex.printStackTrace(); |
217 |
} |
218 |
catch (IOException ioe) { |
219 |
ioe.printStackTrace(); |
220 |
} |
221 |
String message = errors.size()>0? "" : null; |
222 |
for (int i = 0; i < errors.size(); i++) { |
223 |
message += (String) errors.get(i)+"\n"; |
224 |
} |
225 |
return message;
|
226 |
} |
227 |
|
228 |
} |