Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / request / WMTSRequest.java @ 39504

History | View | Annotate | Download (6.64 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
                //System.out.println(url.toString());
114
                downloadFile(url, file, cancel);        
115
                
116
                //downloadFile(new URL(protocolHandler.getHost()), getHttpGetRequest(onlineResource), file, cancel);        
117
        }
118
        
119
        public void cancelDownload() {
120
                if(connection != null) {
121
                        connection.disconnect();
122
                        connection = null;        
123
                }
124
                if(is != null) {
125
                        try {
126
                                is.close();
127
                        } catch (IOException e) {
128
                        }
129
                }
130
                if(dos != null) {
131
                        try {
132
                                dos.close();
133
                        } catch (IOException e) {
134
                        }
135
                }
136
        }
137

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

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

    
162
                
163
                try {
164
                        connection = (HttpURLConnection)url.openConnection();
165
                        connection.setConnectTimeout(timeout);
166
                        connection.setReadTimeout(timeout);
167
                        is = new DataInputStream(connection.getInputStream());
168

    
169
                        dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
170
                        byte[] buffer = new byte[4096];
171

    
172
                        int i = -1;
173

    
174
                        if(cancel != null && cancel.isCanceled()) {
175
                                throw new IOException();
176
                        }
177

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

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

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