1 19 20 package org.netbeans.modules.xml.retriever; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.ProxySelector ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.security.cert.CertificateException ; 28 import java.security.cert.X509Certificate ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import javax.net.ssl.*; 33 import org.openide.DialogDescriptor; 34 import org.openide.DialogDisplayer; 35 import org.openide.ErrorManager; 36 import org.openide.util.NbBundle; 37 38 42 public class SecureURLResourceRetriever extends URLResourceRetriever { 43 44 private static Set <X509Certificate > acceptedCertificates; 45 private static final String URI_SCHEME = "https"; 46 47 48 public SecureURLResourceRetriever() { 49 } 50 51 public boolean accept(String baseAddr, String currentAddr) throws URISyntaxException { 52 53 URI currURI = new URI (currentAddr); 54 if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase(URI_SCHEME))) 55 return true; 56 if(baseAddr != null){ 57 URI baseURI = new URI (baseAddr); 58 if(baseURI.getScheme().equalsIgnoreCase(URI_SCHEME)) 59 return true; 60 } 61 return false; 62 } 63 64 public HashMap <String , InputStream > retrieveDocument(String baseAddress, 65 String documentAddress) throws IOException ,URISyntaxException { 66 67 String effAddr = getEffectiveAddress(baseAddress, documentAddress); 68 if(effAddr == null) 69 return null; 70 URI currURI = new URI (effAddr); 71 HashMap <String , InputStream > result = null; 72 73 if (acceptedCertificates==null) acceptedCertificates = new HashSet (); 74 setRetrieverTrustManager(); 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 private void setRetrieverTrustManager() { 86 TrustManager[] trustAllCerts = new TrustManager[]{ 87 new X509TrustManager() { 88 public X509Certificate [] getAcceptedIssuers() { 89 return new X509Certificate [0]; 90 } 91 public void checkClientTrusted(X509Certificate [] certs, String authType) { 92 } 93 public void checkServerTrusted(X509Certificate [] certs, String authType) 94 throws CertificateException { 95 if (certs!=null) { 97 for (int i=0;i<certs.length;i++) { 98 if (!acceptedCertificates.contains(certs[i])) { 99 DialogDescriptor desc = new DialogDescriptor(new CertificationPanel(certs[i]), 100 NbBundle.getMessage(SecureURLResourceRetriever.class,"TTL_CertifiedWebSite"), 101 true, 102 DialogDescriptor.YES_NO_OPTION, 103 DialogDescriptor.YES_OPTION, 104 null); 105 DialogDisplayer.getDefault().notify(desc); 106 if (DialogDescriptor.YES_OPTION.equals(desc.getValue())) { 107 acceptedCertificates.add(certs[i]); 108 } else { 109 throw new CertificateException ( 110 NbBundle.getMessage(SecureURLResourceRetriever.class,"ERR_NotTrustedCertificate")); 111 } 112 } 113 } } 115 } 116 } 117 }; 118 119 120 try { 121 SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom ()); 123 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 124 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 125 public boolean verify(String string, SSLSession sSLSession) { 126 return true; 128 } 129 }); 130 } catch (java.security.GeneralSecurityException e) { 131 ErrorManager.getDefault().notify(e); 132 } 133 } 134 135 public String getEffectiveAddress(String baseAddress, String documentAddress) throws IOException , URISyntaxException { 136 URI currURI = new URI (documentAddress); 137 String result = null; 138 if(currURI.isAbsolute()){ 139 result = currURI.toString(); 140 return result; 141 }else{ 142 if(baseAddress != null){ 144 URI baseURI = new URI (baseAddress); 145 URI finalURI = baseURI.resolve(currURI); 146 result = finalURI.toString(); 147 return result; 148 }else{ 149 return null; 152 } 153 } 154 } 155 156 } 157 | Popular Tags |