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 @ 41528
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.setConnectTimeout(timeout); |
62 |
//If it uses a HTTP POST
|
63 |
if (data != null) { |
64 |
connection.setRequestProperty("SOAPAction", "post"); |
65 |
connection.setRequestMethod("POST");
|
66 |
connection.setDoOutput(true);
|
67 |
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
68 |
os = new OutputStreamWriter(connection.getOutputStream()); |
69 |
os.write(data); |
70 |
os.flush(); |
71 |
is = new DataInputStream(connection.getInputStream()); |
72 |
} else {
|
73 |
is = new DataInputStream(url.openStream()); |
74 |
} |
75 |
|
76 |
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile))); |
77 |
byte[] buffer = new byte[1024 * 4]; |
78 |
|
79 |
long readed = 0; |
80 |
for (int i = is.read(buffer); !downloader.getCanceled(groupID) && i > 0; i = is.read(buffer)) { |
81 |
dos.write(buffer, 0, i);
|
82 |
readed += i; |
83 |
|
84 |
} |
85 |
if (os != null) { |
86 |
os.close(); |
87 |
} |
88 |
dos.close(); |
89 |
is.close(); |
90 |
is = null;
|
91 |
dos = null;
|
92 |
if (downloader.getCanceled(groupID)) {
|
93 |
LOG.info("[RemoteServices] '" + url + "' CANCELED."); |
94 |
dstFile.delete(); |
95 |
dstFile = null;
|
96 |
} else {
|
97 |
downloader.addDownloadedURL(url, dstFile.getAbsolutePath()); |
98 |
} |
99 |
} catch (Exception e) { |
100 |
LOG.info("Error downloading", e);
|
101 |
downloader.setDownloadException(e); |
102 |
} |
103 |
} |
104 |
|
105 |
/**
|
106 |
* This method disables the Https certificate validation.
|
107 |
*
|
108 |
* @throws KeyManagementException
|
109 |
* @throws NoSuchAlgorithmException
|
110 |
*/
|
111 |
private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException { |
112 |
// Create a trust manager that does not validate certificate chains
|
113 |
TrustManager[] trustAllCerts = new TrustManager[]{ |
114 |
new X509TrustManager() { |
115 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
116 |
return null; |
117 |
} |
118 |
|
119 |
public void checkClientTrusted( |
120 |
java.security.cert.X509Certificate[] certs, String authType) { |
121 |
} |
122 |
|
123 |
public void checkServerTrusted( |
124 |
java.security.cert.X509Certificate[] certs, String authType) { |
125 |
} |
126 |
} |
127 |
}; |
128 |
|
129 |
// Install the all-trusting trust manager
|
130 |
SSLContext sc = SSLContext.getInstance("SSL"); |
131 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
132 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
133 |
} |
134 |
} |