Revision 41528
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.se/src/main/java/org/gvsig/compat/se/net/SEDownloader.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.compat.se.net; |
|
25 |
|
|
26 |
import java.io.BufferedOutputStream; |
|
27 |
import java.io.DataInputStream; |
|
28 |
import java.io.DataOutputStream; |
|
29 |
import java.io.File; |
|
30 |
import java.io.FileNotFoundException; |
|
31 |
import java.io.FileOutputStream; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.OutputStreamWriter; |
|
34 |
import java.net.ConnectException; |
|
35 |
import java.net.HttpURLConnection; |
|
36 |
import java.net.URI; |
|
37 |
import java.net.URL; |
|
38 |
import java.net.UnknownHostException; |
|
39 |
import java.security.KeyManagementException; |
|
40 |
import java.security.NoSuchAlgorithmException; |
|
41 |
import java.util.Hashtable; |
|
42 |
import java.util.prefs.Preferences; |
|
43 |
|
|
44 |
import javax.net.ssl.HttpsURLConnection; |
|
45 |
import javax.net.ssl.SSLContext; |
|
46 |
import javax.net.ssl.TrustManager; |
|
47 |
import javax.net.ssl.X509TrustManager; |
|
48 |
|
|
49 |
import org.slf4j.Logger; |
|
50 |
import org.slf4j.LoggerFactory; |
|
51 |
|
|
52 |
import org.gvsig.compat.net.ICancellable; |
|
53 |
|
|
54 |
|
|
55 |
/** |
|
56 |
* Clase con m?todos de utilidad en el protocolo WMS |
|
57 |
* |
|
58 |
* @authors Laura D?az, jaume dominguez faus |
|
59 |
*/ |
|
60 |
public class SEDownloader implements org.gvsig.compat.net.Downloader{ |
|
61 |
String characters; |
|
62 |
boolean canceled; |
|
63 |
final long latency = 500; |
|
64 |
private static int count = 0; |
|
65 |
private static Logger LOG = LoggerFactory.getLogger(SEDownloader.class); |
|
66 |
|
|
67 |
/** |
|
68 |
* Used to cancel a group of files |
|
69 |
* <b>key</b>: Group id, <b>value</b>: Boolean (true if |
|
70 |
* the group has to be canceled. Otherwise it is |
|
71 |
* false) |
|
72 |
*/ |
|
73 |
Hashtable canceledGroup = new Hashtable(); |
|
74 |
/** |
|
75 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file. |
|
76 |
*/ |
|
77 |
Hashtable cachedURItoFile; |
|
78 |
Exception downloadException; |
|
79 |
final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
|
80 |
|
|
81 |
public SEDownloader() { |
|
82 |
super(); |
|
83 |
characters = ""; |
|
84 |
for (int j = 32; j<=127; j++){ |
|
85 |
characters += (char) j; |
|
86 |
} |
|
87 |
characters += "?????????????????????????????????????????????????\n\r\f\t??"; |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Return the content of a file that has been created |
|
92 |
* from a URL using the HTTP GET protocol |
|
93 |
* @param url |
|
94 |
* The URL |
|
95 |
* @return |
|
96 |
* File containing this URL's content or null if no file was found. |
|
97 |
*/ |
|
98 |
private File getPreviousDownloadedURL(URL url){ |
|
99 |
return getPreviousDownloaded(url); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* Return the content of a file that has been created |
|
104 |
* from a URL using the HTTP POST protocol |
|
105 |
* @param url |
|
106 |
* The URL |
|
107 |
* @param data |
|
108 |
* The data to send on the query |
|
109 |
* @return |
|
110 |
* File containing this URL's content or null if no file was found. |
|
111 |
*/ |
|
112 |
private File getPreviousDownloadedURL(URL url, String data) { |
|
113 |
if(data == null) |
|
114 |
return getPreviousDownloaded(url); |
|
115 |
return getPreviousDownloaded(url.toString() + data); |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* Returns the content of a URL as a file from the file system.<br> |
|
120 |
* <p> |
|
121 |
* If the URL has been already downloaded in this session and notified |
|
122 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b> |
|
123 |
* method, it can be restored faster from the file system avoiding to |
|
124 |
* download it again. |
|
125 |
* </p> |
|
126 |
* @param url |
|
127 |
* @return File containing this URL's content or null if no file was found. |
|
128 |
*/ |
|
129 |
private File getPreviousDownloaded(Object theurl) { |
|
130 |
|
|
131 |
File f = null; |
|
132 |
Object thekey = null; |
|
133 |
try { |
|
134 |
if (theurl instanceof URL) { |
|
135 |
thekey = ((URL) theurl).toURI(); |
|
136 |
} else { |
|
137 |
thekey = theurl; |
|
138 |
} |
|
139 |
} catch (Exception e) { |
|
140 |
LOG.info("Warning: did not check url: " + theurl, e); |
|
141 |
return null; |
|
142 |
} |
|
143 |
|
|
144 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(thekey)) { |
|
145 |
String filePath = (String) cachedURItoFile.get(thekey); |
|
146 |
f = new File(filePath); |
|
147 |
if (!f.exists()) |
|
148 |
return null; |
|
149 |
} |
|
150 |
return f; |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* Adds an URL to the table of downloaded files for further uses. If the URL |
|
155 |
* already exists in the table its filePath value is updated to the new one and |
|
156 |
* the old file itself is removed from the file system. |
|
157 |
* |
|
158 |
* @param url |
|
159 |
* @param filePath |
|
160 |
*/ |
|
161 |
void addDownloadedURL(URL url, String filePath) { |
|
162 |
if (cachedURItoFile == null) |
|
163 |
cachedURItoFile = new Hashtable(); |
|
164 |
|
|
165 |
URI theuri = null; |
|
166 |
try { |
|
167 |
theuri = url.toURI(); |
|
168 |
} catch (Exception e) { |
|
169 |
LOG.info("Warning: did not cache bad url: " + url, e); |
|
170 |
return; |
|
171 |
} |
|
172 |
|
|
173 |
String fileName = (String) cachedURItoFile.put(theuri, filePath); |
|
174 |
//JMV: No se puede eliminar el anterior porque puede que alguien lo |
|
175 |
// este usando |
|
176 |
/* |
|
177 |
if (fileName!=null){ |
|
178 |
File f = new File(fileName); |
|
179 |
if (f.exists()) |
|
180 |
f.delete(); |
|
181 |
} |
|
182 |
*/ |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Downloads an URL into a temporary file that is removed the next time the |
|
187 |
* tempFileManager class is called, which means the next time gvSIG is launched. |
|
188 |
* |
|
189 |
* @param url |
|
190 |
* @param name |
|
191 |
* @return |
|
192 |
* @throws IOException |
|
193 |
* @throws ServerErrorResponseException |
|
194 |
* @throws ConnectException |
|
195 |
* @throws UnknownHostException |
|
196 |
*/ |
|
197 |
public synchronized File downloadFile(URL url, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException { |
|
198 |
File f = null; |
|
199 |
|
|
200 |
if ((f = getPreviousDownloadedURL(url)) == null) { |
|
201 |
File tempDirectory = new File(tempDirectoryPath); |
|
202 |
if (!tempDirectory.exists()) |
|
203 |
tempDirectory.mkdir(); |
|
204 |
|
|
205 |
f = new File(calculateFileName(name)); |
|
206 |
|
|
207 |
if (cancel == null) { |
|
208 |
cancel = new ICancellable() { |
|
209 |
public boolean isCanceled() { |
|
210 |
return false; |
|
211 |
} |
|
212 |
public Object getID(){ |
|
213 |
return SEDownloader.class.getName(); |
|
214 |
} |
|
215 |
}; |
|
216 |
} |
|
217 |
|
|
218 |
Monitor monitorObj = new Monitor(this, cancel); |
|
219 |
Thread downloader = new Thread(new Downloader(this, url, f, cancel.getID())); |
|
220 |
Thread monitor = new Thread(monitorObj); |
|
221 |
|
|
222 |
monitor.start(); |
|
223 |
downloader.start(); |
|
224 |
while(!getCanceled(cancel.getID()) && downloader.isAlive()) { |
|
225 |
try { |
|
226 |
Thread.sleep(latency); |
|
227 |
} catch (InterruptedException e) { |
|
228 |
LOG.error("Error", e); |
|
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
try { |
|
233 |
monitorObj.setFinish(true); |
|
234 |
monitor.join(); |
|
235 |
downloader.join(); |
|
236 |
} catch (InterruptedException e1) { |
|
237 |
LOG.warn(e1.getMessage()); |
|
238 |
} |
|
239 |
downloader = null; |
|
240 |
monitor = null; |
|
241 |
|
|
242 |
if (getCanceled(cancel.getID())) |
|
243 |
return null; |
|
244 |
downloader = null; |
|
245 |
monitor = null; |
|
246 |
if (this.downloadException != null) { |
|
247 |
Exception e = this.downloadException; |
|
248 |
if (e instanceof FileNotFoundException) |
|
249 |
throw (IOException) e; |
|
250 |
else if (e instanceof IOException) |
|
251 |
throw (IOException) e; |
|
252 |
else if (e instanceof ConnectException) |
|
253 |
throw (ConnectException) e; |
|
254 |
else if (e instanceof UnknownHostException) |
|
255 |
throw (UnknownHostException) e; |
|
256 |
} |
|
257 |
} else { |
|
258 |
LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'"); |
|
259 |
} |
|
260 |
|
|
261 |
return f; |
|
262 |
} |
|
263 |
|
|
264 |
private String calculateFileName(String name) { |
|
265 |
count ++; |
|
266 |
int index = name.lastIndexOf("."); |
|
267 |
if (index > 0){ |
|
268 |
return tempDirectoryPath + "/" + name.substring(0,index) + System.currentTimeMillis() + count + |
|
269 |
name.substring(index, name.length()); |
|
270 |
} |
|
271 |
return tempDirectoryPath + "/" + name + System.currentTimeMillis() + count; |
|
272 |
} |
|
273 |
|
|
274 |
|
|
275 |
/** |
|
276 |
* Downloads a URL using the HTTP Post protocol |
|
277 |
* @param url |
|
278 |
* The server URL |
|
279 |
* @param data |
|
280 |
* The data to send in the request |
|
281 |
* @param name |
|
282 |
* A common name for all the retrieved files |
|
283 |
* @param cancel |
|
284 |
* Used to cancel the downloads |
|
285 |
* @return |
|
286 |
* The retrieved file |
|
287 |
* @throws IOException |
|
288 |
* @throws ConnectException |
|
289 |
* @throws UnknownHostException |
|
290 |
*/ |
|
291 |
public synchronized File downloadFile(URL url, String data, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{ |
|
292 |
File f = null; |
|
293 |
|
|
294 |
if ((f = getPreviousDownloadedURL(url,data)) == null) { |
|
295 |
File tempDirectory = new File(tempDirectoryPath); |
|
296 |
if (!tempDirectory.exists()) |
|
297 |
tempDirectory.mkdir(); |
|
298 |
|
|
299 |
f = new File(calculateFileName(name)); |
|
300 |
|
|
301 |
if (cancel == null) { |
|
302 |
cancel = new ICancellable() { |
|
303 |
public boolean isCanceled() { |
|
304 |
return false; |
|
305 |
} |
|
306 |
public Object getID(){ |
|
307 |
return SEDownloader.class.getName(); |
|
308 |
} |
|
309 |
}; |
|
310 |
} |
|
311 |
Monitor monitorObj = new Monitor(this, cancel); |
|
312 |
Thread downloader = new Thread(new Downloader(this, url, data, f, cancel.getID())); |
|
313 |
Thread monitor = new Thread(monitorObj); |
|
314 |
monitor.start(); |
|
315 |
downloader.start(); |
|
316 |
while(!getCanceled(cancel.getID()) && downloader.isAlive()) { |
|
317 |
try { |
|
318 |
Thread.sleep(latency); |
|
319 |
} catch (InterruptedException e) { |
|
320 |
LOG.error("Error", e); |
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
try { |
|
325 |
monitorObj.setFinish(true); |
|
326 |
monitor.join(); |
|
327 |
downloader.join(); |
|
328 |
} catch (InterruptedException e1) { |
|
329 |
LOG.warn(e1.getMessage()); |
|
330 |
} |
|
331 |
downloader = null; |
|
332 |
monitor = null; |
|
333 |
|
|
334 |
if (getCanceled(cancel.getID())) |
|
335 |
return null; |
|
336 |
if (this.downloadException!=null) { |
|
337 |
Exception e = this.downloadException; |
|
338 |
if (e instanceof FileNotFoundException) |
|
339 |
throw (IOException) e; |
|
340 |
else if (e instanceof IOException) |
|
341 |
throw (IOException) e; |
|
342 |
else if (e instanceof ConnectException) |
|
343 |
throw (ConnectException) e; |
|
344 |
else if (e instanceof UnknownHostException) |
|
345 |
throw (UnknownHostException) e; |
|
346 |
} |
|
347 |
} else { |
|
348 |
LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'"); |
|
349 |
} |
|
350 |
|
|
351 |
return f; |
|
352 |
} |
|
353 |
|
|
354 |
/** |
|
355 |
* Try if a group of downloads has been canceled |
|
356 |
* @param groupId |
|
357 |
* Group id |
|
358 |
* @return |
|
359 |
* If the group has been canceled |
|
360 |
*/ |
|
361 |
protected boolean getCanceled(Object groupId) { |
|
362 |
Object obj = canceledGroup.get(groupId); |
|
363 |
if (obj != null) { |
|
364 |
return ((Boolean)obj).booleanValue(); |
|
365 |
} |
|
366 |
return false; |
|
367 |
} |
|
368 |
|
|
369 |
/** |
|
370 |
* Cancel a group of downloads |
|
371 |
* @param groupId |
|
372 |
* Group id |
|
373 |
* @param isCanceled |
|
374 |
* if the group has to be canceled |
|
375 |
*/ |
|
376 |
protected void setCanceled(Object groupId, boolean isCanceled) { |
|
377 |
if (groupId == null) { |
|
378 |
groupId = SEDownloader.class.getName(); |
|
379 |
} |
|
380 |
canceledGroup.put(groupId,new Boolean(isCanceled)); |
|
381 |
} |
|
382 |
|
|
383 |
/** |
|
384 |
* Cleans every temporal file previously downloaded. |
|
385 |
*/ |
|
386 |
public void cleanUpTempFiles() { |
|
387 |
try{ |
|
388 |
File tempDirectory = new File(tempDirectoryPath); |
|
389 |
|
|
390 |
File[] files = tempDirectory.listFiles(); |
|
391 |
if (files!=null) { |
|
392 |
for (int i = 0; i < files.length; i++) { |
|
393 |
// s?lo por si en un futuro se necesitan crear directorios temporales |
|
394 |
if (files[i].isDirectory()) deleteDirectory(files[i]); |
|
395 |
files[i].delete(); |
|
396 |
} |
|
397 |
} |
|
398 |
tempDirectory.delete(); |
|
399 |
} catch (Exception e) { } |
|
400 |
|
|
401 |
} |
|
402 |
/** |
|
403 |
* Recursive directory delete. |
|
404 |
* @param f |
|
405 |
*/ |
|
406 |
private void deleteDirectory(File f) { |
|
407 |
File[] files = f.listFiles(); |
|
408 |
for (int i = 0; i < files.length; i++) { |
|
409 |
if (files[i].isDirectory()) |
|
410 |
deleteDirectory(files[i]); |
|
411 |
files[i].delete(); |
|
412 |
} |
|
413 |
|
|
414 |
} |
|
415 |
|
|
416 |
|
|
417 |
/** |
|
418 |
* Remove an URL from the system cache. The file will remain in the file |
|
419 |
* system for further eventual uses. |
|
420 |
* @param request |
|
421 |
*/ |
|
422 |
public void removeURL(URL url) { |
|
423 |
|
|
424 |
URI theuri = null; |
|
425 |
|
|
426 |
try { |
|
427 |
theuri = url.toURI(); |
|
428 |
} catch (Exception e) { |
|
429 |
LOG.info("Warning: did not remove bad url: " + url, e); |
|
430 |
return; |
|
431 |
} |
|
432 |
|
|
433 |
|
|
434 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(theuri)) |
|
435 |
cachedURItoFile.remove(theuri); |
|
436 |
} |
|
437 |
|
|
438 |
/** |
|
439 |
* Remove an URL from the system cache. The file will remain in the file |
|
440 |
* system for further eventual uses. |
|
441 |
* @param request |
|
442 |
*/ |
|
443 |
public void removeURL(Object url) { |
|
444 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(url)) |
|
445 |
cachedURItoFile.remove(url); |
|
446 |
} |
|
447 |
} |
|
448 |
|
|
449 |
final class Monitor implements Runnable { |
|
450 |
ICancellable c; |
|
451 |
private volatile boolean finish = false; |
|
452 |
SEDownloader downloader; |
|
453 |
private static Logger LOG = LoggerFactory.getLogger(Monitor.class); |
|
454 |
|
|
455 |
public Monitor(SEDownloader downloader, ICancellable cancel) { |
|
456 |
downloader.setCanceled(cancel.getID(),false); |
|
457 |
this.c = cancel; |
|
458 |
this.downloader = downloader; |
|
459 |
} |
|
460 |
|
|
461 |
public void run() { |
|
462 |
while (!c.isCanceled() && !getFinish()) { |
|
463 |
try { |
|
464 |
Thread.sleep(downloader.latency); |
|
465 |
} catch (InterruptedException e) { |
|
466 |
LOG.error("Error", e); |
|
467 |
} |
|
468 |
} |
|
469 |
|
|
470 |
/* WARNING!! This works because only one download is being processed at once. |
|
471 |
* You could prefer to start several transfers simultaneously. If so, you |
|
472 |
* should consideer using a non-static variable such is Utilities.canceled to |
|
473 |
* control when and which transfer in particular has been canceled. |
|
474 |
* |
|
475 |
* The feature of transfer several files is at the moment under study. We are |
|
476 |
* planning to add an intelligent system that will give you a lot of services |
|
477 |
* and ease-of-use. So, we encourage you to wait for it instead of write your |
|
478 |
* own code. |
|
479 |
*/ |
|
480 |
if(c.isCanceled()) |
|
481 |
downloader.setCanceled(c.getID(), true); |
|
482 |
} |
|
483 |
|
|
484 |
public synchronized void setFinish(boolean value) { |
|
485 |
finish = value; |
|
486 |
} |
|
487 |
|
|
488 |
public synchronized boolean getFinish() { |
|
489 |
return finish; |
|
490 |
} |
|
491 |
} |
|
492 |
|
|
493 |
final class Downloader implements Runnable { |
|
494 |
private URL url = null; |
|
495 |
private File dstFile = null; |
|
496 |
private Object groupID = null; |
|
497 |
private String data = null; |
|
498 |
private SEDownloader downloader = null; |
|
499 |
private static Logger LOG = LoggerFactory.getLogger(Downloader.class); |
|
500 |
|
|
501 |
public Downloader(SEDownloader downloader, URL url, File dstFile, Object groupID) { |
|
502 |
this.url = url; |
|
503 |
this.dstFile = dstFile; |
|
504 |
this.groupID = groupID; |
|
505 |
this.downloader = downloader; |
|
506 |
downloader.downloadException = null; |
|
507 |
} |
|
508 |
|
|
509 |
public Downloader(SEDownloader downloader, URL url, String data, File dstFile, Object groupID) { |
|
510 |
this.url = url; |
|
511 |
this.data = data; |
|
512 |
this.dstFile = dstFile; |
|
513 |
this.groupID = groupID; |
|
514 |
this.downloader = downloader; |
|
515 |
downloader.downloadException = null; |
|
516 |
} |
|
517 |
|
|
518 |
public void run() { |
|
519 |
LOG.info("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath()); |
|
520 |
if (data != null){ |
|
521 |
LOG.info("using POST, request = " + data); |
|
522 |
} |
|
523 |
// getting timeout from preferences in milliseconds. |
|
524 |
Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" ); |
|
525 |
// by default 1 minute (60000 milliseconds. |
|
526 |
int timeout = prefs.getInt("timeout", 60000); |
|
527 |
|
|
528 |
DataOutputStream dos; |
|
529 |
try { |
|
530 |
DataInputStream is; |
|
531 |
OutputStreamWriter os = null; |
|
532 |
HttpURLConnection connection = null; |
|
533 |
//If the used protocol is HTTPS |
|
534 |
if (url.getProtocol().equals("https")) { |
|
535 |
disableHttsValidation(); |
|
536 |
} |
|
537 |
connection = (HttpURLConnection)url.openConnection(); |
|
538 |
connection.setUseCaches(false); |
|
539 |
connection.setConnectTimeout(timeout); |
|
540 |
//If it uses a HTTP POST |
|
541 |
if (data != null) { |
|
542 |
connection.setRequestProperty("SOAPAction", "post"); |
|
543 |
connection.setRequestMethod("POST"); |
|
544 |
connection.setDoOutput(true); |
|
545 |
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
|
546 |
os = new OutputStreamWriter(connection.getOutputStream()); |
|
547 |
os.write(data); |
|
548 |
os.flush(); |
|
549 |
is = new DataInputStream(connection.getInputStream()); |
|
550 |
}else{ |
|
551 |
is = new DataInputStream(url.openStream()); |
|
552 |
} |
|
553 |
|
|
554 |
dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(dstFile))); |
|
555 |
byte[] buffer = new byte[1024 * 4]; |
|
556 |
|
|
557 |
|
|
558 |
long readed = 0; |
|
559 |
for (int i = is.read(buffer); !downloader.getCanceled(groupID) && i>0; i = is.read(buffer)){ |
|
560 |
dos.write(buffer, 0, i); |
|
561 |
readed += i; |
|
562 |
|
|
563 |
} |
|
564 |
if(os != null) { |
|
565 |
os.close(); |
|
566 |
} |
|
567 |
dos.close(); |
|
568 |
is.close(); |
|
569 |
is = null; |
|
570 |
dos = null; |
|
571 |
if (downloader.getCanceled(groupID)) { |
|
572 |
LOG.info("[RemoteServices] '" + url + "' CANCELED."); |
|
573 |
dstFile.delete(); |
|
574 |
dstFile = null; |
|
575 |
} else { |
|
576 |
downloader.addDownloadedURL(url, dstFile.getAbsolutePath()); |
|
577 |
} |
|
578 |
} catch (Exception e) { |
|
579 |
LOG.info("Error downloading", e); |
|
580 |
downloader.downloadException = e; |
|
581 |
} |
|
582 |
} |
|
583 |
|
|
584 |
/** |
|
585 |
* This method disables the Https certificate validation. |
|
586 |
* @throws KeyManagementException |
|
587 |
* @throws NoSuchAlgorithmException |
|
588 |
*/ |
|
589 |
private void disableHttsValidation() throws KeyManagementException, NoSuchAlgorithmException{ |
|
590 |
// Create a trust manager that does not validate certificate chains |
|
591 |
TrustManager[] trustAllCerts = new TrustManager[] { |
|
592 |
new X509TrustManager() { |
|
593 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
|
594 |
return null; |
|
595 |
} |
|
596 |
public void checkClientTrusted( |
|
597 |
java.security.cert.X509Certificate[] certs, String authType) { |
|
598 |
} |
|
599 |
public void checkServerTrusted( |
|
600 |
java.security.cert.X509Certificate[] certs, String authType) { |
|
601 |
} |
|
602 |
} |
|
603 |
}; |
|
604 |
|
|
605 |
// Install the all-trusting trust manager |
|
606 |
SSLContext sc = SSLContext.getInstance("SSL"); |
|
607 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
|
608 |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
|
609 |
} |
|
610 |
} |
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/Downloader.java | ||
---|---|---|
1 |
package org.gvsig.compat.se.net.downloader; |
|
2 |
|
|
3 |
import java.net.URL; |
|
4 |
|
|
5 |
public interface Downloader extends org.gvsig.compat.net.Downloader { |
|
6 |
|
|
7 |
public void setDownloadException(Exception exception); |
|
8 |
|
|
9 |
public boolean getCanceled(Object groupId); |
|
10 |
|
|
11 |
void addDownloadedURL(URL url, String filePath); |
|
12 |
|
|
13 |
long getLatency(); |
|
14 |
|
|
15 |
void setCanceled(Object groupId, boolean isCanceled); |
|
16 |
|
|
17 |
} |
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/HttpClientDownloader.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.compat.se.net.downloader.httpclient; |
|
3 |
|
|
4 |
import java.io.File; |
|
5 |
import java.net.URL; |
|
6 |
import org.gvsig.compat.se.net.downloader.Downloader; |
|
7 |
import org.gvsig.compat.se.net.downloader.se.SEDownloader; |
|
8 |
|
|
9 |
public class HttpClientDownloader extends SEDownloader { |
|
10 |
|
|
11 |
public HttpClientDownloader() { |
|
12 |
super(); |
|
13 |
} |
|
14 |
|
|
15 |
protected Runnable createDownloaderTask(Downloader downloader, URL url, String data, File target, Object groupID) { |
|
16 |
return new HttpClientDownloaderTask(downloader, url, data, target, groupID); |
|
17 |
} |
|
18 |
|
|
19 |
} |
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 | ||
---|---|---|
1 |
|
|
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 |
} |
|
137 |
|
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 | ||
---|---|---|
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 |
} |
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/SEMonitor.java | ||
---|---|---|
1 |
package org.gvsig.compat.se.net.downloader.se; |
|
2 |
|
|
3 |
import org.gvsig.compat.net.ICancellable; |
|
4 |
import org.gvsig.compat.se.net.downloader.Downloader; |
|
5 |
import org.slf4j.Logger; |
|
6 |
import org.slf4j.LoggerFactory; |
|
7 |
|
|
8 |
final class SEMonitor implements Runnable { |
|
9 |
|
|
10 |
ICancellable c; |
|
11 |
private volatile boolean finish = false; |
|
12 |
Downloader downloader; |
|
13 |
private static Logger LOG = LoggerFactory.getLogger(SEMonitor.class); |
|
14 |
|
|
15 |
public SEMonitor(Downloader downloader, ICancellable cancel) { |
|
16 |
downloader.setCanceled(cancel.getID(), false); |
|
17 |
this.c = cancel; |
|
18 |
this.downloader = downloader; |
|
19 |
} |
|
20 |
|
|
21 |
public void run() { |
|
22 |
while (!c.isCanceled() && !getFinish()) { |
|
23 |
try { |
|
24 |
Thread.sleep(downloader.getLatency()); |
|
25 |
} catch (InterruptedException e) { |
|
26 |
LOG.error("Error", e); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
/* WARNING!! This works because only one download is being processed at once. |
|
31 |
* You could prefer to start several transfers simultaneously. If so, you |
|
32 |
* should consideer using a non-static variable such is Utilities.canceled to |
|
33 |
* control when and which transfer in particular has been canceled. |
|
34 |
* |
|
35 |
* The feature of transfer several files is at the moment under study. We are |
|
36 |
* planning to add an intelligent system that will give you a lot of services |
|
37 |
* and ease-of-use. So, we encourage you to wait for it instead of write your |
|
38 |
* own code. |
|
39 |
*/ |
|
40 |
if (c.isCanceled()) { |
|
41 |
downloader.setCanceled(c.getID(), true); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
public synchronized void setFinish(boolean value) { |
|
46 |
finish = value; |
|
47 |
} |
|
48 |
|
|
49 |
public synchronized boolean getFinish() { |
|
50 |
return finish; |
|
51 |
} |
|
52 |
} |
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/SEDownloader.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify it under |
|
7 |
* the terms of the GNU General Public License as published by the Free Software |
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later |
|
9 |
* version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
14 |
* details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us at info AT |
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.compat.se.net.downloader.se; |
|
24 |
|
|
25 |
import java.io.File; |
|
26 |
import java.io.FileNotFoundException; |
|
27 |
import java.io.IOException; |
|
28 |
import java.net.ConnectException; |
|
29 |
import java.net.URI; |
|
30 |
import java.net.URL; |
|
31 |
import java.net.UnknownHostException; |
|
32 |
import java.util.Hashtable; |
|
33 |
|
|
34 |
import org.slf4j.Logger; |
|
35 |
import org.slf4j.LoggerFactory; |
|
36 |
|
|
37 |
import org.gvsig.compat.net.ICancellable; |
|
38 |
import org.gvsig.compat.se.net.downloader.Downloader; |
|
39 |
|
|
40 |
/** |
|
41 |
* Clase con m?todos de utilidad en el protocolo WMS |
|
42 |
* |
|
43 |
* @authors Laura D?az, jaume dominguez faus |
|
44 |
*/ |
|
45 |
public class SEDownloader implements Downloader { |
|
46 |
|
|
47 |
// String characters; |
|
48 |
private boolean canceled; |
|
49 |
private final long latency = 500; |
|
50 |
private static int count = 0; |
|
51 |
private static Logger LOG = LoggerFactory.getLogger(SEDownloader.class); |
|
52 |
|
|
53 |
/** |
|
54 |
* Used to cancel a group of files |
|
55 |
* <b>key</b>: Group id, <b>value</b>: Boolean (true if the group has to be |
|
56 |
* canceled. Otherwise it is false) |
|
57 |
*/ |
|
58 |
private Hashtable canceledGroup = new Hashtable(); |
|
59 |
/** |
|
60 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file. |
|
61 |
*/ |
|
62 |
private Hashtable cachedURItoFile; |
|
63 |
private Exception downloadException; |
|
64 |
final String tempDirectoryPath = System.getProperty("java.io.tmpdir") + "/tmp-andami"; |
|
65 |
|
|
66 |
public SEDownloader() { |
|
67 |
super(); |
|
68 |
// characters = ""; |
|
69 |
// for (int j = 32; j <= 127; j++) { |
|
70 |
// characters += (char) j; |
|
71 |
// } |
|
72 |
// characters += "?????????????????????????????????????????????????\n\r\f\t??"; |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Return the content of a file that has been created from a URL using the |
|
77 |
* HTTP GET protocol |
|
78 |
* |
|
79 |
* @param url The URL |
|
80 |
* @return File containing this URL's content or null if no file was found. |
|
81 |
*/ |
|
82 |
private File getPreviousDownloadedURL(URL url) { |
|
83 |
return getPreviousDownloaded(url); |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Return the content of a file that has been created from a URL using the |
|
88 |
* HTTP POST protocol |
|
89 |
* |
|
90 |
* @param url The URL |
|
91 |
* @param data The data to send on the query |
|
92 |
* @return File containing this URL's content or null if no file was found. |
|
93 |
*/ |
|
94 |
private File getPreviousDownloadedURL(URL url, String data) { |
|
95 |
if (data == null) { |
|
96 |
return getPreviousDownloaded(url); |
|
97 |
} |
|
98 |
return getPreviousDownloaded(url.toString() + data); |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* Returns the content of a URL as a file from the file system.<br> |
|
103 |
* <p> |
|
104 |
* If the URL has been already downloaded in this session and notified to |
|
105 |
* the system using the static <b>Utilities.addDownloadedURL(URL)</b> |
|
106 |
* method, it can be restored faster from the file system avoiding to |
|
107 |
* download it again. |
|
108 |
* </p> |
|
109 |
* |
|
110 |
* @param url |
|
111 |
* @return File containing this URL's content or null if no file was found. |
|
112 |
*/ |
|
113 |
private File getPreviousDownloaded(Object theurl) { |
|
114 |
|
|
115 |
File f = null; |
|
116 |
Object thekey = null; |
|
117 |
try { |
|
118 |
if (theurl instanceof URL) { |
|
119 |
thekey = ((URL) theurl).toURI(); |
|
120 |
} else { |
|
121 |
thekey = theurl; |
|
122 |
} |
|
123 |
} catch (Exception e) { |
|
124 |
LOG.info("Warning: did not check url: " + theurl, e); |
|
125 |
return null; |
|
126 |
} |
|
127 |
|
|
128 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(thekey)) { |
|
129 |
String filePath = (String) cachedURItoFile.get(thekey); |
|
130 |
f = new File(filePath); |
|
131 |
if (!f.exists()) { |
|
132 |
return null; |
|
133 |
} |
|
134 |
} |
|
135 |
return f; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Adds an URL to the table of downloaded files for further uses. If the URL |
|
140 |
* already exists in the table its filePath value is updated to the new one |
|
141 |
* and the old file itself is removed from the file system. |
|
142 |
* |
|
143 |
* @param url |
|
144 |
* @param filePath |
|
145 |
*/ |
|
146 |
public void addDownloadedURL(URL url, String filePath) { |
|
147 |
if (cachedURItoFile == null) { |
|
148 |
cachedURItoFile = new Hashtable(); |
|
149 |
} |
|
150 |
|
|
151 |
URI theuri = null; |
|
152 |
try { |
|
153 |
theuri = url.toURI(); |
|
154 |
} catch (Exception e) { |
|
155 |
LOG.info("Warning: did not cache bad url: " + url, e); |
|
156 |
return; |
|
157 |
} |
|
158 |
|
|
159 |
String fileName = (String) cachedURItoFile.put(theuri, filePath); |
|
160 |
//JMV: No se puede eliminar el anterior porque puede que alguien lo |
|
161 |
// este usando |
|
162 |
/* |
|
163 |
if (fileName!=null){ |
|
164 |
File f = new File(fileName); |
|
165 |
if (f.exists()) |
|
166 |
f.delete(); |
|
167 |
} |
|
168 |
*/ |
|
169 |
} |
|
170 |
|
|
171 |
/** |
|
172 |
* Downloads an URL into a temporary file that is removed the next time the |
|
173 |
* tempFileManager class is called, which means the next time gvSIG is |
|
174 |
* launched. |
|
175 |
* |
|
176 |
* @param url |
|
177 |
* @param name |
|
178 |
* @return |
|
179 |
* @throws IOException |
|
180 |
* @throws ServerErrorResponseException |
|
181 |
* @throws ConnectException |
|
182 |
* @throws UnknownHostException |
|
183 |
*/ |
|
184 |
public synchronized File downloadFile(URL url, String name, ICancellable cancel) throws IOException, ConnectException, UnknownHostException { |
|
185 |
File f = null; |
|
186 |
|
|
187 |
if ((f = getPreviousDownloadedURL(url)) == null) { |
|
188 |
File tempDirectory = new File(tempDirectoryPath); |
|
189 |
if (!tempDirectory.exists()) { |
|
190 |
tempDirectory.mkdir(); |
|
191 |
} |
|
192 |
|
|
193 |
f = new File(calculateFileName(name)); |
|
194 |
|
|
195 |
if (cancel == null) { |
|
196 |
cancel = new ICancellable() { |
|
197 |
public boolean isCanceled() { |
|
198 |
return false; |
|
199 |
} |
|
200 |
|
|
201 |
public Object getID() { |
|
202 |
return SEDownloader.class.getName(); |
|
203 |
} |
|
204 |
}; |
|
205 |
} |
|
206 |
|
|
207 |
SEMonitor monitorObj = new SEMonitor(this, cancel); |
|
208 |
Thread downloader = new Thread(createDownloaderTask(this, url, null, f, cancel.getID())); |
|
209 |
Thread monitor = new Thread(monitorObj); |
|
210 |
|
|
211 |
monitor.start(); |
|
212 |
downloader.start(); |
|
213 |
while (!getCanceled(cancel.getID()) && downloader.isAlive()) { |
|
214 |
try { |
|
215 |
Thread.sleep(latency); |
|
216 |
} catch (InterruptedException e) { |
|
217 |
LOG.error("Error", e); |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
try { |
|
222 |
monitorObj.setFinish(true); |
|
223 |
monitor.join(); |
|
224 |
downloader.join(); |
|
225 |
} catch (InterruptedException e1) { |
|
226 |
LOG.warn(e1.getMessage()); |
|
227 |
} |
|
228 |
downloader = null; |
|
229 |
monitor = null; |
|
230 |
|
|
231 |
if (getCanceled(cancel.getID())) { |
|
232 |
return null; |
|
233 |
} |
|
234 |
downloader = null; |
|
235 |
monitor = null; |
|
236 |
if (this.downloadException != null) { |
|
237 |
Exception e = this.downloadException; |
|
238 |
if (e instanceof FileNotFoundException) { |
|
239 |
throw (IOException) e; |
|
240 |
} else if (e instanceof IOException) { |
|
241 |
throw (IOException) e; |
|
242 |
} else if (e instanceof ConnectException) { |
|
243 |
throw (ConnectException) e; |
|
244 |
} else if (e instanceof UnknownHostException) { |
|
245 |
throw (UnknownHostException) e; |
|
246 |
} |
|
247 |
} |
|
248 |
} else { |
|
249 |
LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'"); |
|
250 |
} |
|
251 |
|
|
252 |
return f; |
|
253 |
} |
|
254 |
|
|
255 |
private String calculateFileName(String name) { |
|
256 |
count++; |
|
257 |
int index = name.lastIndexOf("."); |
|
258 |
if (index > 0) { |
|
259 |
return tempDirectoryPath + "/" + name.substring(0, index) + System.currentTimeMillis() + count |
|
260 |
+ name.substring(index, name.length()); |
|
261 |
} |
|
262 |
return tempDirectoryPath + "/" + name + System.currentTimeMillis() + count; |
|
263 |
} |
|
264 |
|
|
265 |
/** |
|
266 |
* Downloads a URL using the HTTP Post protocol |
|
267 |
* |
|
268 |
* @param url The server URL |
|
269 |
* @param data The data to send in the request |
|
270 |
* @param name A common name for all the retrieved files |
|
271 |
* @param cancel Used to cancel the downloads |
|
272 |
* @return The retrieved file |
|
273 |
* @throws IOException |
|
274 |
* @throws ConnectException |
|
275 |
* @throws UnknownHostException |
|
276 |
*/ |
|
277 |
public synchronized File downloadFile(URL url, String data, String name, ICancellable cancel) throws IOException, ConnectException, UnknownHostException { |
|
278 |
File f = null; |
|
279 |
|
|
280 |
if ((f = getPreviousDownloadedURL(url, data)) == null) { |
|
281 |
File tempDirectory = new File(tempDirectoryPath); |
|
282 |
if (!tempDirectory.exists()) { |
|
283 |
tempDirectory.mkdir(); |
|
284 |
} |
|
285 |
|
|
286 |
f = new File(calculateFileName(name)); |
|
287 |
|
|
288 |
if (cancel == null) { |
|
289 |
cancel = new ICancellable() { |
|
290 |
public boolean isCanceled() { |
|
291 |
return false; |
|
292 |
} |
|
293 |
|
|
294 |
public Object getID() { |
|
295 |
return SEDownloader.class.getName(); |
|
296 |
} |
|
297 |
}; |
|
298 |
} |
|
299 |
SEMonitor monitorObj = new SEMonitor(this, cancel); |
|
300 |
Thread downloader = new Thread(createDownloaderTask(this, url, data, f, cancel.getID())); |
|
301 |
Thread monitor = new Thread(monitorObj); |
|
302 |
monitor.start(); |
|
303 |
downloader.start(); |
|
304 |
while (!getCanceled(cancel.getID()) && downloader.isAlive()) { |
|
305 |
try { |
|
306 |
Thread.sleep(latency); |
|
307 |
} catch (InterruptedException e) { |
|
308 |
LOG.error("Error", e); |
|
309 |
} |
|
310 |
} |
|
311 |
|
|
312 |
try { |
|
313 |
monitorObj.setFinish(true); |
|
314 |
monitor.join(); |
|
315 |
downloader.join(); |
|
316 |
} catch (InterruptedException e1) { |
|
317 |
LOG.warn(e1.getMessage()); |
|
318 |
} |
|
319 |
downloader = null; |
|
320 |
monitor = null; |
|
321 |
|
|
322 |
if (getCanceled(cancel.getID())) { |
|
323 |
return null; |
|
324 |
} |
|
325 |
if (this.downloadException != null) { |
|
326 |
Exception e = this.downloadException; |
|
327 |
if (e instanceof FileNotFoundException) { |
|
328 |
throw (IOException) e; |
|
329 |
} else if (e instanceof IOException) { |
|
330 |
throw (IOException) e; |
|
331 |
} else if (e instanceof ConnectException) { |
|
332 |
throw (ConnectException) e; |
|
333 |
} else if (e instanceof UnknownHostException) { |
|
334 |
throw (UnknownHostException) e; |
|
335 |
} |
|
336 |
} |
|
337 |
} else { |
|
338 |
LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'"); |
|
339 |
} |
|
340 |
|
|
341 |
return f; |
|
342 |
} |
|
343 |
|
|
344 |
/** |
|
345 |
* Try if a group of downloads has been canceled |
|
346 |
* |
|
347 |
* @param groupId Group id |
|
348 |
* @return If the group has been canceled |
|
349 |
*/ |
|
350 |
public boolean getCanceled(Object groupId) { |
|
351 |
Object obj = canceledGroup.get(groupId); |
|
352 |
if (obj != null) { |
|
353 |
return ((Boolean) obj).booleanValue(); |
|
354 |
} |
|
355 |
return false; |
|
356 |
} |
|
357 |
|
|
358 |
/** |
|
359 |
* Cancel a group of downloads |
|
360 |
* |
|
361 |
* @param groupId Group id |
|
362 |
* @param isCanceled if the group has to be canceled |
|
363 |
*/ |
|
364 |
public void setCanceled(Object groupId, boolean isCanceled) { |
|
365 |
if (groupId == null) { |
|
366 |
groupId = SEDownloader.class.getName(); |
|
367 |
} |
|
368 |
canceledGroup.put(groupId, new Boolean(isCanceled)); |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* Cleans every temporal file previously downloaded. |
|
373 |
*/ |
|
374 |
public void cleanUpTempFiles() { |
|
375 |
try { |
|
376 |
File tempDirectory = new File(tempDirectoryPath); |
|
377 |
|
|
378 |
File[] files = tempDirectory.listFiles(); |
|
379 |
if (files != null) { |
|
380 |
for (int i = 0; i < files.length; i++) { |
|
381 |
// s?lo por si en un futuro se necesitan crear directorios temporales |
|
382 |
if (files[i].isDirectory()) { |
|
383 |
deleteDirectory(files[i]); |
|
384 |
} |
|
385 |
files[i].delete(); |
|
386 |
} |
|
387 |
} |
|
388 |
tempDirectory.delete(); |
|
389 |
} catch (Exception e) { |
|
390 |
} |
|
391 |
|
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* Recursive directory delete. |
|
396 |
* |
|
397 |
* @param f |
|
398 |
*/ |
|
399 |
private void deleteDirectory(File f) { |
|
400 |
File[] files = f.listFiles(); |
|
401 |
for (int i = 0; i < files.length; i++) { |
|
402 |
if (files[i].isDirectory()) { |
|
403 |
deleteDirectory(files[i]); |
|
404 |
} |
|
405 |
files[i].delete(); |
|
406 |
} |
|
407 |
|
|
408 |
} |
|
409 |
|
|
410 |
/** |
|
411 |
* Remove an URL from the system cache. The file will remain in the file |
|
412 |
* system for further eventual uses. |
|
413 |
* |
|
414 |
* @param request |
|
415 |
*/ |
|
416 |
public void removeURL(URL url) { |
|
417 |
|
|
418 |
URI theuri = null; |
|
419 |
|
|
420 |
try { |
|
421 |
theuri = url.toURI(); |
|
422 |
} catch (Exception e) { |
|
423 |
LOG.info("Warning: did not remove bad url: " + url, e); |
|
424 |
return; |
|
425 |
} |
|
426 |
|
|
427 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(theuri)) { |
|
428 |
cachedURItoFile.remove(theuri); |
|
429 |
} |
|
430 |
} |
|
431 |
|
|
432 |
/** |
|
433 |
* Remove an URL from the system cache. The file will remain in the file |
|
434 |
* system for further eventual uses. |
|
435 |
* |
|
436 |
* @param request |
|
437 |
*/ |
|
438 |
public void removeURL(Object url) { |
|
439 |
if (cachedURItoFile != null && cachedURItoFile.containsKey(url)) { |
|
440 |
cachedURItoFile.remove(url); |
|
441 |
} |
|
442 |
} |
|
443 |
|
|
444 |
public void setDownloadException(Exception exception) { |
|
445 |
this.downloadException = exception; |
|
446 |
} |
|
447 |
|
|
448 |
public long getLatency() { |
|
449 |
return this.latency; |
|
450 |
} |
|
451 |
|
|
452 |
protected Runnable createDownloaderTask(Downloader downloader, URL url, String data, File target, Object groupID){ |
|
453 |
return new SEDownloaderTask(downloader, url, data, target, groupID); |
|
454 |
} |
|
455 |
} |
|
0 | 456 |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.se/src/main/java/org/gvsig/compat/se/SECompatLibrary.java | ||
---|---|---|
28 | 28 |
import org.gvsig.compat.se.lang.SEGraphUtils; |
29 | 29 |
import org.gvsig.compat.se.lang.SEMathUtils; |
30 | 30 |
import org.gvsig.compat.se.lang.StandardStringUtils; |
31 |
import org.gvsig.compat.se.net.SEDownloader; |
|
31 |
import org.gvsig.compat.se.net.downloader.se.SEDownloader;
|
|
32 | 32 |
import org.gvsig.tools.library.AbstractLibrary; |
33 | 33 |
import org.gvsig.tools.library.LibraryException; |
34 | 34 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.se/pom.xml | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="UTF-8"?> |
2 | 2 |
|
3 | 3 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
<artifactId>org.gvsig.compat.se</artifactId> |
|
6 |
<name>${project.artifactId}</name> |
|
7 |
<parent> |
|
8 |
<groupId>org.gvsig</groupId> |
|
9 |
<artifactId>org.gvsig.compat</artifactId> |
|
10 |
<version>2.0.45-SNAPSHOT</version> |
|
11 |
</parent> |
|
12 |
|
|
13 |
<dependencies> |
|
14 |
<dependency> |
|
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
<artifactId>org.gvsig.compat.se</artifactId> |
|
6 |
<name>${project.artifactId}</name> |
|
7 |
<parent> |
Also available in: Unified diff