Example usage for org.apache.http.nio.reactor.ssl SSLIOSession SESSION_KEY

List of usage examples for org.apache.http.nio.reactor.ssl SSLIOSession SESSION_KEY

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor.ssl SSLIOSession SESSION_KEY.

Prototype

String SESSION_KEY

To view the source code for org.apache.http.nio.reactor.ssl SSLIOSession SESSION_KEY.

Click Source Link

Document

Name of the context attribute key, which can be used to obtain the SSL session.

Usage

From source file:org.aevans.goat.net.SSLStrategyGetter.java

public static SchemeIOSessionStrategy getSchemeIOSessionStrategy() {
    DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(
            PublicSuffixMatcherLoader.getDefault());
    SchemeIOSessionStrategy sioss = new SchemeIOSessionStrategy() {

        @Override/*from   w w w  . java2s.c o  m*/
        public boolean isLayeringRequired() {
            return true;
        }

        @Override
        public IOSession upgrade(final HttpHost host, final IOSession iosession) throws IOException {

            SSLSetupHandler handler = new SSLSetupHandler() {

                @Override
                public void initalize(SSLEngine sslengine) throws SSLException {
                }

                @Override
                public void verify(IOSession iosession, SSLSession sslsession) throws SSLException {
                    if (!hostnameVerifier.verify(host.getHostName(), sslsession)) {
                        final java.security.cert.Certificate[] certs = sslsession.getPeerCertificates();
                        final X509Certificate x509 = (X509Certificate) certs[0];
                        final X500Principal x500Principal = x509.getSubjectX500Principal();
                        throw new SSLPeerUnverifiedException("Host name '" + host.getHostName()
                                + "' does not match " + "the certificate subject provided by the peer ("
                                + x500Principal.toString() + ")");
                    }
                }

            };
            SSLBufferManagementStrategy sslbm = new ReleasableSSLBufferManagementStrategy();
            SSLIOSession ssio = new SSLIOSession(iosession, SSLMode.CLIENT, host, SSLContexts.createDefault(),
                    handler, sslbm);
            iosession.setAttribute(SSLIOSession.SESSION_KEY, ssio);
            ssio.initialize();
            return ssio;
        }

    };

    return sioss;
}

From source file:org.siddhiesb.transport.http.conn.ServerConnFactory.java

public DefaultNHttpServerConnection createConnection(final IOSession iosession) {
    org.siddhiesb.transport.http.conn.SSLContextDetails customSSL = null;
    if (sslByIPMap != null) {
        customSSL = sslByIPMap.get(iosession.getLocalAddress());
    }/*from   w ww . j a va2  s .c om*/
    if (customSSL == null) {
        customSSL = ssl;
    }
    IOSession customSession;
    if (customSSL != null) {
        customSession = new SSLIOSession(iosession, SSLMode.SERVER, customSSL.getContext(),
                customSSL.getHandler());
        iosession.setAttribute(SSLIOSession.SESSION_KEY, customSession);
    } else {
        customSession = iosession;
    }
    DefaultNHttpServerConnection conn = LoggingUtils.createServerConnection(customSession, requestFactory,
            allocator, params);
    int timeout = HttpConnectionParams.getSoTimeout(params);
    conn.setSocketTimeout(timeout);
    return conn;
}

From source file:org.siddhiesb.transport.http.conn.ClientConnFactory.java

public DefaultNHttpClientConnection createConnection(final IOSession iosession, final HttpRoute route) {
    IOSession customSession;//from  www  .ja  v  a  2 s .c  o  m
    if (ssl != null && route.isSecure() && !route.isTunnelled()) {
        SSLContext customContext = getSSLContext(iosession);
        SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.CLIENT, customContext,
                ssl.getHandler());
        iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
        customSession = ssliosession;
    } else {
        customSession = iosession;
    }
    DefaultNHttpClientConnection conn = org.siddhiesb.transport.http.conn.LoggingUtils
            .createClientConnection(customSession, responseFactory, allocator, params);
    int timeout = HttpConnectionParams.getSoTimeout(params);
    conn.setSocketTimeout(timeout);
    return conn;
}

From source file:org.siddhiesb.transport.http.conn.ClientConnFactory.java

public void upgrade(final UpgradableNHttpConnection conn) {
    if (ssl != null) {
        IOSession iosession = conn.getIOSession();
        if (!(iosession instanceof SSLIOSession)) {
            SSLContext customContext = getSSLContext(iosession);
            SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.CLIENT, customContext,
                    ssl.getHandler());//from  w ww. j  av a  2 s .c  om
            iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
            conn.bind(ssliosession);
        }
    }
}

From source file:org.apache.synapse.transport.nhttp.ServerWorker.java

/**
 * Create an Axis2 message context for the given http request. The request may be in the
 * process of being streamed// w  w w.j ava2s  .  co  m
 * @param request the http request to be used to create the corresponding Axis2 message context
 * @return the Axis2 message context created
 */
