Revision 18333 trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/utils/Utilities.java
Utilities.java | ||
---|---|---|
50 | 50 |
import java.io.IOException; |
51 | 51 |
import java.io.InputStream; |
52 | 52 |
import java.io.OutputStream; |
53 |
import java.io.OutputStreamWriter; |
|
53 | 54 |
import java.net.ConnectException; |
55 |
import java.net.HttpURLConnection; |
|
54 | 56 |
import java.net.URL; |
57 |
import java.net.URLConnection; |
|
55 | 58 |
import java.net.UnknownHostException; |
56 | 59 |
import java.rmi.NoSuchObjectException; |
57 | 60 |
import java.security.KeyManagementException; |
58 | 61 |
import java.security.NoSuchAlgorithmException; |
59 |
import java.security.Security; |
|
60 | 62 |
import java.util.Hashtable; |
61 | 63 |
import java.util.StringTokenizer; |
62 | 64 |
import java.util.Vector; |
63 | 65 |
|
64 | 66 |
import javax.net.ssl.HttpsURLConnection; |
65 | 67 |
import javax.net.ssl.SSLContext; |
66 |
import javax.net.ssl.SSLSocketFactory; |
|
67 | 68 |
import javax.net.ssl.TrustManager; |
68 | 69 |
import javax.net.ssl.X509TrustManager; |
69 | 70 |
|
... | ... | |
423 | 424 |
} |
424 | 425 |
|
425 | 426 |
/** |
426 |
* Returns the content of this URL as a file from the file system.<br> |
|
427 |
* Return the content of a file that has been created |
|
428 |
* from a URL using the HTTP GET protocol |
|
429 |
* @param url |
|
430 |
* The URL |
|
431 |
* @return |
|
432 |
* File containing this URL's content or null if no file was found. |
|
433 |
*/ |
|
434 |
private static File getPreviousDownloadedURL(URL url){ |
|
435 |
return getPreviousDownloaded(url); |
|
436 |
} |
|
437 |
|
|
438 |
/** |
|
439 |
* Return the content of a file that has been created |
|
440 |
* from a URL using the HTTP POST protocol |
|
441 |
* @param url |
|
442 |
* The URL |
|
443 |
* @param data |
|
444 |
* The data to send on the query |
|
445 |
* @return |
|
446 |
* File containing this URL's content or null if no file was found. |
|
447 |
*/ |
|
448 |
private static File getPreviousDownloadedURL(URL url, String data){ |
|
449 |
return getPreviousDownloaded(url+data); |
|
450 |
} |
|
451 |
|
|
452 |
/** |
|
453 |
* Returns the content of a URL as a file from the file system.<br> |
|
427 | 454 |
* <p> |
428 | 455 |
* If the URL has been already downloaded in this session and notified |
429 | 456 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b> |
... | ... | |
433 | 460 |
* @param url |
434 | 461 |
* @return File containing this URL's content or null if no file was found. |
435 | 462 |
*/ |
436 |
private static File getPreviousDownloadedURL(URL url){
|
|
463 |
private static File getPreviousDownloaded(Object object){
|
|
437 | 464 |
File f = null; |
438 |
if (downloadedFiles!=null && downloadedFiles.containsKey(url)){
|
|
439 |
String filePath = (String) downloadedFiles.get(url);
|
|
465 |
if (downloadedFiles!=null && downloadedFiles.containsKey(object)){
|
|
466 |
String filePath = (String) downloadedFiles.get(object);
|
|
440 | 467 |
f = new File(filePath); |
441 | 468 |
if (!f.exists()) |
442 | 469 |
return null; |
... | ... | |
535 | 562 |
} |
536 | 563 |
|
537 | 564 |
/** |
565 |
* Downloads a URL using the HTTP Post protocol |
|
566 |
* @param url |
|
567 |
* The server URL |
|
568 |
* @param data |
|
569 |
* The data to send in the request |
|
570 |
* @param name |
|
571 |
* A common name for all the retrieved files |
|
572 |
* @param cancel |
|
573 |
* Used to cancel the downloads |
|
574 |
* @return |
|
575 |
* The retrieved file |
|
576 |
* @throws IOException |
|
577 |
* @throws ConnectException |
|
578 |
* @throws UnknownHostException |
|
579 |
*/ |
|
580 |
public static synchronized File downloadFile(URL url, String data, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{ |
|
581 |
File f = null; |
|
582 |
|
|
583 |
if ((f=getPreviousDownloadedURL(url,data))==null){ |
|
584 |
File tempDirectory = new File(tempDirectoryPath); |
|
585 |
if (!tempDirectory.exists()) |
|
586 |
tempDirectory.mkdir(); |
|
587 |
|
|
588 |
f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
|
589 |
|
|
590 |
if (cancel == null) { |
|
591 |
cancel = new ICancellable() { |
|
592 |
public boolean isCanceled() { |
|
593 |
return false; |
|
594 |
} |
|
595 |
public Object getID(){ |
|
596 |
return Utilities.class.getName(); |
|
597 |
} |
|
598 |
}; |
|
599 |
} |
|
600 |
Thread downloader = new Thread(new Downloader(url, data, f, cancel.getID())); |
|
601 |
Thread monitor = new Thread(new Monitor(cancel)); |
|
602 |
monitor.start(); |
|
603 |
downloader.start(); |
|
604 |
while(!getCanceled(cancel.getID()) && downloader.isAlive()) { |
|
605 |
try { |
|
606 |
Thread.sleep(latency); |
|
607 |
} catch (InterruptedException e) { |
|
608 |
// TODO Auto-generated catch block |
|
609 |
e.printStackTrace(); |
|
610 |
} |
|
611 |
} |
|
612 |
|
|
613 |
if (getCanceled(cancel.getID())) |
|
614 |
return null; |
|
615 |
downloader = null; |
|
616 |
monitor = null; |
|
617 |
if (Utilities.downloadException!=null) { |
|
618 |
Exception e = Utilities.downloadException; |
|
619 |
if (e instanceof FileNotFoundException) |
|
620 |
throw (IOException) e; |
|
621 |
else if (e instanceof IOException) |
|
622 |
throw (IOException) e; |
|
623 |
else if (e instanceof ConnectException) |
|
624 |
throw (ConnectException) e; |
|
625 |
else if (e instanceof UnknownHostException) |
|
626 |
throw (UnknownHostException) e; |
|
627 |
} |
|
628 |
} else { |
|
629 |
System.out.println(url.toString()+" cached at '"+f.getAbsolutePath()+"'"); |
|
630 |
} |
|
631 |
|
|
632 |
return f; |
|
633 |
} |
|
634 |
|
|
635 |
/** |
|
538 | 636 |
* Try if a group of downloads has been canceled |
539 | 637 |
* @param groupId |
540 | 638 |
* Group id |
... | ... | |
606 | 704 |
downloadedFiles.remove(url); |
607 | 705 |
} |
608 | 706 |
|
609 |
|
|
707 |
/** |
|
708 |
* Remove an URL from the system cache. The file will remain in the file |
|
709 |
* system for further eventual uses. |
|
710 |
* @param request |
|
711 |
*/ |
|
712 |
public static void removeURL(Object url) { |
|
713 |
if (downloadedFiles != null && downloadedFiles.containsKey(url)) |
|
714 |
downloadedFiles.remove(url); |
|
715 |
} |
|
610 | 716 |
} |
611 | 717 |
|
612 | 718 |
final class Monitor implements Runnable { |
... | ... | |
643 | 749 |
private URL url; |
644 | 750 |
private File dstFile; |
645 | 751 |
private Object groupID = null; |
752 |
private String data = null; |
|
646 | 753 |
|
647 |
|
|
648 | 754 |
public Downloader(URL url, File dstFile, Object groupID) { |
649 | 755 |
this.url = url; |
650 | 756 |
this.dstFile = dstFile; |
... | ... | |
652 | 758 |
Utilities.downloadException = null; |
653 | 759 |
} |
654 | 760 |
|
761 |
public Downloader(URL url, String data, File dstFile, Object groupID) { |
|
762 |
this.url = url; |
|
763 |
this.data = data; |
|
764 |
this.dstFile = dstFile; |
|
765 |
this.groupID = groupID; |
|
766 |
Utilities.downloadException = null; |
|
767 |
} |
|
768 |
|
|
655 | 769 |
public void run() { |
656 | 770 |
System.out.println("downloading '"+url.toString()+"' to: "+dstFile.getAbsolutePath()); |
657 | 771 |
|
658 | 772 |
DataOutputStream dos; |
659 | 773 |
try { |
660 | 774 |
DataInputStream is; |
775 |
OutputStreamWriter os = null; |
|
776 |
HttpURLConnection connection = null; |
|
777 |
//If the used protocol is HTTPS |
|
661 | 778 |
if (url.getProtocol().equals("https")){ |
662 | 779 |
disableHttsValidation(); |
663 |
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); |
|
780 |
} |
|
781 |
connection = (HttpURLConnection)url.openConnection(); |
|
782 |
//If it uses a HTTP POST |
|
783 |
if (data != null){ |
|
784 |
connection.setRequestProperty("SOAPAction","post"); |
|
785 |
connection.setRequestMethod("POST"); |
|
786 |
connection.setDoOutput(true); |
|
787 |
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
|
788 |
os = new OutputStreamWriter(connection.getOutputStream()); |
|
789 |
os.write(data); |
|
790 |
os.flush(); |
|
664 | 791 |
is = new DataInputStream(connection.getInputStream()); |
665 | 792 |
}else{ |
666 | 793 |
is = new DataInputStream(url.openStream()); |
667 | 794 |
} |
795 |
|
|
668 | 796 |
dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(dstFile))); |
669 | 797 |
byte[] buffer = new byte[1024*4]; |
670 | 798 |
|
... | ... | |
675 | 803 |
readed += i; |
676 | 804 |
|
677 | 805 |
} |
806 |
if(os != null){ |
|
807 |
os.close(); |
|
808 |
} |
|
678 | 809 |
dos.close(); |
679 | 810 |
is.close(); |
680 | 811 |
is = null; |
Also available in: Unified diff