1 19 20 28 29 package org.netbeans.modules.xml.retriever; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.net.HttpURLConnection ; 34 import java.net.Proxy ; 35 import java.net.ProxySelector ; 36 import java.net.URI ; 37 import java.net.URISyntaxException ; 38 import java.net.URL ; 39 import java.net.URLConnection ; 40 import java.util.HashMap ; 41 42 46 public class URLResourceRetriever implements ResourceRetriever{ 47 48 private static final String URI_SCHEME = "http"; 50 public URLResourceRetriever() { 51 } 52 53 public boolean accept(String baseAddr, String currentAddr) throws URISyntaxException { 54 URI currURI = new URI (currentAddr); 55 if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase(URI_SCHEME))) 56 return true; 57 if(baseAddr != null){ 58 if(!currURI.isAbsolute()){ 59 URI baseURI = new URI (baseAddr); 60 if(baseURI.getScheme().equalsIgnoreCase(URI_SCHEME)) 61 return true; 62 } 63 } 64 return false; 65 } 66 67 public HashMap <String , InputStream > retrieveDocument(String baseAddress, 68 String documentAddress) throws IOException ,URISyntaxException { 69 70 String effAddr = getEffectiveAddress(baseAddress, documentAddress); 71 if(effAddr == null) 72 return null; 73 URI currURI = new URI (effAddr); 74 HashMap <String , InputStream > result = null; 75 76 InputStream is = getInputStreamOfURL(currURI.toURL(), ProxySelector. 77 getDefault().select(currURI).get(0)); 78 result = new HashMap <String , InputStream >(); 79 result.put(effectiveURL.toString(), is); 80 return result; 81 82 } 83 84 long streamLength = 0; 85 URL effectiveURL = null; 86 public InputStream getInputStreamOfURL(URL downloadURL, Proxy proxy) throws IOException { 87 88 URLConnection ucn = null; 89 90 if(Thread.currentThread().isInterrupted()) 91 return null; 92 if(proxy != null) 93 ucn = downloadURL.openConnection(proxy); 94 else 95 ucn = downloadURL.openConnection(); 96 HttpURLConnection hucn = null; 97 if(ucn instanceof HttpURLConnection ){ 98 hucn = ((HttpURLConnection )ucn); 99 hucn.setFollowRedirects(false); 100 } 101 if(Thread.currentThread().isInterrupted()) 102 return null; 103 ucn.connect(); 104 while( (hucn.getResponseCode() == hucn.HTTP_MOVED_TEMP) || 106 (hucn.getResponseCode() == hucn.HTTP_MOVED_PERM) ) { 107 String addr = hucn.getHeaderField("Location"); 108 downloadURL = new URL (addr); 109 if(proxy != null) 110 ucn = downloadURL.openConnection(proxy); 111 else 112 ucn = downloadURL.openConnection(); 113 if(ucn instanceof HttpURLConnection ){ 114 hucn = ((HttpURLConnection )ucn); 115 hucn.setFollowRedirects(false); 116 } 117 if(Thread.currentThread().isInterrupted()) 118 return null; 119 ucn.connect(); 120 } 121 ucn.setReadTimeout(10000); 122 InputStream is = ucn.getInputStream(); 123 streamLength = ucn.getContentLength(); 124 effectiveURL = ucn.getURL(); 125 return is; 126 127 } 128 129 public long getStreamLength() { 130 return streamLength; 131 } 132 133 public String getEffectiveAddress(String baseAddress, String documentAddress) throws IOException , URISyntaxException { 134 return resolveURL(baseAddress, documentAddress); 135 } 136 137 public static String resolveURL(String baseAddress, String documentAddress) throws URISyntaxException { 138 URI currURI = new URI (documentAddress); 139 String result = null; 140 if(currURI.isAbsolute()){ 141 result = currURI.toString(); 142 return result; 143 }else{ 144 if(baseAddress != null){ 146 URI baseURI = new URI (baseAddress); 147 URI finalURI = baseURI.resolve(currURI); 148 result = finalURI.toString(); 149 return result; 150 }else{ 151 return null; 154 } 155 } 156 } 157 } 158 | Popular Tags |