001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: HttpsProtocolSupport.java,v 1.1 2003/02/04 19:17:26 russgold Exp $
005: *
006: * Copyright (c) 2003, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.security.Provider;
024: import java.security.Security;
025:
026: /**
027: * Encapsulates support for the HTTPS protocol.
028: *
029: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
030: **/
031: abstract public class HttpsProtocolSupport {
032:
033: /** The name of the system parameter used by java.net to locate protocol handlers. **/
034: private final static String PROTOCOL_HANDLER_PKGS = "java.protocol.handler.pkgs";
035:
036: /** The name of the JSSE class which provides support for SSL. **/
037: private final static String SunJSSE_PROVIDER_CLASS = "com.sun.net.ssl.internal.ssl.Provider";
038:
039: /** The name of the JSSE class which supports the https protocol. **/
040: private final static String SSL_PROTOCOL_HANDLER = "com.sun.net.ssl.internal.www.protocol";
041:
042: private static Class _httpsProviderClass;
043:
044: private static boolean _httpsSupportVerified;
045:
046: private static boolean _httpsProtocolSupportEnabled;
047:
048: /**
049: * Returns true if the JSSE extension is installed.
050: */
051: static boolean hasHttpsSupport() {
052: if (!_httpsSupportVerified) {
053: try {
054: getHttpsProviderClass();
055: } catch (ClassNotFoundException e) {
056: }
057: _httpsSupportVerified = true;
058: }
059: return _httpsProviderClass != null;
060: }
061:
062: /**
063: * Attempts to register the JSSE extension if it is not already registered. Will throw an exception if unable to
064: * register the extension.
065: */
066: static void verifyProtocolSupport(String protocol) {
067: if (protocol.equalsIgnoreCase("http")) {
068: return;
069: } else if (protocol.equalsIgnoreCase("https")) {
070: validateHttpsProtocolSupport();
071: }
072: }
073:
074: private static void validateHttpsProtocolSupport() {
075: if (!_httpsProtocolSupportEnabled) {
076: verifyHttpsSupport();
077: _httpsProtocolSupportEnabled = true;
078: }
079: }
080:
081: private static void verifyHttpsSupport() {
082: try {
083: Class providerClass = getHttpsProviderClass();
084: if (!hasProvider(providerClass))
085: Security.addProvider((Provider) providerClass
086: .newInstance());
087: registerSSLProtocolHandler();
088: } catch (ClassNotFoundException e) {
089: throw new RuntimeException(
090: "https support requires the Java Secure Sockets Extension. See http://java.sun.com/products/jsse");
091: } catch (Throwable e) {
092: throw new RuntimeException(
093: "Unable to enable https support. Make sure that you have installed JSSE "
094: + "as described in http://java.sun.com/products/jsse/install.html: "
095: + e);
096: }
097: }
098:
099: private static Class getHttpsProviderClass()
100: throws ClassNotFoundException {
101: if (_httpsProviderClass == null) {
102: _httpsProviderClass = Class.forName(SunJSSE_PROVIDER_CLASS);
103: }
104: return _httpsProviderClass;
105: }
106:
107: private static boolean hasProvider(Class providerClass) {
108: Provider[] list = Security.getProviders();
109: for (int i = 0; i < list.length; i++) {
110: if (list[i].getClass().equals(providerClass))
111: return true;
112: }
113: return false;
114: }
115:
116: private static void registerSSLProtocolHandler() {
117: String list = System.getProperty(PROTOCOL_HANDLER_PKGS);
118: if (list == null || list.length() == 0) {
119: System.setProperty(PROTOCOL_HANDLER_PKGS,
120: SSL_PROTOCOL_HANDLER);
121: } else if (list.indexOf(SSL_PROTOCOL_HANDLER) < 0) {
122: System.setProperty(PROTOCOL_HANDLER_PKGS,
123: SSL_PROTOCOL_HANDLER + " | " + list);
124: }
125: }
126: }
|