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 @ 1961
History | View | Annotate | Download (9.99 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 |
StringBuffer req = new StringBuffer(); |
111 |
req.append("Layer=" + status.getLayer())
|
112 |
.append("&Style=" + status.getStyle())
|
113 |
.append("&Format=" + status.getFormat())
|
114 |
.append("&TileMatrixSet=" + status.getTileMatrixSet())
|
115 |
.append("&TileMatrix=" + status.getTileMatrix())
|
116 |
.append("&TileRow=" + status. getTileRow())
|
117 |
.append("&TileCol=" + status.getTileCol());
|
118 |
return req.toString();
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* Send a request to the server.
|
123 |
* @return
|
124 |
* The server reply
|
125 |
* @throws IOException
|
126 |
* @throws UnknownHostException
|
127 |
* @throws ConnectException
|
128 |
*/
|
129 |
public void sendRequest(ICancellable cancel, File file) throws ConnectException, UnknownHostException, IOException { |
130 |
String onlineResource = protocolHandler.getBaseURL(getOperationName());
|
131 |
URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
132 |
//System.out.println(url.toString());
|
133 |
downloadFile(url, file, cancel); |
134 |
|
135 |
//downloadFile(new URL(protocolHandler.getHost()), getHttpGetRequest(onlineResource), file, cancel);
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Send a request to the server.
|
140 |
* @return
|
141 |
* The server reply
|
142 |
* @throws IOException
|
143 |
* @throws UnknownHostException
|
144 |
* @throws ConnectException
|
145 |
*/
|
146 |
public File sendRequest(ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
147 |
//if the status is null is because is a GetCapabilities operation
|
148 |
if (status != null) { |
149 |
if (status.getProtocol() != Tags.PROTOCOL_UNDEFINED){
|
150 |
if (status.getProtocol() == Tags.PROTOCOL_GET){
|
151 |
String onlineResource = protocolHandler.getHost();
|
152 |
String symbol = protocolHandler.getSymbol(onlineResource);
|
153 |
onlineResource = onlineResource + symbol; |
154 |
return sendHttpGetRequest(onlineResource, cancel);
|
155 |
}else{
|
156 |
String onlineResource = protocolHandler.getHost();
|
157 |
return sendHttpPostRequest(onlineResource);
|
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
String onlineResource = protocolHandler.getBaseURL(getOperationName(), Tags.PROTOCOL_GET);
|
163 |
if (onlineResource != null) |
164 |
return sendHttpGetRequest(onlineResource, cancel);
|
165 |
|
166 |
onlineResource = protocolHandler.getBaseURL(getOperationName(), Tags.PROTOCOL_POST); |
167 |
if (onlineResource != null) |
168 |
return sendHttpPostRequest(onlineResource);
|
169 |
|
170 |
return sendHttpGetRequest(protocolHandler.getBaseURL(getOperationName()), cancel);
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Send a Http request using the get protocol
|
175 |
* @param onlineResource
|
176 |
* @return
|
177 |
* @throws ConnectException
|
178 |
* @throws UnknownHostException
|
179 |
* @throws IOException
|
180 |
*/
|
181 |
private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
182 |
URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
183 |
if (isDeleted()){
|
184 |
Utilities.removeURL(url);
|
185 |
} |
186 |
return Utilities.downloadFile(url, getTempFilePrefix(), cancel); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Send a Http request using the post protocol
|
191 |
* @param onlineResource
|
192 |
* @return
|
193 |
* @throws ConnectException
|
194 |
* @throws UnknownHostException
|
195 |
* @throws IOException
|
196 |
*/
|
197 |
private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{ |
198 |
URL url = new URL(onlineResource); |
199 |
String data = getHttpPostRequest(onlineResource);
|
200 |
if (isDeleted()){
|
201 |
Utilities.removeURL(url+data);
|
202 |
} |
203 |
return Utilities.downloadFile(url, data, getTempFilePrefix(), null); |
204 |
} |
205 |
|
206 |
|
207 |
public void cancelDownload() { |
208 |
if(connection != null) { |
209 |
connection.disconnect(); |
210 |
connection = null;
|
211 |
} |
212 |
if(is != null) { |
213 |
try {
|
214 |
is.close(); |
215 |
} catch (IOException e) { |
216 |
} |
217 |
} |
218 |
if(dos != null) { |
219 |
try {
|
220 |
dos.close(); |
221 |
} catch (IOException e) { |
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
HttpURLConnection connection = null; |
227 |
DataOutputStream dos = null; |
228 |
DataInputStream is = null; |
229 |
|
230 |
public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException { |
231 |
if(cancel != null && cancel.isCanceled()) |
232 |
throw new IOException(); |
233 |
|
234 |
Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" ); |
235 |
// by default 1 minute (60000 milliseconds.
|
236 |
int timeout = prefs.getInt("timeout", 20000); |
237 |
|
238 |
//If the used protocol is HTTPS
|
239 |
if (url.getProtocol().equals("https")) { |
240 |
try {
|
241 |
disableHttsValidation(); |
242 |
} catch (KeyManagementException e) { |
243 |
throw new IOException(); |
244 |
} catch (NoSuchAlgorithmException e) { |
245 |
throw new IOException(); |
246 |
} |
247 |
} |
248 |
|
249 |
try {
|
250 |
connection = (HttpURLConnection)url.openConnection();
|
251 |
connection.setConnectTimeout(timeout); |
252 |
connection.setReadTimeout(timeout); |
253 |
is = new DataInputStream(connection.getInputStream()); |
254 |
|
255 |
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile))); |
256 |
byte[] buffer = new byte[4096]; |
257 |
|
258 |
int i = -1; |
259 |
|
260 |
if(cancel != null && cancel.isCanceled()) { |
261 |
throw new IOException(); |
262 |
} |
263 |
|
264 |
while((i = is.read(buffer)) > 0) { |
265 |
if(cancel != null && cancel.isCanceled()) { |
266 |
throw new IOException(); |
267 |
} |
268 |
dos.write(buffer, 0, i);
|
269 |
} |
270 |
} catch (SocketTimeoutException e) { |
271 |
throw new IOException(); |
272 |
} finally {
|
273 |
if(is != null) |
274 |
is.close(); |
275 |
if(dos != null) |
276 |
dos.close(); |
277 |
if(connection != null) |
278 |
connection.disconnect(); |
279 |
} |
280 |
} |
281 |
|
282 |
/**
|
283 |
* This method disables the Https certificate validation.
|
284 |
* @throws KeyManagementException
|
285 |
* @throws NoSuchAlgorithmException
|
286 |
*/
|
287 |
private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{ |
288 |
// Create a trust manager that does not validate certificate chains
|
289 |
TrustManager[] trustAllCerts = new TrustManager[] { |
290 |
new X509TrustManager() { |
291 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
292 |
return null; |
293 |
} |
294 |
public void checkClientTrusted( |
295 |
java.security.cert.X509Certificate[] certs, String authType) { |
296 |
} |
297 |
public void checkServerTrusted( |
298 |
java.security.cert.X509Certificate[] certs, String authType) { |
299 |
} |
300 |
} |
301 |
}; |
302 |
|
303 |
// Install the all-trusting trust manager
|
304 |
SSLContext sc = SSLContext.getInstance("SSL"); |
305 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
306 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
307 |
} |
308 |
|
309 |
public boolean isDeleted() { |
310 |
return isDeleted;
|
311 |
} |
312 |
|
313 |
public void setDeleted(boolean isDeleted) { |
314 |
this.isDeleted = isDeleted;
|
315 |
} |
316 |
|
317 |
protected String createXMLStartTag(String tagName){ |
318 |
return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
319 |
} |
320 |
|
321 |
protected String createXMLEndtTag(String tagName){ |
322 |
return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
323 |
} |
324 |
|
325 |
} |