Search in sources :

Example 11 with MultiValueMap

use of org.apache.commons.collections.map.MultiValueMap in project wso2-synapse by wso2.

the class ClientHandler method setHeaders.

private void setHeaders(HttpContext context, HttpResponse response, MessageContext outMsgCtx, MessageContext responseMsgCtx) {
    Header[] headers = response.getAllHeaders();
    if (headers != null && headers.length > 0) {
        Map<String, String> headerMap = new TreeMap<String, String>(new Comparator<String>() {

            public int compare(String o1, String o2) {
                return o1.compareToIgnoreCase(o2);
            }
        });
        String endpointURLPrefix = (String) context.getAttribute(NhttpConstants.ENDPOINT_PREFIX);
        String servicePrefix = (String) outMsgCtx.getProperty(NhttpConstants.SERVICE_PREFIX);
        for (int i = 0; i < headers.length; i++) {
            Header header = headers[i];
            // if this header is already added
            if (headerMap.containsKey(header.getName())) {
                /* this is a multi-value header */
                // generate the key
                String key = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
                // get the old value
                String oldValue = headerMap.get(header.getName());
                // adds additional values to a list in a property of message
                // context
                Map map;
                if (responseMsgCtx.getProperty(key) != null) {
                    map = (Map) responseMsgCtx.getProperty(key);
                    map.put(header.getName(), oldValue);
                } else {
                    map = new MultiValueMap();
                    map.put(header.getName(), oldValue);
                    // set as a property in message context
                    responseMsgCtx.setProperty(key, map);
                }
            }
            if ("Location".equals(header.getName()) && endpointURLPrefix != null && servicePrefix != null) {
                // name and the port.
                try {
                    URI serviceURI = new URI(servicePrefix);
                    URI endpointURI = new URI(endpointURLPrefix);
                    URI locationURI = new URI(header.getValue());
                    if (locationURI.getHost().equalsIgnoreCase(endpointURI.getHost())) {
                        URI newURI = new URI(locationURI.getScheme(), locationURI.getUserInfo(), serviceURI.getHost(), serviceURI.getPort(), locationURI.getPath(), locationURI.getQuery(), locationURI.getFragment());
                        headerMap.put(header.getName(), newURI.toString());
                        responseMsgCtx.setProperty(NhttpConstants.SERVICE_PREFIX, outMsgCtx.getProperty(NhttpConstants.SERVICE_PREFIX));
                    }
                } catch (URISyntaxException e) {
                    log.error(e.getMessage(), e);
                }
            } else {
                headerMap.put(header.getName(), header.getValue());
            }
        }
        responseMsgCtx.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap);
    }
}
Also used : Header(org.apache.http.Header) URISyntaxException(java.net.URISyntaxException) TreeMap(java.util.TreeMap) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) URI(java.net.URI) MultiValueMap(org.apache.commons.collections.map.MultiValueMap)

Example 12 with MultiValueMap

use of org.apache.commons.collections.map.MultiValueMap 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
 */
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;
}
Also used : SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HttpContext(org.apache.http.protocol.HttpContext) SSLIOSession(org.apache.http.nio.reactor.ssl.SSLIOSession) MessageContext(org.apache.axis2.context.MessageContext) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) InetAddress(java.net.InetAddress) MultiValueMap(org.apache.commons.collections.map.MultiValueMap)

Aggregations

MultiValueMap (org.apache.commons.collections.map.MultiValueMap)12 Map (java.util.Map)5 HashMap (java.util.HashMap)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MultiMap (org.apache.commons.collections.MultiMap)3 IOException (java.io.IOException)2 RandomAccessFile (java.io.RandomAccessFile)2 LinkedHashMap (java.util.LinkedHashMap)2 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)2 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)2 Document (org.alfresco.rest.api.tests.client.data.Document)2 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)2 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)2 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)2 PromotionMessageDTO (org.broadleafcommerce.core.promotionMessage.dto.PromotionMessageDTO)2 Test (org.junit.Test)2 ServiceDescriptor (com.emc.sa.descriptor.ServiceDescriptor)1 ServiceField (com.emc.sa.descriptor.ServiceField)1