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 @ 42140

History | View | Annotate | Download (5.17 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
    private int maxbytes = -1;
31

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

    
41
    public void setMaxbytes(int maxbytes) {
42
        this.maxbytes = maxbytes;
43
    }
44

    
45
    public int getMaxbytes() {
46
        return maxbytes;
47
    }
48

    
49
    
50
    public void run() {
51
        LOG.info("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath());
52
        if (data != null) {
53
            LOG.info("using POST, request = " + data);
54
        }
55
        // getting timeout from preferences in milliseconds.
56
        Preferences prefs = Preferences.userRoot().node("gvsig.downloader");
57
        // by default 1 minute (60000 milliseconds.
58
        int timeout = prefs.getInt("timeout", 60000);
59

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

    
85
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
86
            byte[] buffer = new byte[1024 * 4];
87

    
88
            long readed = 0;
89
            for (int i = is.read(buffer); !downloader.getCanceled(groupID) && i > 0; i = is.read(buffer)) {
90
                dos.write(buffer, 0, i);
91
                readed += i;
92
                if( maxbytes>0 && readed>maxbytes ) {
93
                    break;
94
                }
95

    
96
            }
97
            if (os != null) {
98
                os.close();
99
            }
100
            dos.close();
101
            is.close();
102
            is = null;
103
            dos = null;
104
            if (downloader.getCanceled(groupID)) {
105
                LOG.info("[RemoteServices] '" + url + "' CANCELED.");
106
                dstFile.delete();
107
                dstFile = null;
108
            } else {
109
                downloader.addDownloadedURL(url, dstFile.getAbsolutePath());
110
            }
111
        } catch (Exception e) {
112
            LOG.info("Error downloading", e);
113
            downloader.setDownloadException(e);
114
        }
115
    }
116

    
117
    /**
118
     * This method disables the Https certificate validation.
119
     *
120
     * @throws KeyManagementException
121
     * @throws NoSuchAlgorithmException
122
     */
123
    private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException {
124
        // Create a trust manager that does not validate certificate chains
125
        TrustManager[] trustAllCerts = new TrustManager[]{
126
            new X509TrustManager() {
127
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
128
                    return null;
129
                }
130

    
131
                public void checkClientTrusted(
132
                        java.security.cert.X509Certificate[] certs, String authType) {
133
                }
134

    
135
                public void checkServerTrusted(
136
                        java.security.cert.X509Certificate[] certs, String authType) {
137
                }
138
            }
139
        };
140

    
141
        // Install the all-trusting trust manager
142
        SSLContext sc = SSLContext.getInstance("SSL");
143
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
144
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
145
    }
146
}