root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / request / WMTSRequest.java @ 38531
History | View | Annotate | Download (6.6 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.remoteclient.wmts.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.SocketTimeoutException; |
45 |
import java.net.URL; |
46 |
import java.net.UnknownHostException; |
47 |
import java.security.KeyManagementException; |
48 |
import java.security.NoSuchAlgorithmException; |
49 |
import java.util.prefs.Preferences; |
50 |
|
51 |
import javax.net.ssl.HttpsURLConnection; |
52 |
import javax.net.ssl.SSLContext; |
53 |
import javax.net.ssl.TrustManager; |
54 |
import javax.net.ssl.X509TrustManager; |
55 |
|
56 |
import org.gvsig.compat.CompatLocator; |
57 |
import org.gvsig.compat.lang.StringUtils; |
58 |
import org.gvsig.compat.net.ICancellable; |
59 |
import org.gvsig.remoteclient.RemoteClientStatus; |
60 |
import org.gvsig.remoteclient.ogc.request.OGCRequest; |
61 |
import org.gvsig.remoteclient.wmts.WMTSProtocolHandler; |
62 |
import org.gvsig.remoteclient.wmts.WMTSStatus; |
63 |
|
64 |
/**
|
65 |
* Base class for a WMTS request
|
66 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
67 |
*/
|
68 |
public abstract class WMTSRequest extends OGCRequest { |
69 |
protected static final StringUtils stringUtils = CompatLocator.getStringUtils(); |
70 |
|
71 |
public WMTSRequest(RemoteClientStatus status,
|
72 |
WMTSProtocolHandler protocolHandler) { |
73 |
super(status, protocolHandler);
|
74 |
} |
75 |
|
76 |
/*
|
77 |
* (non-Javadoc)
|
78 |
* @see org.gvsig.remoteClient.wfs.requests.WFSRequest#getSchemaLocation()
|
79 |
*/
|
80 |
protected String getSchemaLocation() { |
81 |
return null; |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Gets the part of the OGC request that share GetMap and GetFeatureInfo
|
86 |
* @return String request
|
87 |
*/
|
88 |
protected String getPartialQuery(WMTSStatus status) { |
89 |
StringBuffer req = new StringBuffer(); |
90 |
req.append("Layer=" + status.getLayer())
|
91 |
.append("&Style=" + status.getStyle())
|
92 |
.append("&Format=" + status.getFormat())
|
93 |
.append("&TileMatrixSet=" + status.getTileMatrixSet())
|
94 |
.append("&TileMatrix=" + status.getTileMatrix())
|
95 |
.append("&TileRow=" + status. getTileRow())
|
96 |
.append("&TileCol=" + status.getTileCol());
|
97 |
return req.toString();
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Send a request to the server.
|
102 |
* @return
|
103 |
* The server reply
|
104 |
* @throws IOException
|
105 |
* @throws UnknownHostException
|
106 |
* @throws ConnectException
|
107 |
*/
|
108 |
public void sendRequest(ICancellable cancel, File file) throws ConnectException, UnknownHostException, IOException { |
109 |
String onlineResource = protocolHandler.getHost();
|
110 |
String symbol = getSymbol(onlineResource);
|
111 |
onlineResource = onlineResource + symbol; |
112 |
URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
113 |
downloadFile(url, file, cancel); |
114 |
|
115 |
//downloadFile(new URL(protocolHandler.getHost()), getHttpGetRequest(onlineResource), file, cancel);
|
116 |
} |
117 |
|
118 |
public void cancelDownload() { |
119 |
if(connection != null) { |
120 |
connection.disconnect(); |
121 |
connection = null;
|
122 |
} |
123 |
if(is != null) { |
124 |
try {
|
125 |
is.close(); |
126 |
} catch (IOException e) { |
127 |
} |
128 |
} |
129 |
if(dos != null) { |
130 |
try {
|
131 |
dos.close(); |
132 |
} catch (IOException e) { |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
HttpURLConnection connection = null; |
138 |
DataOutputStream dos = null; |
139 |
DataInputStream is = null; |
140 |
|
141 |
public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException { |
142 |
if(cancel != null && cancel.isCanceled()) |
143 |
throw new IOException(); |
144 |
|
145 |
Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" ); |
146 |
// by default 1 minute (60000 milliseconds.
|
147 |
int timeout = prefs.getInt("timeout", 20000); |
148 |
|
149 |
//If the used protocol is HTTPS
|
150 |
if (url.getProtocol().equals("https")) { |
151 |
try {
|
152 |
disableHttsValidation(); |
153 |
} catch (KeyManagementException e) { |
154 |
throw new IOException(); |
155 |
} catch (NoSuchAlgorithmException e) { |
156 |
throw new IOException(); |
157 |
} |
158 |
} |
159 |
|
160 |
|
161 |
|
162 |
try {
|
163 |
connection = (HttpURLConnection)url.openConnection();
|
164 |
connection.setConnectTimeout(timeout); |
165 |
connection.setReadTimeout(timeout); |
166 |
is = new DataInputStream(connection.getInputStream()); |
167 |
|
168 |
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile))); |
169 |
byte[] buffer = new byte[4096]; |
170 |
|
171 |
int i = -1; |
172 |
|
173 |
if(cancel != null && cancel.isCanceled()) { |
174 |
throw new IOException(); |
175 |
} |
176 |
|
177 |
while((i = is.read(buffer)) > 0) { |
178 |
if(cancel != null && cancel.isCanceled()) { |
179 |
throw new IOException(); |
180 |
} |
181 |
dos.write(buffer, 0, i);
|
182 |
} |
183 |
} catch (SocketTimeoutException e) { |
184 |
throw new IOException(); |
185 |
} finally {
|
186 |
if(is != null) |
187 |
is.close(); |
188 |
if(dos != null) |
189 |
dos.close(); |
190 |
if(connection != null) |
191 |
connection.disconnect(); |
192 |
} |
193 |
} |
194 |
|
195 |
/**
|
196 |
* This method disables the Https certificate validation.
|
197 |
* @throws KeyManagementException
|
198 |
* @throws NoSuchAlgorithmException
|
199 |
*/
|
200 |
private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{ |
201 |
// Create a trust manager that does not validate certificate chains
|
202 |
TrustManager[] trustAllCerts = new TrustManager[] { |
203 |
new X509TrustManager() { |
204 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
205 |
return null; |
206 |
} |
207 |
public void checkClientTrusted( |
208 |
java.security.cert.X509Certificate[] certs, String authType) { |
209 |
} |
210 |
public void checkServerTrusted( |
211 |
java.security.cert.X509Certificate[] certs, String authType) { |
212 |
} |
213 |
} |
214 |
}; |
215 |
|
216 |
// Install the all-trusting trust manager
|
217 |
SSLContext sc = SSLContext.getInstance("SSL"); |
218 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
219 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
220 |
} |
221 |
|
222 |
} |