private MessageContext createMessageContext(HttpRequest request) {

    MessageContext msgContext = new MessageContext();
    msgContext.setMessageID(UIDGenerator.generateURNString());

    // There is a discrepency in what I thought, Axis2 spawns a new threads to
    // send a message if this is TRUE - and I want it to be the other way
    msgContext.setProperty(MessageContext.CLIENT_API_NON_BLOCKING, Boolean.FALSE);
    msgContext.setConfigurationContext(cfgCtx);
    if ("https".equalsIgnoreCase(schemeName)) {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTPS));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTPS));
        msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTPS);

        SSLIOSession session = (SSLIOSession) (conn.getContext()).getAttribute(SSLIOSession.SESSION_KEY);
        //set SSL certificates to message context if SSLVerifyClient parameter is set
        if (session != null && msgContext.getTransportIn() != null
                && msgContext.getTransportIn().getParameter(NhttpConstants.SSL_VERIFY_CLIENT) != null) {
            try {
                msgContext.setProperty(NhttpConstants.SSL_CLIENT_AUTH_CERT_X509,
                        session.getSSLSession().getPeerCertificateChain());
            } catch (SSLPeerUnverifiedException e) {
                //Peer Certificate Chain may not be available always.(in case of verify client is optional)
                if (log.isTraceEnabled()) {
                    log.trace("Peer certificate chain is not available for MsgContext "
                            + msgContext.getMessageID());
                }
            }
        }
    } else {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTP));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTP));
        msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
    }
    msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);
    // the following statement causes the soap session services to be failing - ruwan        
    // msgContext.setServiceGroupContextId(UUIDGenerator.getUUID());
    msgContext.setServerSide(true);
    msgContext.setProperty(Constants.Configuration.TRANSPORT_IN_URL, request.getRequestLine().getUri());

    // http transport header names are case insensitive 
    Map<String, String> headers = new TreeMap<String, String>(new Comparator<String>() {
        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    });

    for (Header header : request.getAllHeaders()) {

        String headerName = header.getName();

        // if this header is already added
        if (headers.containsKey(headerName)) {
            /* this is a multi-value header */
            // generate the key
            String key = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
            // get the old value
            String oldValue = headers.get(headerName);
            // adds additional values to a list in a property of message context
            Map map;
            if (msgContext.getProperty(key) != null) {
                map = (Map) msgContext.getProperty(key);
                map.put(headerName, oldValue);
            } else {
                map = new MultiValueMap();
                map.put(headerName, oldValue);
                // set as a property in message context
                msgContext.setProperty(key, map);
            }

        }
        headers.put(header.getName(), header.getValue());
    }
    msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, headers);

    // find the remote party IP address and set it to the message context
    if (conn instanceof HttpInetConnection) {
        HttpContext httpContext = conn.getContext();
        HttpInetConnection inetConn = (HttpInetConnection) conn;
        InetAddress remoteAddr = inetConn.getRemoteAddress();
        if (remoteAddr != null) {
            httpContext.setAttribute(NhttpConstants.CLIENT_REMOTE_ADDR, remoteAddr);
            httpContext.setAttribute(NhttpConstants.CLIENT_REMOTE_PORT, inetConn.getRemotePort());
            msgContext.setProperty(MessageContext.REMOTE_ADDR, remoteAddr.getHostAddress());
            msgContext.setProperty(NhttpConstants.REMOTE_HOST, NhttpUtil.getHostName(remoteAddr));
            remoteAddress = remoteAddr.getHostAddress();
        }
    }

    msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL,
            new HttpCoreRequestResponseTransport(msgContext));

    msgContext.setProperty(ServerHandler.SERVER_CONNECTION_DEBUG,
            conn.getContext().getAttribute(ServerHandler.SERVER_CONNECTION_DEBUG));

    msgContext.setProperty(NhttpConstants.NHTTP_INPUT_STREAM, is);
    msgContext.setProperty(NhttpConstants.NHTTP_OUTPUT_STREAM, os);

    return msgContext;
}

From source file:org.apache.synapse.transport.http.conn.ClientConnFactory.java

