Search in sources :

Example 1 with HttpCoreRequestResponseTransport

use of org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport in project wso2-synapse by wso2.

the class ServerWorker method createMessageContext.

/**
 * Create an Axis2 message context for the given http request. The request may be in the
 * process of being streamed
 *
 * @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);
    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;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HttpInetConnection(org.apache.http.HttpInetConnection) TreeMap(java.util.TreeMap) HttpCoreRequestResponseTransport(org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport) NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) SSLIOSession(org.apache.http.nio.reactor.ssl.SSLIOSession) MessageContext(org.apache.axis2.context.MessageContext) Map(java.util.Map) TreeMap(java.util.TreeMap) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 MessageContext (org.apache.axis2.context.MessageContext)1 HttpInetConnection (org.apache.http.HttpInetConnection)1 NHttpServerConnection (org.apache.http.nio.NHttpServerConnection)1 SSLIOSession (org.apache.http.nio.reactor.ssl.SSLIOSession)1 HttpCoreRequestResponseTransport (org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport)1