Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.compat / org.gvsig.compat.se / src / main / java / org / gvsig / compat / se / net / downloader / se / SEDownloaderTask.java @ 41851

History | View | Annotate | Download (4.89 KB)

1
package org.gvsig.compat.se.net.downloader.se;
2

    
3
import java.io.BufferedOutputStream;
4
import java.io.DataInputStream;
5
import java.io.DataOutputStream;
6
import java.io.File;
7
import java.io.FileOutputStream;
8
import java.io.OutputStreamWriter;
9
import java.net.HttpURLConnection;
10
import java.net.URL;
11
import java.security.KeyManagementException;
12
import java.security.NoSuchAlgorithmException;
13
import java.util.prefs.Preferences;
14
import javax.net.ssl.HttpsURLConnection;
15
import javax.net.ssl.SSLContext;
16
import javax.net.ssl.TrustManager;
17
import javax.net.ssl.X509TrustManager;
18
import org.gvsig.compat.se.net.downloader.Downloader;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
final class SEDownloaderTask implements Runnable {
23

    
24
    private URL url = null;
25
    private File dstFile = null;
26
    private Object groupID = null;
27
    private String data = null;
28
    private Downloader downloader = null;
29
    private static Logger LOG = LoggerFactory.getLogger(SEDownloaderTask.class);
30

    
31
    public SEDownloaderTask(Downloader downloader, URL url, String data, File dstFile, Object groupID) {
32
        this.url = url;
33
        this.data = data;
34
        this.dstFile = dstFile;
35
        this.groupID = groupID;
36
        this.downloader = downloader;
37
        downloader.setDownloadException(null);
38
    }
39

    
40
    public void run() {
41
        LOG.info("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath());
42
        if (data != null) {
43
            LOG.info("using POST, request = " + data);
44
        }
45
        // getting timeout from preferences in milliseconds.
46
        Preferences prefs = Preferences.userRoot().node("gvsig.downloader");
47
        // by default 1 minute (60000 milliseconds.
48
        int timeout = prefs.getInt("timeout", 60000);
49

    
50
        DataOutputStream dos;
51
        try {
52
            DataInputStream is;
53
            OutputStreamWriter os = null;
54
            HttpURLConnection connection = null;
55
            //If the used protocol is HTTPS
56
            if (url.getProtocol().equals("https")) {
57
                disableHttsValidation();
58
            }
59
            connection = (HttpURLConnection) url.openConnection();
60
            connection.setUseCaches(false);
61
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (gvSIG) like Gecko");
62
            connection.setConnectTimeout(timeout);
63
            //If it uses a HTTP POST
64
            if (data != null) {
65
                connection.setRequestProperty("SOAPAction", "post");
66
                connection.setRequestMethod("POST");
67
                connection.setDoOutput(true);
68
                connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
69
                os = new OutputStreamWriter(connection.getOutputStream());
70
                os.write(data);
71
                os.flush();
72
            }
73
            is = new DataInputStream(connection.getInputStream());
74

    
75
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
76
            byte[] buffer = new byte[1024 * 4];
77

    
78
            long readed = 0;
79
            for (int i = is.read(buffer); !downloader.getCanceled(groupID) && i > 0; i = is.read(buffer)) {
80
                dos.write(buffer, 0, i);
81
                readed += i;
82

    
83
            }
84
            if (os != null) {
85
                os.close();
86
            }
87
            dos.close();
88
            is.close();
89
            is = null;
90
            dos = null;
91
            if (downloader.getCanceled(groupID)) {
92
                LOG.info("[RemoteServices] '" + url + "' CANCELED.");
93
                dstFile.delete();
94
                dstFile = null;
95
            } else {
96
                downloader.addDownloadedURL(url, dstFile.getAbsolutePath());
97
            }
98
        } catch (Exception e) {
99
            LOG.info("Error downloading", e);
100
            downloader.setDownloadException(e);
101
        }
102
    }
103

    
104
    /**
105
     * This method disables the Https certificate validation.
106
     *
107
     * @throws KeyManagementException
108
     * @throws NoSuchAlgorithmException
109
     */
110
    private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException {
111
        // Create a trust manager that does not validate certificate chains
112
        TrustManager[] trustAllCerts = new TrustManager[]{
113
            new X509TrustManager() {
114
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
115
                    return null;
116
                }
117

    
118
                public void checkClientTrusted(
119
                        java.security.cert.X509Certificate[] certs, String authType) {
120
                }
121

    
122
                public void checkServerTrusted(
123
                        java.security.cert.X509Certificate[] certs, String authType) {
124
                }
125
            }
126
        };
127

    
128
        // Install the all-trusting trust manager
129
        SSLContext sc = SSLContext.getInstance("SSL");
130
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
131
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
132
    }
133
}