public DefaultNHttpClientConnection createConnection(final IOSession iosession, final HttpRoute route) {
    IOSession customSession;/*from  ww  w. j a v  a 2  s  .c  o  m*/
    if (ssl != null && route.isSecure() && !route.isTunnelled()) {
        SSLContext customContext = getSSLContext(iosession);
        SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.CLIENT, customContext,
                ssl.getHandler());
        iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
        customSession = ssliosession;
    } else {
        if (route != null && route.isTunnelled()) {
            org.apache.http.HttpHost httpHost = route.getTargetHost();
            String beAddress = null;
            String proxyAdd = null;
            if (httpHost != null) {
                String hostname = httpHost.getHostName();
                int port = httpHost.getPort();
                beAddress = hostname + ":" + port;
            }

            org.apache.http.HttpHost proxyHost = route.getProxyHost();
            if (proxyHost != null) {
                String proxyHostName = proxyHost.getHostName();
                int proxyPort = proxyHost.getPort();
                proxyAdd = proxyHostName + ":" + proxyPort;
            }

            if (sslByHostMap != null && sslByHostMap.containsKey(beAddress)) {
                SSLContext beCtx = sslByHostMap.get(beAddress);
                sslByHostMap.put(proxyAdd, beCtx);
            }
        }
        customSession = iosession;
    }
    DefaultNHttpClientConnection conn = LoggingUtils.createClientConnection(customSession, responseFactory,
            allocator, params);
    int timeout = HttpConnectionParams.getSoTimeout(params);
    conn.setSocketTimeout(timeout);
    return conn;
}

From source file:org.apache.synapse.transport.passthru.ServerWorker.java

/**
 * Create an Axis2 message context for the given http request. The request may be in the
 * process of being streamed/*  ww  w .  j a  v  a2s.  c  o  m*/
 *
 * @param request the http request to be used to create the corresponding Axis2 message context
 * @return the Axis2 message context created
 */
public MessageContext createMessageContext(MessageContext msgContext, SourceRequest request) {

    Map excessHeaders = request.getExcessHeaders();
    ConfigurationContext cfgCtx = sourceConfiguration.getConfigurationContext();

    if (msgContext == null) {
        msgContext = new MessageContext();
    }
    msgContext.setMessageID(UIDGenerator.generateURNString());

    // Axis2 spawns a new threads to send a message if this is TRUE - and it has to
    // be the other way
    msgContext.setProperty(MessageContext.CLIENT_API_NON_BLOCKING, Boolean.FALSE);
    msgContext.setConfigurationContext(cfgCtx);

    //        msgContext.setTransportOut(cfgCtx.getAxisConfiguration()
    //                .getTransportOut(Constants.TRANSPORT_HTTP));
    //        msgContext.setTransportIn(cfgCtx.getAxisConfiguration()
    //                .getTransportIn(Constants.TRANSPORT_HTTP));
    //        msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
    //        msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);

    NHttpServerConnection conn = request.getConnection();

    if (sourceConfiguration.getScheme().isSSL()) {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTPS));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTPS));
        msgContext.setIncomingTransportName(sourceConfiguration.getInDescription() != null
                ? sourceConfiguration.getInDescription().getName()
                : Constants.TRANSPORT_HTTPS);

        SSLIOSession ssliosession = (SSLIOSession) (conn.getContext()).getAttribute(SSLIOSession.SESSION_KEY);
        //set SSL certificates to message context if SSLVerifyClient parameter is set
        if (ssliosession != null && msgContext.getTransportIn() != null
                && msgContext.getTransportIn().getParameter(NhttpConstants.SSL_VERIFY_CLIENT) != null) {
            try {
                msgContext.setProperty(NhttpConstants.SSL_CLIENT_AUTH_CERT_X509,
                        ssliosession.getSSLSession().getPeerCertificateChain());
            } catch (SSLPeerUnverifiedException e) {
                //Peer Certificate Chain may not be available always.(in case of Mutual SSL is not enabled)
                if (log.isTraceEnabled()) {
                    log.trace("Peer certificate chain is not available for MsgContext "
                            + msgContext.getMessageID());
                }
            }
        }
    } else {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTP));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTP));
        msgContext.setIncomingTransportName(sourceConfiguration.getInDescription() != null
                ? sourceConfiguration.getInDescription().getName()
                : Constants.TRANSPORT_HTTP);

    }

    msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);
    msgContext.setServerSide(true);
    msgContext.setProperty(Constants.Configuration.TRANSPORT_IN_URL, request.getUri());

    // http transport header names are case insensitive
    Map<String, String> headers = new TreeMap<String, String>(new Comparator<String>() {
        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    });

    Set<Map.Entry<String, String>> entries = request.getHeaders().entrySet();
    for (Map.Entry<String, String> entry : entries) {
        headers.put(entry.getKey(), entry.getValue());
    }
    msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, headers);
    msgContext.setProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS, excessHeaders);

    // Following section is required for throttling to work

    if (conn instanceof HttpInetConnection) {
        HttpInetConnection netConn = (HttpInetConnection) conn;
        InetAddress remoteAddress = netConn.getRemoteAddress();
        if (remoteAddress != null) {
            msgContext.setProperty(MessageContext.REMOTE_ADDR, remoteAddress.getHostAddress());
            msgContext.setProperty(NhttpConstants.REMOTE_HOST, NhttpUtil.getHostName(remoteAddress));
        }
    }

    msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL,
            new HttpCoreRequestResponseTransport(msgContext));

    return msgContext;
}