Search in sources :

Example 1 with TargetRequest

use of org.apache.synapse.transport.passthru.TargetRequest in project wso2-synapse by wso2.

the class TargetRequestFactory method create.

public static TargetRequest create(MessageContext msgContext, HttpRoute route, TargetConfiguration configuration) throws AxisFault {
    try {
        String httpMethod = (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
        if (httpMethod == null) {
            httpMethod = "POST";
        }
        // basic request
        Boolean noEntityBody = (Boolean) msgContext.getProperty(PassThroughConstants.NO_ENTITY_BODY);
        if (msgContext.getEnvelope().getBody().getFirstElement() != null) {
            noEntityBody = false;
        }
        EndpointReference epr = PassThroughTransportUtils.getDestinationEPR(msgContext);
        URL url = new URL(epr.getAddress());
        TargetRequest request = new TargetRequest(configuration, route, url, httpMethod, noEntityBody == null || !noEntityBody);
        // otherwise Host header will not replaced after first call
        if (msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER) != null) {
            Object headers = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
            if (headers != null) {
                Map headersMap = (Map) headers;
                if (!headersMap.containsKey(HTTPConstants.HEADER_HOST)) {
                    headersMap.put(HTTPConstants.HEADER_HOST, msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER));
                }
            }
        }
        // headers
        PassThroughTransportUtils.removeUnwantedHeaders(msgContext, configuration);
        Object o = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
        if (o != null && o instanceof Map) {
            Map headers = (Map) o;
            for (Object entryObj : headers.entrySet()) {
                Map.Entry entry = (Map.Entry) entryObj;
                if (entry.getValue() != null && entry.getKey() instanceof String && entry.getValue() instanceof String) {
                    if (HTTPConstants.HEADER_HOST.equalsIgnoreCase((String) entry.getKey()) && !configuration.isPreserveHttpHeader(HTTPConstants.HEADER_HOST)) {
                        if (msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER) != null) {
                            request.addHeader((String) entry.getKey(), (String) msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER));
                        }
                    } else {
                        request.addHeader((String) entry.getKey(), (String) entry.getValue());
                    }
                }
            }
        }
        String cType = getContentType(msgContext, configuration.isPreserveHttpHeader(HTTP.CONTENT_TYPE));
        if (cType != null && (!httpMethod.equals(HTTPConstants.HTTP_METHOD_GET) && !RelayUtils.isDeleteRequestWithoutPayload(msgContext))) {
            String messageType = (String) msgContext.getProperty(NhttpConstants.MESSAGE_TYPE);
            if (messageType != null) {
                boolean builderInvoked = false;
                final Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                if (pipe != null) {
                    builderInvoked = Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED));
                }
                // skip of setting formatter specific content Type
                if (messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED) == -1 && messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA) == -1) {
                    Map msgCtxheaders = (Map) o;
                    if (msgCtxheaders != null && !cType.isEmpty()) {
                        msgCtxheaders.put(HTTP.CONTENT_TYPE, cType);
                    }
                    request.addHeader(HTTP.CONTENT_TYPE, cType);
                }
                // boundary related content type at Content-Type header
                if (builderInvoked && (((messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED) != -1) || (messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA) != -1)))) {
                    request.addHeader(HTTP.CONTENT_TYPE, cType);
                }
            } else {
                request.addHeader(HTTP.CONTENT_TYPE, cType);
            }
        }
        // version
        String forceHttp10 = (String) msgContext.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
        if ("true".equals(forceHttp10)) {
            request.setVersion(HttpVersion.HTTP_1_0);
        }
        // keep alive
        String noKeepAlive = (String) msgContext.getProperty(PassThroughConstants.NO_KEEPALIVE);
        if ("true".equals(noKeepAlive) || PassThroughConfiguration.getInstance().isKeepAliveDisabled()) {
            request.setKeepAlive(false);
        }
        // port
        int port = url.getPort();
        request.setPort(port);
        // chunk
        String disableChunking = (String) msgContext.getProperty(PassThroughConstants.DISABLE_CHUNKING);
        if ("true".equals(disableChunking)) {
            request.setChunk(false);
        }
        // full url
        String fullUrl = (String) msgContext.getProperty(PassThroughConstants.FULL_URI);
        if ("true".equals(fullUrl)) {
            request.setFullUrl(true);
        }
        // Add excess respsonse header.
        String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
        Map excessHeaders = (Map) msgContext.getProperty(excessProp);
        if (excessHeaders != null) {
            for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext(); ) {
                String key = (String) iterator.next();
                for (String excessVal : (Collection<String>) excessHeaders.get(key)) {
                    request.addHeader(key, (String) excessVal);
                }
            }
        }
        return request;
    } catch (MalformedURLException e) {
        handleException("Invalid to address" + msgContext.getTo().getAddress(), e);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) Pipe(org.apache.synapse.transport.passthru.Pipe) URL(java.net.URL) EndpointReference(org.apache.axis2.addressing.EndpointReference) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map) TargetRequest(org.apache.synapse.transport.passthru.TargetRequest)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 Pipe (org.apache.synapse.transport.passthru.Pipe)1 TargetRequest (org.apache.synapse.transport.passthru.TargetRequest)1