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 / request / WMTSRequest.java @ 2613
History | View | Annotate | Download (10.4 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 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 Iver T.I. {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
* This class sends a WMS request and returns the
|
30 |
* reply in a local File. It tries if the server
|
31 |
* supports both HTTP Get and Post requests.
|
32 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
33 |
*/
|
34 |
package org.gvsig.raster.wmts.ogc.impl.request; |
35 |
|
36 |
import java.io.BufferedOutputStream; |
37 |
import java.io.DataInputStream; |
38 |
import java.io.DataOutputStream; |
39 |
import java.io.File; |
40 |
import java.io.FileOutputStream; |
41 |
import java.io.IOException; |
42 |
import java.net.ConnectException; |
43 |
import java.net.HttpURLConnection; |
44 |
import java.net.MalformedURLException; |
45 |
import java.net.SocketTimeoutException; |
46 |
import java.net.URL; |
47 |
import java.net.UnknownHostException; |
48 |
import java.security.KeyManagementException; |
49 |
import java.security.NoSuchAlgorithmException; |
50 |
import java.util.prefs.Preferences; |
51 |
|
52 |
import javax.net.ssl.HttpsURLConnection; |
53 |
import javax.net.ssl.SSLContext; |
54 |
import javax.net.ssl.TrustManager; |
55 |
import javax.net.ssl.X509TrustManager; |
56 |
|
57 |
import org.gvsig.compat.CompatLocator; |
58 |
import org.gvsig.compat.lang.StringUtils; |
59 |
import org.gvsig.compat.net.ICancellable; |
60 |
import org.gvsig.raster.wmts.ogc.impl.Tags; |
61 |
import org.gvsig.raster.wmts.ogc.impl.Utilities; |
62 |
import org.gvsig.raster.wmts.ogc.impl.base.WMTSProtocolHandler; |
63 |
import org.gvsig.raster.wmts.ogc.impl.base.WMTSStatusImpl; |
64 |
|
65 |
/**
|
66 |
* Base class for a WMTS request
|
67 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
68 |
*/
|
69 |
public abstract class WMTSRequest { |
70 |
protected static final StringUtils stringUtils = CompatLocator.getStringUtils(); |
71 |
protected WMTSStatusImpl status = null; |
72 |
protected WMTSProtocolHandler protocolHandler = null; |
73 |
protected boolean isDeleted = false; |
74 |
protected static final String XMLTAG_STARTCHARACTER = "<"; |
75 |
protected static final String XMLTAG_FINISHCHARACTER = "</"; |
76 |
protected static final String XMLTAG_ENDCHARACTER = ">"; |
77 |
|
78 |
public WMTSRequest(WMTSStatusImpl status,
|
79 |
WMTSProtocolHandler protocolHandler) { |
80 |
this.status = status;
|
81 |
this.protocolHandler = protocolHandler;
|
82 |
} |
83 |
|
84 |
protected abstract String getHttpGetRequest(String onlineResource); |
85 |
|
86 |
protected abstract String getHttpPostRequest(String onlineResource); |
87 |
|
88 |
protected abstract String getTempFilePrefix(); |
89 |
|
90 |
protected abstract String getOperationName(); |
91 |
|
92 |
/**
|
93 |
* @return the URL used in the HTTP get operation
|
94 |
* @throws MalformedURLException
|
95 |
*/
|
96 |
public URL getURL() throws MalformedURLException { |
97 |
String onlineResource = protocolHandler.getBaseURL(getOperationName());
|
98 |
return new URL(getHttpGetRequest(onlineResource)); |
99 |
} |
100 |
|
101 |
protected String getSchemaLocation() { |
102 |
return null; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Gets the part of the OGC request that share GetMap and GetFeatureInfo
|
107 |
* @return String request
|
108 |
*/
|
109 |
protected String getPartialQuery(WMTSStatusImpl status) { |
110 |
String parametersForDimension = getParametersForDimension();
|
111 |
StringBuffer req = new StringBuffer(); |
112 |
req.append("Layer=" + status.getLayer())
|
113 |
.append("&Style=" + status.getStyle())
|
114 |
.append("&Format=" + status.getFormat())
|
115 |
.append(parametersForDimension != null ? parametersForDimension : "") |
116 |
.append("&TileMatrixSet=" + status.getTileMatrixSet())
|
117 |
.append("&TileMatrix=" + status.getTileMatrix())
|
118 |
.append("&TileRow=" + status. getTileRow())
|
119 |
.append("&TileCol=" + status.getTileCol());
|
120 |
return req.toString();
|
121 |
} |
122 |
|
123 |
private String getParametersForDimension() { |
124 |
String dimension = status.getDimension();
|
125 |
String valueForDimension = status.getValueForDimension();
|
126 |
if(dimension != null && valueForDimension != null) |
127 |
return ("&" + dimension + "=" + valueForDimension); |
128 |
return null; |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Send a request to the server.
|
133 |
* @return
|
134 |
* The server reply
|
135 |
* @throws IOException
|
136 |
* @throws UnknownHostException
|
137 |
* @throws ConnectException
|
138 |
*/
|
139 |
public void sendRequest(ICancellable cancel, File file) throws ConnectException, UnknownHostException, IOException { |
140 |
String onlineResource = protocolHandler.getBaseURL(getOperationName());
|
141 |
URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
142 |
//System.out.println(url.toString());
|
143 |
downloadFile(url, file, cancel); |
144 |
|
145 |
//downloadFile(new URL(protocolHandler.getHost()), getHttpGetRequest(onlineResource), file, cancel);
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Send a request to the server.
|
150 |
* @return
|
151 |
* The server reply
|
152 |
* @throws IOException
|
153 |
* @throws UnknownHostException
|
154 |
* @throws ConnectException
|
155 |
*/
|
156 |
public File sendRequest(ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
157 |
//if the status is null is because is a GetCapabilities operation
|
158 |
if (status != null) { |
159 |
if (status.getProtocol() != Tags.PROTOCOL_UNDEFINED){
|
160 |
if (status.getProtocol() == Tags.PROTOCOL_GET){
|
161 |
String onlineResource = protocolHandler.getHost();
|
162 |
String symbol = protocolHandler.getSymbol(onlineResource);
|
163 |
onlineResource = onlineResource + symbol; |
164 |
return sendHttpGetRequest(onlineResource, cancel);
|
165 |
}else{
|
166 |
String onlineResource = protocolHandler.getHost();
|
167 |
return sendHttpPostRequest(onlineResource);
|
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
String onlineResource = protocolHandler.getBaseURL(getOperationName(), Tags.PROTOCOL_GET);
|
173 |
if (onlineResource != null) |
174 |
return sendHttpGetRequest(onlineResource, cancel);
|
175 |
|
176 |
onlineResource = protocolHandler.getBaseURL(getOperationName(), Tags.PROTOCOL_POST); |
177 |
if (onlineResource != null) |
178 |
return sendHttpPostRequest(onlineResource);
|
179 |
|
180 |
return sendHttpGetRequest(protocolHandler.getBaseURL(getOperationName()), cancel);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Send a Http request using the get protocol
|
185 |
* @param onlineResource
|
186 |
* @return
|
187 |
* @throws ConnectException
|
188 |
* @throws UnknownHostException
|
189 |
* @throws IOException
|
190 |
*/
|
191 |
private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
192 |
URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
193 |
if (isDeleted()){
|
194 |
Utilities.removeURL(url);
|
195 |
} |
196 |
return Utilities.downloadFile(url, getTempFilePrefix(), cancel); |
197 |
} |
198 |
|
199 |
/**
|
200 |
* Send a Http request using the post protocol
|
201 |
* @param onlineResource
|
202 |
* @return
|
203 |
* @throws ConnectException
|
204 |
* @throws UnknownHostException
|
205 |
* @throws IOException
|
206 |
*/
|
207 |
private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{ |
208 |
URL url = new URL(onlineResource); |
209 |
String data = getHttpPostRequest(onlineResource);
|
210 |
if (isDeleted()){
|
211 |
Utilities.removeURL(url+data);
|
212 |
} |
213 |
return Utilities.downloadFile(url, data, getTempFilePrefix(), null); |
214 |
} |
215 |
|
216 |
|
217 |
public void cancelDownload() { |
218 |
if(connection != null) { |
219 |
connection.disconnect(); |
220 |
connection = null;
|
221 |
} |
222 |
if(is != null) { |
223 |
try {
|
224 |
is.close(); |
225 |
} catch (IOException e) { |
226 |
} |
227 |
} |
228 |
if(dos != null) { |
229 |
try {
|
230 |
dos.close(); |
231 |
} catch (IOException e) { |
232 |
} |
233 |
} |
234 |
} |
235 |
|
236 |
HttpURLConnection connection = null; |
237 |
DataOutputStream dos = null; |
238 |
DataInputStream is = null; |
239 |
|
240 |
public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException { |
241 |
if(cancel != null && cancel.isCanceled()) |
242 |
throw new IOException(); |
243 |
|
244 |
Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" ); |
245 |
// by default 1 minute (60000 milliseconds.
|
246 |
int timeout = prefs.getInt("timeout", 20000); |
247 |
|
248 |
//If the used protocol is HTTPS
|
249 |
if (url.getProtocol().equals("https")) { |
250 |
try {
|
251 |
disableHttsValidation(); |
252 |
} catch (KeyManagementException e) { |
253 |
throw new IOException(); |
254 |
} catch (NoSuchAlgorithmException e) { |
255 |
throw new IOException(); |
256 |
} |
257 |
} |
258 |
|
259 |
try {
|
260 |
connection = (HttpURLConnection)url.openConnection();
|
261 |
connection.setConnectTimeout(timeout); |
262 |
connection.setReadTimeout(timeout); |
263 |
is = new DataInputStream(connection.getInputStream()); |
264 |
|
265 |
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile))); |
266 |
byte[] buffer = new byte[4096]; |
267 |
|
268 |
int i = -1; |
269 |
|
270 |
if(cancel != null && cancel.isCanceled()) { |
271 |
throw new IOException(); |
272 |
} |
273 |
|
274 |
while((i = is.read(buffer)) > 0) { |
275 |
if(cancel != null && cancel.isCanceled()) { |
276 |
throw new IOException(); |
277 |
} |
278 |
dos.write(buffer, 0, i);
|
279 |
} |
280 |
} catch (SocketTimeoutException e) { |
281 |
throw new IOException(); |
282 |
} finally {
|
283 |
if(is != null) |
284 |
is.close(); |
285 |
if(dos != null) |
286 |
dos.close(); |
287 |
if(connection != null) |
288 |
connection.disconnect(); |
289 |
} |
290 |
} |
291 |
|
292 |
/**
|
293 |
* This method disables the Https certificate validation.
|
294 |
* @throws KeyManagementException
|
295 |
* @throws NoSuchAlgorithmException
|
296 |
*/
|
297 |
private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{ |
298 |
// Create a trust manager that does not validate certificate chains
|
299 |
TrustManager[] trustAllCerts = new TrustManager[] { |
300 |
new X509TrustManager() { |
301 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
302 |
return null; |
303 |
} |
304 |
public void checkClientTrusted( |
305 |
java.security.cert.X509Certificate[] certs, String authType) { |
306 |
} |
307 |
public void checkServerTrusted( |
308 |
java.security.cert.X509Certificate[] certs, String authType) { |
309 |
} |
310 |
} |
311 |
}; |
312 |
|
313 |
// Install the all-trusting trust manager
|
314 |
SSLContext sc = SSLContext.getInstance("SSL"); |
315 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
316 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
317 |
} |
318 |
|
319 |
public boolean isDeleted() { |
320 |
return isDeleted;
|
321 |
} |
322 |
|
323 |
public void setDeleted(boolean isDeleted) { |
324 |
this.isDeleted = isDeleted;
|
325 |
} |
326 |
|
327 |
protected String createXMLStartTag(String tagName){ |
328 |
return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
329 |
} |
330 |
|
331 |
protected String createXMLEndtTag(String tagName){ |
332 |
return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
333 |
} |
334 |
|
335 |
} |