KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > ssl > https > HttpsURLStreamHandlerFactory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
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 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.ssl.https;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URLStreamHandler JavaDoc;
24 import java.net.URLStreamHandlerFactory JavaDoc;
25
26 /**
27  *
28  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
29  */

30 public class HttpsURLStreamHandlerFactory implements URLStreamHandlerFactory JavaDoc {
31
32     static String JavaDoc packagesPropertyName = "java.protocol.handler.pkgs"; //$NON-NLS-1$
33
static String JavaDoc packageProperty = "com.maverick.ssl"; //$NON-NLS-1$
34

35     public HttpsURLStreamHandlerFactory() {
36     }
37
38     public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
39         if (protocol.equalsIgnoreCase("https")) { //$NON-NLS-1$
40
return new Handler();
41         } else {
42             return null;
43         }
44     }
45
46     public static void addHTTPSSupport() throws IOException JavaDoc, SecurityException JavaDoc {
47
48         String JavaDoc packagesProperty = System.getProperty(packagesPropertyName, ""); //$NON-NLS-1$
49
int index = packagesProperty.indexOf(packageProperty + "|"); //$NON-NLS-1$
50
if (index >= 0) {
51             packagesProperty = packagesProperty.substring(0, index)
52                 + packagesProperty.substring(index + packageProperty.length() + 1);
53         }
54         index = packagesProperty.indexOf("|" + packageProperty); //$NON-NLS-1$
55
if (index >= 0) {
56             packagesProperty = packagesProperty.substring(0, index)
57                 + packagesProperty.substring(index + packageProperty.length() + 1);
58         }
59         if (packagesProperty.equals(packageProperty) || packagesProperty.equals("")) { //$NON-NLS-1$
60
packagesProperty = packageProperty;
61         } else {
62             packagesProperty = packageProperty + "|" + packagesProperty; //$NON-NLS-1$
63
}
64         System.getProperties().put(packagesPropertyName, packagesProperty);
65     }
66
67     public static void removeHTTPSSupport() throws SecurityException JavaDoc {
68
69         String JavaDoc packagesProperty = System.getProperty(packagesPropertyName, ""); //$NON-NLS-1$
70
int index = packagesProperty.indexOf(packageProperty + "|"); //$NON-NLS-1$
71
if (index >= 0) {
72             packagesProperty = packagesProperty.substring(0, index)
73                 + packagesProperty.substring(index + packageProperty.length() + 1);
74         }
75         index = packagesProperty.indexOf("|" + packageProperty); //$NON-NLS-1$
76
if (index >= 0) {
77             packagesProperty = packagesProperty.substring(0, index)
78                 + packagesProperty.substring(index + packageProperty.length() + 1);
79         }
80         if (packagesProperty.equals(packageProperty)) {
81             packagesProperty = ""; //$NON-NLS-1$
82
}
83         System.getProperties().put(packagesPropertyName, packagesProperty);
84     }
85
86     public static void setHTTPProxyHost(String JavaDoc host, int port, String JavaDoc username, String JavaDoc password, boolean isSecure)
87                     throws SecurityException JavaDoc {
88         System.getProperties().put(HttpsURLConnection.httpProxyHostProperty, host);
89         System.getProperties().put(HttpsURLConnection.httpProxyPortProperty, "" + port); //$NON-NLS-1$
90
System.getProperties().put(HttpsURLConnection.httpProxyUsernameProperty, "" + username); //$NON-NLS-1$
91
System.getProperties().put(HttpsURLConnection.httpProxyPasswordProperty, "" + password); //$NON-NLS-1$
92
System.getProperties().put(HttpsURLConnection.httpProxySecureProperty, "" + String.valueOf(isSecure)); //$NON-NLS-1$
93
}
94
95     public static void clearHTTPProxyHost() throws SecurityException JavaDoc {
96         System.getProperties().remove(HttpsURLConnection.httpProxyHostProperty);
97         System.getProperties().remove(HttpsURLConnection.httpProxyPortProperty);
98         System.getProperties().remove(HttpsURLConnection.httpProxyUsernameProperty);
99         System.getProperties().remove(HttpsURLConnection.httpProxyPasswordProperty);
100         System.getProperties().remove(HttpsURLConnection.httpProxySecureProperty);
101     }
102 }
103
Popular Tags