KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > SecureURLResourceRetriever


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.retriever;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.ProxySelector JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.security.cert.CertificateException JavaDoc;
28 import java.security.cert.X509Certificate JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
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 /**
39  *
40  * @author Lukas Jungmann, Milan Kuchtiak
41  */

42 public class SecureURLResourceRetriever extends URLResourceRetriever {
43     
44     private static Set JavaDoc<X509Certificate JavaDoc> acceptedCertificates;
45     private static final String JavaDoc URI_SCHEME = "https";
46     
47     /** Creates a new instance of SecureURLResourceRetriever */
48     public SecureURLResourceRetriever() {
49     }
50
51     public boolean accept(String JavaDoc baseAddr, String JavaDoc currentAddr) throws URISyntaxException JavaDoc {
52         
53         URI JavaDoc currURI = new URI JavaDoc(currentAddr);
54         if( (currURI.isAbsolute()) && (currURI.getScheme().equalsIgnoreCase(URI_SCHEME)))
55             return true;
56         if(baseAddr != null){
57             URI JavaDoc baseURI = new URI JavaDoc(baseAddr);
58             if(baseURI.getScheme().equalsIgnoreCase(URI_SCHEME))
59                 return true;
60         }
61         return false;
62     }
63     
64     public HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> retrieveDocument(String JavaDoc baseAddress,
65             String JavaDoc documentAddress) throws IOException JavaDoc,URISyntaxException JavaDoc{
66         
67         String JavaDoc effAddr = getEffectiveAddress(baseAddress, documentAddress);
68         if(effAddr == null)
69             return null;
70         URI JavaDoc currURI = new URI JavaDoc(effAddr);
71         HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> result = null;
72         
73         if (acceptedCertificates==null) acceptedCertificates = new HashSet JavaDoc();
74         setRetrieverTrustManager();
75         
76         InputStream JavaDoc is = getInputStreamOfURL(currURI.toURL(), ProxySelector.
77                 getDefault().select(currURI).get(0));
78         result = new HashMap JavaDoc<String JavaDoc, InputStream JavaDoc>();
79         result.put(effectiveURL.toString(), is);
80         return result;
81         
82     }
83     
84     // Install the trust manager for retriever
85
private void setRetrieverTrustManager() {
86         TrustManager[] trustAllCerts = new TrustManager[]{
87             new X509TrustManager() {
88                 public X509Certificate JavaDoc[] getAcceptedIssuers() {
89                     return new X509Certificate JavaDoc[0];
90                 }
91                 public void checkClientTrusted(X509Certificate JavaDoc[] certs, String JavaDoc authType) {
92                 }
93                 public void checkServerTrusted(X509Certificate JavaDoc[] certs, String JavaDoc authType)
94                 throws CertificateException JavaDoc {
95                     // ask user to accept the unknown certificate
96
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 JavaDoc(
110                                             NbBundle.getMessage(SecureURLResourceRetriever.class,"ERR_NotTrustedCertificate"));
111                                 }
112                             }
113                         } // end for
114
}
115                 }
116             }
117         };
118         
119         
120         try {
121             SSLContext sslContext = SSLContext.getInstance("SSL"); //NOI18N
122
sslContext.init(null, trustAllCerts, new java.security.SecureRandom JavaDoc());
123             HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
124             HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
125                 public boolean verify(String JavaDoc string, SSLSession sSLSession) {
126                     // accept all hosts
127
return true;
128                 }
129             });
130         } catch (java.security.GeneralSecurityException JavaDoc e) {
131             ErrorManager.getDefault().notify(e);
132         }
133     }
134     
135     public String JavaDoc getEffectiveAddress(String JavaDoc baseAddress, String JavaDoc documentAddress) throws IOException JavaDoc, URISyntaxException JavaDoc {
136         URI JavaDoc currURI = new URI JavaDoc(documentAddress);
137         String JavaDoc result = null;
138         if(currURI.isAbsolute()){
139             result = currURI.toString();
140             return result;
141         }else{
142             //relative URI
143
if(baseAddress != null){
144                 URI JavaDoc baseURI = new URI JavaDoc(baseAddress);
145                 URI JavaDoc finalURI = baseURI.resolve(currURI);
146                 result = finalURI.toString();
147                 return result;
148             }else{
149                 //neither the current URI nor the base URI are absoulte. So, can not resolve this
150
//path
151
return null;
152             }
153         }
154     }
155     
156 }
157
Popular Tags