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 / httpclient / HttpClientDownloaderTask.java @ 41528
History | View | Annotate | Download (4.26 KB)
1 | 41528 | jjdelcerro | |
---|---|---|---|
2 | |||
3 | package org.gvsig.compat.se.net.downloader.httpclient; |
||
4 | |||
5 | import org.gvsig.compat.se.net.downloader.se.*; |
||
6 | import java.io.BufferedOutputStream; |
||
7 | import java.io.DataInputStream; |
||
8 | import java.io.DataOutputStream; |
||
9 | import java.io.File; |
||
10 | import java.io.FileOutputStream; |
||
11 | import java.io.OutputStreamWriter; |
||
12 | import java.net.HttpURLConnection; |
||
13 | import java.net.URL; |
||
14 | import java.security.KeyManagementException; |
||
15 | import java.security.NoSuchAlgorithmException; |
||
16 | import java.util.prefs.Preferences; |
||
17 | import javax.net.ssl.HttpsURLConnection; |
||
18 | import javax.net.ssl.SSLContext; |
||
19 | import javax.net.ssl.TrustManager; |
||
20 | import javax.net.ssl.X509TrustManager; |
||
21 | import org.gvsig.compat.se.net.downloader.Downloader; |
||
22 | import org.slf4j.Logger; |
||
23 | import org.slf4j.LoggerFactory; |
||
24 | |||
25 | |||
26 | final class HttpClientDownloaderTask implements Runnable { |
||
27 | private URL url = null; |
||
28 | private File dstFile = null; |
||
29 | private Object groupID = null; |
||
30 | private String data = null; |
||
31 | private Downloader downloader = null; |
||
32 | private static Logger LOG = LoggerFactory.getLogger(HttpClientDownloaderTask.class); |
||
33 | |||
34 | |||
35 | public HttpClientDownloaderTask(Downloader downloader, URL url, String data, File dstFile, Object groupID) { |
||
36 | this.url = url;
|
||
37 | this.data = data;
|
||
38 | this.dstFile = dstFile;
|
||
39 | this.groupID = groupID;
|
||
40 | this.downloader = downloader;
|
||
41 | downloader.setDownloadException(null);
|
||
42 | } |
||
43 | |||
44 | public void run() { |
||
45 | LOG.info("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath()); |
||
46 | if (data != null){ |
||
47 | LOG.info("using POST, request = " + data);
|
||
48 | } |
||
49 | // getting timeout from preferences in milliseconds.
|
||
50 | Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" ); |
||
51 | // by default 1 minute (60000 milliseconds.
|
||
52 | int timeout = prefs.getInt("timeout", 60000); |
||
53 | |||
54 | DataOutputStream dos;
|
||
55 | try {
|
||
56 | DataInputStream is;
|
||
57 | OutputStreamWriter os = null; |
||
58 | HttpURLConnection connection = null; |
||
59 | //If the used protocol is HTTPS
|
||
60 | if (url.getProtocol().equals("https")) { |
||
61 | disableHttsValidation(); |
||
62 | } |
||
63 | connection = (HttpURLConnection)url.openConnection();
|
||
64 | connection.setUseCaches(false);
|
||
65 | connection.setConnectTimeout(timeout); |
||
66 | //If it uses a HTTP POST
|
||
67 | if (data != null) { |
||
68 | connection.setRequestProperty("SOAPAction", "post"); |
||
69 | connection.setRequestMethod("POST");
|
||
70 | connection.setDoOutput(true);
|
||
71 | connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
||
72 | os = new OutputStreamWriter(connection.getOutputStream()); |
||
73 | os.write(data); |
||
74 | os.flush(); |
||
75 | is = new DataInputStream(connection.getInputStream()); |
||
76 | }else{
|
||
77 | is = new DataInputStream(url.openStream()); |
||
78 | } |
||
79 | |||
80 | dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(dstFile))); |
||
81 | byte[] buffer = new byte[1024 * 4]; |
||
82 | |||
83 | |||
84 | long readed = 0; |
||
85 | for (int i = is.read(buffer); !downloader.getCanceled(groupID) && i>0; i = is.read(buffer)){ |
||
86 | dos.write(buffer, 0, i);
|
||
87 | readed += i; |
||
88 | |||
89 | } |
||
90 | if(os != null) { |
||
91 | os.close(); |
||
92 | } |
||
93 | dos.close(); |
||
94 | is.close(); |
||
95 | is = null;
|
||
96 | dos = null;
|
||
97 | if (downloader.getCanceled(groupID)) {
|
||
98 | LOG.info("[RemoteServices] '" + url + "' CANCELED."); |
||
99 | dstFile.delete(); |
||
100 | dstFile = null;
|
||
101 | } else {
|
||
102 | downloader.addDownloadedURL(url, dstFile.getAbsolutePath()); |
||
103 | } |
||
104 | } catch (Exception e) { |
||
105 | LOG.info("Error downloading", e);
|
||
106 | downloader.setDownloadException(e); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /**
|
||
111 | * This method disables the Https certificate validation.
|
||
112 | * @throws KeyManagementException
|
||
113 | * @throws NoSuchAlgorithmException
|
||
114 | */
|
||
115 | private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{ |
||
116 | // Create a trust manager that does not validate certificate chains
|
||
117 | TrustManager[] trustAllCerts = new TrustManager[] { |
||
118 | new X509TrustManager() { |
||
119 | public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
||
120 | return null; |
||
121 | } |
||
122 | public void checkClientTrusted( |
||
123 | java.security.cert.X509Certificate[] certs, String authType) { |
||
124 | } |
||
125 | public void checkServerTrusted( |
||
126 | java.security.cert.X509Certificate[] certs, String authType) { |
||
127 | } |
||
128 | } |
||
129 | }; |
||
130 | |||
131 | // Install the all-trusting trust manager
|
||
132 | SSLContext sc = SSLContext.getInstance("SSL"); |
||
133 | sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
||
134 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
||
135 | } |
||
136 | } |