Search in sources :

Example 11 with Pipe

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

the class MessageHelper method cloneAxis2MessageContextForAggregate.

private static org.apache.axis2.context.MessageContext cloneAxis2MessageContextForAggregate(org.apache.axis2.context.MessageContext mc) throws AxisFault {
    org.apache.axis2.context.MessageContext newMC = clonePartiallyForAggregate(mc);
    newMC.setEnvelope(cloneSOAPEnvelope(mc.getEnvelope()));
    newMC.setOptions(cloneOptions(mc.getOptions()));
    newMC.setServiceContext(mc.getServiceContext());
    newMC.setOperationContext(mc.getOperationContext());
    newMC.setAxisMessage(mc.getAxisMessage());
    if (newMC.getAxisMessage() != null) {
        newMC.getAxisMessage().setParent(mc.getAxisOperation());
    }
    newMC.setAxisService(mc.getAxisService());
    // copying transport related parts from the original
    newMC.setTransportIn(mc.getTransportIn());
    newMC.setTransportOut(mc.getTransportOut());
    newMC.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, mc.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
    newMC.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, getClonedTransportHeaders(mc));
    if (newMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE) != null) {
        // clone passthrough pipe here..writer...
        // newMC.setProperty(PassThroughConstants.CLONE_PASS_THROUGH_PIPE_REQUEST,true);
        NHttpServerConnection conn = (NHttpServerConnection) newMC.getProperty("pass-through.Source-Connection");
        if (conn != null) {
            SourceConfiguration sourceConfiguration = (SourceConfiguration) newMC.getProperty("PASS_THROUGH_SOURCE_CONFIGURATION");
            Pipe pipe = new Pipe(conn, sourceConfiguration.getBufferFactory().getBuffer(), "source", sourceConfiguration);
            newMC.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe);
        }
    }
    return newMC;
}
Also used : NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) SourceConfiguration(org.apache.synapse.transport.passthru.config.SourceConfiguration) Pipe(org.apache.synapse.transport.passthru.Pipe)

Example 12 with Pipe

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

the class RelayUtils method buildMessage.

public static void buildMessage(MessageContext messageContext, boolean earlyBuild) throws IOException, XMLStreamException {
    final Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (messageContext.getProperty(Constants.Configuration.CONTENT_TYPE) != null) {
        if (log.isDebugEnabled()) {
            log.debug("Content Type is " + messageContext.getProperty(Constants.Configuration.CONTENT_TYPE));
        }
        if (pipe != null && !Boolean.TRUE.equals(messageContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED)) && forcePTBuild) {
            InputStream in = pipe.getInputStream();
            Object http_sc = messageContext.getProperty(NhttpConstants.HTTP_SC);
            if (http_sc != null && http_sc instanceof Integer && http_sc.equals(202)) {
                if (in != null) {
                    InputStream bis = new ReadOnlyBIS(in);
                    int c = bis.read();
                    if (c == -1) {
                        messageContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
                        messageContext.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
                        return;
                    }
                    bis.reset();
                    in = bis;
                }
            }
            builldMessage(messageContext, earlyBuild, in);
            return;
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Content Type is null and the message is not build");
        }
        messageContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
        return;
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Pipe(org.apache.synapse.transport.passthru.Pipe)

Example 13 with Pipe

use of org.apache.synapse.transport.passthru.Pipe 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

Pipe (org.apache.synapse.transport.passthru.Pipe)13 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 NHttpServerConnection (org.apache.http.nio.NHttpServerConnection)3 SourceConfiguration (org.apache.synapse.transport.passthru.config.SourceConfiguration)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Parameter (org.apache.axis2.description.Parameter)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 HashMap (java.util.HashMap)1 Stack (java.util.Stack)1