Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wmts / request / WMTSRequest.java @ 40769

History | View | Annotate | Download (6.62 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
/**
26
 * This class sends a WMS request and returns the
27
 * reply in a local File. It tries if the server
28
 * supports both HTTP Get and Post requests.
29
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
30
 */
31
package org.gvsig.remoteclient.wmts.request;
32

    
33
import java.io.BufferedOutputStream;
34
import java.io.DataInputStream;
35
import java.io.DataOutputStream;
36
import java.io.File;
37
import java.io.FileOutputStream;
38
import java.io.IOException;
39
import java.net.ConnectException;
40
import java.net.HttpURLConnection;
41
import java.net.SocketTimeoutException;
42
import java.net.URL;
43
import java.net.UnknownHostException;
44
import java.security.KeyManagementException;
45
import java.security.NoSuchAlgorithmException;
46
import java.util.prefs.Preferences;
47

    
48
import javax.net.ssl.HttpsURLConnection;
49
import javax.net.ssl.SSLContext;
50
import javax.net.ssl.TrustManager;
51
import javax.net.ssl.X509TrustManager;
52

    
53
import org.gvsig.compat.CompatLocator;
54
import org.gvsig.compat.lang.StringUtils;
55
import org.gvsig.compat.net.ICancellable;
56
import org.gvsig.remoteclient.RemoteClientStatus;
57
import org.gvsig.remoteclient.ogc.request.OGCRequest;
58
import org.gvsig.remoteclient.wmts.WMTSProtocolHandler;
59
import org.gvsig.remoteclient.wmts.WMTSStatus;
60

    
61
/**
62
 * Base class for a WMTS request
63
 * @author Nacho Brodin (nachobrodin@gmail.com)
64
 */
65
public abstract class WMTSRequest extends OGCRequest {
66
        protected static final StringUtils stringUtils = CompatLocator.getStringUtils();
67
        
68
        public WMTSRequest(RemoteClientStatus status,
69
                        WMTSProtocolHandler protocolHandler) {
70
                super(status, protocolHandler);
71
        }
72

    
73
        /*
74
         * (non-Javadoc)
75
         * @see org.gvsig.remoteClient.wfs.requests.WFSRequest#getSchemaLocation()
76
         */
77
        protected String getSchemaLocation() {
78
                return null;
79
        }
80
        
81
        /**
82
     * Gets the part of the OGC request that share GetMap and GetFeatureInfo
83
     * @return String request
84
     */
85
        protected String getPartialQuery(WMTSStatus status) {
86
        StringBuffer req = new StringBuffer();
87
        req.append("Layer=" + status.getLayer())
88
        .append("&Style=" + status.getStyle())
89
        .append("&Format=" + status.getFormat())
90
        .append("&TileMatrixSet=" + status.getTileMatrixSet())
91
        .append("&TileMatrix=" + status.getTileMatrix())
92
        .append("&TileRow=" + status. getTileRow())
93
        .append("&TileCol=" + status.getTileCol());
94
        return req.toString();
95
    }
96
        
97
        /**
98
         * Send a request to the server.
99
         * @return
100
         * The server reply
101
         * @throws IOException 
102
         * @throws UnknownHostException 
103
         * @throws ConnectException 
104
         */
105
        public void sendRequest(ICancellable cancel, File file) throws ConnectException, UnknownHostException, IOException {
106
                String onlineResource = protocolHandler.getHost();
107
                String symbol = getSymbol(onlineResource);
108
                onlineResource = onlineResource + symbol;
109
                URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20"));
110
                //System.out.println(url.toString());
111
                downloadFile(url, file, cancel);        
112
                
113
                //downloadFile(new URL(protocolHandler.getHost()), getHttpGetRequest(onlineResource), file, cancel);        
114
        }
115
        
116
        public void cancelDownload() {
117
                if(connection != null) {
118
                        connection.disconnect();
119
                        connection = null;        
120
                }
121
                if(is != null) {
122
                        try {
123
                                is.close();
124
                        } catch (IOException e) {
125
                        }
126
                }
127
                if(dos != null) {
128
                        try {
129
                                dos.close();
130
                        } catch (IOException e) {
131
                        }
132
                }
133
        }
134

    
135
        HttpURLConnection connection = null;
136
        DataOutputStream dos = null;
137
        DataInputStream is = null;
138
        
139
        public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException {
140
                if(cancel != null && cancel.isCanceled())
141
                        throw new IOException();
142
                
143
                Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" );
144
                // by default 1 minute (60000 milliseconds.
145
                int timeout = prefs.getInt("timeout", 20000);
146

    
147
                //If the used protocol is HTTPS
148
                if (url.getProtocol().equals("https")) {
149
                        try {
150
                                disableHttsValidation();
151
                        } catch (KeyManagementException e) {
152
                                throw new IOException();
153
                        } catch (NoSuchAlgorithmException e) {
154
                                throw new IOException();
155
                        }
156
                }
157
        
158

    
159
                
160
                try {
161
                        connection = (HttpURLConnection)url.openConnection();
162
                        connection.setConnectTimeout(timeout);
163
                        connection.setReadTimeout(timeout);
164
                        is = new DataInputStream(connection.getInputStream());
165

    
166
                        dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
167
                        byte[] buffer = new byte[4096];
168

    
169
                        int i = -1;
170

    
171
                        if(cancel != null && cancel.isCanceled()) {
172
                                throw new IOException();
173
                        }
174

    
175
                        while((i = is.read(buffer)) > 0) {
176
                                if(cancel != null && cancel.isCanceled()) {
177
                                        throw new IOException();
178
                                }
179
                                dos.write(buffer, 0, i);
180
                        }
181
                } catch (SocketTimeoutException e) {
182
                        throw new IOException();
183
                } finally {
184
                        if(is != null)
185
                                is.close();
186
                        if(dos != null)
187
                                dos.close();
188
                        if(connection != null)
189
                                connection.disconnect();
190
                }
191
        }
192

    
193
        /**
194
         * This method disables the Https certificate validation.
195
         * @throws KeyManagementException
196
         * @throws NoSuchAlgorithmException
197
         */
198
        private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{
199
                // Create a trust manager that does not validate certificate chains
200
                TrustManager[] trustAllCerts = new TrustManager[] {
201
                                new X509TrustManager() {
202
                                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
203
                                                return null;
204
                                        }
205
                                        public void checkClientTrusted(
206
                                                        java.security.cert.X509Certificate[] certs, String authType) {
207
                                        }
208
                                        public void checkServerTrusted(
209
                                                        java.security.cert.X509Certificate[] certs, String authType) {
210
                                        }
211
                                }
212
                };
213

    
214
                // Install the all-trusting trust manager
215
                SSLContext sc = SSLContext.getInstance("SSL");
216
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
217
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
218
        }
219
        
220
}