All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.enterprise.admin.util.HttpConnectorAddress Maven / Gradle / Ivy

There is a newer version: 10.0-b28
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.enterprise.admin.util;

import com.sun.enterprise.security.ssl.SSLUtils;
import com.sun.enterprise.security.store.AsadminSecurityUtil;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.KeyManagementException;

import com.sun.enterprise.universal.GFBase64Encoder;

import javax.net.ssl.*;
import org.jvnet.hk2.component.Habitat;

public final class HttpConnectorAddress {
    static final String HTTP_CONNECTOR = "http";
    static final String HTTPS_CONNECTOR = "https";
    public static final String  AUTHORIZATION_KEY     = "Authorization";
    private static final String AUTHORIZATION_TYPE = "Basic ";

    private String host;
    private int    port;
    private String path;
    private boolean secure;
    private AuthenticationInfo  authInfo;

    private SSLSocketFactory sslSocketFactory;

    
    public HttpConnectorAddress() {
    }

    public HttpConnectorAddress(String host, int port) {
        this(host, port, false);
    }
    
    /**
     * construct an address which indicates the host, port and
     * security attributes desired.
     * @param host a host address
     * @param port a port number
     * @param secure a boolean indication of whether the connection should be
     *  secure (i.e. confidential) or not
     */
    public HttpConnectorAddress(String host, int port, boolean secure) {
        this(host, port, secure, null);
    }

    public HttpConnectorAddress(String host, int port, boolean secure, String path) {
        this(host, port, secure, path, null);
    }

    public HttpConnectorAddress(String host, int port, SSLSocketFactory sslSocketFactory) {
        this(host, port, true /* secure */, null /* path */, sslSocketFactory);
    }

    public HttpConnectorAddress(String host, int port, boolean secure, String path,
            SSLSocketFactory sslSocketFactory) {
        this.host = host;
        this.port = port;
        this.secure = secure;
        this.path = path;
        this.sslSocketFactory = sslSocketFactory;
    }


    /**
     * Open a connection using the reciever and the given path
     * @param path the path to the required resource (path here is
     * the portion after the hostname:port portion of a URL)
     * @return a connection to the required resource. The
     * connection returned may be a sub-class of
     * URLConnection including
     * HttpsURLConnection. If the sub-class is a
     * HttpsURLConnection then this connection will
     * accept any certificate from any server where the server's
     * name matches the host name of this object. Specifically we
     * allows the certificate not to contain the name of
     * the server. This is a potential security hole, but is also a
     * usability enhancement.
     * @throws IOException if there's a problem in connecting to the
     * resource
     */
    public URLConnection openConnection(String path) throws IOException {
        if (path == null || path.trim().length() == 0)
            path = this.path;
        final URLConnection cnx = this.openConnection(this.toURL(path));
        if (! (cnx instanceof HttpsURLConnection)) {
            return cnx;
        }

        configureSSL((HttpsURLConnection) cnx);
        
        return cnx;
    }

    private void configureSSL(final HttpsURLConnection httpsCnx) throws IOException {
        httpsCnx.setHostnameVerifier(new BasicHostnameVerifier(this.host));
        httpsCnx.setSSLSocketFactory(getOrCreateSSLSocketFactory());
    }

    private synchronized SSLSocketFactory getOrCreateSSLSocketFactory() throws IOException {
        /*
         * The SSL socket factory will have been assigned a value if this
         * connection was made from the DAS or an instance...that code would have
         * used the constructor which accepts an SSLSocketFactory as an argument.
         * (That socket factory should provide client authentication.)
         *
         * If that value is null then this connection is originating from
         * somewhere else - such as asadmin - and the socket factory should be
         * the one which uses SSL but does not provide client auth.
         */
        if (sslSocketFactory == null) {
            sslSocketFactory = createAdminSSLSocketFactory(null, null);
        }
        return sslSocketFactory;
    }

