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 / AbstractSEDownloaderTask.java @ 47825
History | View | Annotate | Download (4.66 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.IOException; |
9 |
import java.io.InputStream; |
10 |
import java.io.OutputStreamWriter; |
11 |
import java.net.HttpURLConnection; |
12 |
import java.net.URL; |
13 |
import java.security.KeyManagementException; |
14 |
import java.security.NoSuchAlgorithmException; |
15 |
import java.util.prefs.Preferences; |
16 |
import javax.net.ssl.HttpsURLConnection; |
17 |
import javax.net.ssl.SSLContext; |
18 |
import javax.net.ssl.TrustManager; |
19 |
import javax.net.ssl.X509TrustManager; |
20 |
import org.apache.commons.io.IOUtils; |
21 |
import org.gvsig.compat.se.net.downloader.Downloader; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
@SuppressWarnings("UseSpecificCatch") |
26 |
public abstract class AbstractSEDownloaderTask implements Runnable { |
27 |
|
28 |
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSEDownloaderTask.class); |
29 |
|
30 |
protected final URL url; |
31 |
protected final File dstFile; |
32 |
protected final Object groupID; |
33 |
protected final String data; |
34 |
protected final SEDownloader downloader; |
35 |
|
36 |
private int maxbytes; |
37 |
|
38 |
public AbstractSEDownloaderTask(SEDownloader downloader, URL url, String data, File dstFile, Object groupID) { |
39 |
this.url = url;
|
40 |
this.data = data;
|
41 |
this.dstFile = dstFile;
|
42 |
this.groupID = groupID;
|
43 |
this.downloader = downloader;
|
44 |
this.maxbytes = -1; |
45 |
downloader.setDownloadException(null);
|
46 |
} |
47 |
|
48 |
public void setMaxbytes(int maxbytes) { |
49 |
this.maxbytes = maxbytes;
|
50 |
} |
51 |
|
52 |
public int getMaxbytes() { |
53 |
return maxbytes;
|
54 |
} |
55 |
|
56 |
protected void download(InputStream is) throws IOException { |
57 |
DataOutputStream dos = null; |
58 |
DataInputStream dis = null; |
59 |
FileOutputStream fos = null; |
60 |
BufferedOutputStream bos = null; |
61 |
try {
|
62 |
fos = new FileOutputStream(dstFile); |
63 |
if( is != null ) { |
64 |
bos = new BufferedOutputStream(fos); |
65 |
dos = new DataOutputStream(bos); |
66 |
|
67 |
dis = new DataInputStream(is); |
68 |
byte[] buffer = new byte[1024 * 4]; |
69 |
long readed = 0; |
70 |
for (int i = dis.read(buffer); !isCanceled() && i > 0; i = dis.read(buffer)) { |
71 |
dos.write(buffer, 0, i);
|
72 |
readed += i; |
73 |
if( maxbytes>0 && readed>maxbytes ) { |
74 |
break;
|
75 |
} |
76 |
} |
77 |
} |
78 |
} finally {
|
79 |
IOUtils.closeQuietly(dos); |
80 |
IOUtils.closeQuietly(bos); |
81 |
IOUtils.closeQuietly(fos); |
82 |
IOUtils.closeQuietly(dis); |
83 |
} |
84 |
} |
85 |
|
86 |
protected boolean isCanceled() { |
87 |
return downloader.getCanceled(groupID);
|
88 |
} |
89 |
|
90 |
protected int getTimeout() { |
91 |
// getting timeout from preferences in milliseconds.
|
92 |
Preferences prefs = Preferences.userRoot().node("gvsig.downloader"); |
93 |
// by default 1 minute (60000 milliseconds.
|
94 |
int timeout = prefs.getInt("timeout", 60000); |
95 |
|
96 |
return timeout;
|
97 |
} |
98 |
|
99 |
protected void postdownload() { |
100 |
if (isCanceled()) {
|
101 |
LOGGER.info("[Downloader] '" + url + "' CANCELED."); |
102 |
dstFile.delete(); |
103 |
// dstFile = null;
|
104 |
} else {
|
105 |
downloader.addDownloadedURL(url, dstFile.getAbsolutePath()); |
106 |
} |
107 |
} |
108 |
|
109 |
protected void exception(Exception e) { |
110 |
LOGGER.info("Error downloading", e);
|
111 |
downloader.setDownloadException(e); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* This method disables the Https certificate validation.
|
116 |
*
|
117 |
* @throws KeyManagementException
|
118 |
* @throws NoSuchAlgorithmException
|
119 |
*/
|
120 |
protected void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException { |
121 |
// Create a trust manager that does not validate certificate chains
|
122 |
TrustManager[] trustAllCerts = new TrustManager[]{ |
123 |
new X509TrustManager() { |
124 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
125 |
return null; |
126 |
} |
127 |
|
128 |
public void checkClientTrusted( |
129 |
java.security.cert.X509Certificate[] certs, String authType) { |
130 |
} |
131 |
|
132 |
public void checkServerTrusted( |
133 |
java.security.cert.X509Certificate[] certs, String authType) { |
134 |
} |
135 |
} |
136 |
}; |
137 |
|
138 |
// Install the all-trusting trust manager
|
139 |
SSLContext sc = SSLContext.getInstance("SSL"); |
140 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
141 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
142 |
} |
143 |
} |