Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / request / WMTSRequest.java @ 37920

History | View | Annotate | Download (5.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.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.io.OutputStreamWriter;
43
import java.net.ConnectException;
44
import java.net.HttpURLConnection;
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
        
116
        public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException {
117
                if(cancel.isCanceled())
118
                        throw new IOException();
119
                Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" );
120
                // by default 1 minute (60000 milliseconds.
121
                int timeout = prefs.getInt("timeout", 15000);
122

    
123
                //If the used protocol is HTTPS
124
                if (url.getProtocol().equals("https")) {
125
                        try {
126
                                disableHttsValidation();
127
                        } catch (KeyManagementException e) {
128
                                throw new IOException();
129
                        } catch (NoSuchAlgorithmException e) {
130
                                throw new IOException();
131
                        }
132
                }
133
        
134
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
135
                connection.setConnectTimeout(timeout);
136
                DataInputStream is = new DataInputStream(connection.getInputStream());
137
                cancel.equals(is);
138

    
139
                DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
140
                byte[] buffer = new byte[1024 * 4];
141

    
142
                long readed = 0;
143
                int i = -1;
144
                
145
                if(cancel != null && cancel.isCanceled()) {
146
                        throw new IOException();
147
                }
148
                
149
                while((i = is.read(buffer)) > 0) {
150
                        if(cancel != null && cancel.isCanceled()) {
151
                                throw new IOException();
152
                        }
153
                        dos.write(buffer, 0, i);
154
                        readed += i;
155
                }
156
        
157
                is.close();
158
                dos.close();
159
                connection.disconnect();
160
        }
161

    
162
        /**
163
         * This method disables the Https certificate validation.
164
         * @throws KeyManagementException
165
         * @throws NoSuchAlgorithmException
166
         */
167
        private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{
168
                // Create a trust manager that does not validate certificate chains
169
                TrustManager[] trustAllCerts = new TrustManager[] {
170
                                new X509TrustManager() {
171
                                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
172
                                                return null;
173
                                        }
174
                                        public void checkClientTrusted(
175
                                                        java.security.cert.X509Certificate[] certs, String authType) {
176
                                        }
177
                                        public void checkServerTrusted(
178
                                                        java.security.cert.X509Certificate[] certs, String authType) {
179
                                        }
180
                                }
181
                };
182

    
183
                // Install the all-trusting trust manager
184
                SSLContext sc = SSLContext.getInstance("SSL");
185
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
186
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
187
        }
188
        
189
}