    private SSLSocketFactory createAdminSSLSocketFactory(String alias, String protocol) {

        try {
            if (protocol == null) {
                protocol = "TLSv1";
            }
            SSLContext cntxt = SSLContext.getInstance(protocol);
            /*
             * Pass null for the array of KeyManagers.  That uses the default
             * ones, so if the user has loaded client keys into the standard
             * Java SE keystore they will be found.
             */
            cntxt.init(null, new TrustManager[] {new AsadminTrustManager()}, null);

            return cntxt.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * get the protocol prefix to be used for a connection for the
     * receiver
     * @return the protocol prefix - one of http or
     *https depending upon the security setting.
     */
    public String getConnectorType() {
        return this.isSecure() ? HTTPS_CONNECTOR : HTTP_CONNECTOR;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public AuthenticationInfo getAuthenticationInfo() {
        return authInfo;
    }

    public void setAuthenticationInfo(AuthenticationInfo authInfo) {
        this.authInfo = authInfo;
    }

    /**
     * Set the security attibute
     */
    public void setSecure(boolean secure) {
        this.secure = secure;
    }
  

    /**
     * Indicate if the reciever represents a secure address
     */
    public boolean isSecure() {
        return secure;
    }

    public URL toURL(String path) throws MalformedURLException{
        return new URL(this.asURLSpec(path));
    }

    public SSLSocketFactory getSSLSocketFactory() {
        return sslSocketFactory;
    }
  
    private final String getUser() {
        return authInfo != null ? authInfo.getUser() : "";
    }

    private final String getPassword() {
        return authInfo != null ? authInfo.getPassword() : "";
    }

    /**
     * Return a string which can be used as the specification to
     * form an URL.
     * @return a string which can be used as the specification to
     * form an URL. This string is in the form of
     * >protocol>://>host>:>port>/ with the
     * appropriate substitutions
     */
    private final String asURLSpec(String path) {
        return this.getConnectorType()
        +"://"+this.getAuthority()
        +(path != null? path : "");
    }

    /**
     * Return the authority portion of the URL spec
     */
    private final String getAuthority() {
        return this.getHost() + ":" + this.getPort();
    }


    private final URLConnection openConnection(URL url) throws IOException    {
        return this.setOptions(this.makeConnection(url));
    }

    private final URLConnection makeConnection(URL url) throws IOException {
        return ( url.openConnection() );
    }

    private final URLConnection setOptions(URLConnection uc) {
        uc.setDoOutput(true);
        uc.setUseCaches(false);
        uc.setRequestProperty("Content-type", "application/octet-stream");
        uc.setRequestProperty("Connection", "Keep-Alive"); 
        return this.setAuthentication(uc);
    }

    private final URLConnection setAuthentication(URLConnection uc) {
        if (authInfo != null) {
            uc.setRequestProperty(AUTHORIZATION_KEY, this.getBasicAuthString());
        }
        return uc;
    }

    public final String getBasicAuthString() {
        /*
         * taking care of the descripancies in the Base64Encoder, for very
         * large lengths of passwords and/or usernames.
         * Abhijit did the analysis and as per his suggestion, replacing
         * a newline in Base64 encoded String by newline followed by a space
         * should work for any length of password, independent of the
         * web server buffer length. That investigation is still on, but
         * in the meanwhile, it was found that the replacement of newline
         * character with empty string "" works. Hence implementing the same.
         * Date: 10/10/2003.
         */
        String cs = null, user = this.getUser(), pass = this.getPassword();
        String up = (user == null) ? "" : user;
        String pp = (pass == null) ? "" : pass;
        cs = up + ":" + pp;
        String enc = this.getBase64Encoded(cs);
        enc = enc.replaceAll(System.getProperty("line.separator"), "");
        return ( AUTHORIZATION_TYPE + enc );
    }
  
    private final String getBase64Encoded(String clearString) {
        return new GFBase64Encoder().encode(clearString.getBytes());
    }

    private static class BasicHostnameVerifier implements HostnameVerifier {
        private final String host;
        public BasicHostnameVerifier(String host) {
            if (host == null)
                throw new IllegalArgumentException("null host");
            this.host = host;
        }

        public boolean verify(String s, SSLSession sslSession) {
            return host.equals(s);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy