1 19 20 package com.maverick.ssl.https; 21 22 import java.io.IOException ; 23 import java.net.URLStreamHandler ; 24 import java.net.URLStreamHandlerFactory ; 25 26 30 public class HttpsURLStreamHandlerFactory implements URLStreamHandlerFactory { 31 32 static String packagesPropertyName = "java.protocol.handler.pkgs"; static String packageProperty = "com.maverick.ssl"; 35 public HttpsURLStreamHandlerFactory() { 36 } 37 38 public URLStreamHandler createURLStreamHandler(String protocol) { 39 if (protocol.equalsIgnoreCase("https")) { return new Handler(); 41 } else { 42 return null; 43 } 44 } 45 46 public static void addHTTPSSupport() throws IOException , SecurityException { 47 48 String packagesProperty = System.getProperty(packagesPropertyName, ""); int index = packagesProperty.indexOf(packageProperty + "|"); if (index >= 0) { 51 packagesProperty = packagesProperty.substring(0, index) 52 + packagesProperty.substring(index + packageProperty.length() + 1); 53 } 54 index = packagesProperty.indexOf("|" + packageProperty); if (index >= 0) { 56 packagesProperty = packagesProperty.substring(0, index) 57 + packagesProperty.substring(index + packageProperty.length() + 1); 58 } 59 if (packagesProperty.equals(packageProperty) || packagesProperty.equals("")) { packagesProperty = packageProperty; 61 } else { 62 packagesProperty = packageProperty + "|" + packagesProperty; } 64 System.getProperties().put(packagesPropertyName, packagesProperty); 65 } 66 67 public static void removeHTTPSSupport() throws SecurityException { 68 69 String packagesProperty = System.getProperty(packagesPropertyName, ""); int index = packagesProperty.indexOf(packageProperty + "|"); if (index >= 0) { 72 packagesProperty = packagesProperty.substring(0, index) 73 + packagesProperty.substring(index + packageProperty.length() + 1); 74 } 75 index = packagesProperty.indexOf("|" + packageProperty); if (index >= 0) { 77 packagesProperty = packagesProperty.substring(0, index) 78 + packagesProperty.substring(index + packageProperty.length() + 1); 79 } 80 if (packagesProperty.equals(packageProperty)) { 81 packagesProperty = ""; } 83 System.getProperties().put(packagesPropertyName, packagesProperty); 84 } 85 86 public static void setHTTPProxyHost(String host, int port, String username, String password, boolean isSecure) 87 throws SecurityException { 88 System.getProperties().put(HttpsURLConnection.httpProxyHostProperty, host); 89 System.getProperties().put(HttpsURLConnection.httpProxyPortProperty, "" + port); System.getProperties().put(HttpsURLConnection.httpProxyUsernameProperty, "" + username); System.getProperties().put(HttpsURLConnection.httpProxyPasswordProperty, "" + password); System.getProperties().put(HttpsURLConnection.httpProxySecureProperty, "" + String.valueOf(isSecure)); } 94 95 public static void clearHTTPProxyHost() throws SecurityException { 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 |