Search in sources :

Example 1 with Pipe

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

the class MessageHelper method cloneAxis2MessageContext.

/**
 * This method will simulate cloning the message context and creating an exact copy of the
 * passed message. One should use this method with care; that is because, inside the new MC,
 * most of the attributes of the MC like opCtx and so on are still kept as references. Otherwise
 * there will be perf issues. But ..... this may reveal in some conflicts in the cloned message
 * if you try to do advanced mediations with the cloned message, in which case you should
 * manually get a clone of the changing part of the MC and set that cloned part to your MC.
 * Changing the MC after doing that will solve most of the issues. (Note: You don't have to worry
 * about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
 *
 * @param mc - this will be cloned for getting an exact copy
 * @param cloneSoapEnvelope The flag to say whether to clone the SOAP envelope or not.
 * @return cloned MessageContext from the given mc
 * @throws AxisFault if there is a failure in copying the certain attributes of the
 *          provided message context
 */
public static org.apache.axis2.context.MessageContext cloneAxis2MessageContext(org.apache.axis2.context.MessageContext mc, boolean cloneSoapEnvelope) throws AxisFault {
    // empty buffer in PASS_THROUGH_PIPE without the message payload.
    try {
        RelayUtils.buildMessage(mc, false);
    } catch (IOException e) {
        handleException(e);
    } catch (XMLStreamException e) {
        handleException(e);
    }
    org.apache.axis2.context.MessageContext newMC = clonePartially(mc);
    if (cloneSoapEnvelope) {
        newMC.setEnvelope(cloneSOAPEnvelope(mc.getEnvelope()));
    }
    // XXX: always this section must come after the above step. ie. after applying Envelope.
    // That is to get the existing headers into the new envelope.
    JsonUtil.cloneJsonPayload(mc, newMC);
    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);
        } else {
            newMC.removeProperty(PassThroughConstants.PASS_THROUGH_PIPE);
        }
    }
    return newMC;
}
Also used : NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) XMLStreamException(javax.xml.stream.XMLStreamException) SourceConfiguration(org.apache.synapse.transport.passthru.config.SourceConfiguration) IOException(java.io.IOException) Pipe(org.apache.synapse.transport.passthru.Pipe)

Example 2 with Pipe

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

the class FailoverEndpoint method sendMessage.

private void sendMessage(MessageContext synCtx) {
    logSetter();
    if (log.isDebugEnabled()) {
        log.debug("Failover Endpoint : " + getName());
    }
    if (getContext().isState(EndpointContext.ST_OFF)) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "Failover endpoint : " + getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT + " - is inactive");
        return;
    }
    boolean isARetry = false;
    Map<String, Integer> mEndpointLog = null;
    if (synCtx.getProperty(SynapseConstants.LAST_ENDPOINT) == null) {
        if (log.isDebugEnabled()) {
            log.debug(this + " Building the SoapEnvelope");
        }
        // If buildMessage attribute available in failover config it is honoured, else global property is considered
        if (isBuildMessageAttAvailable) {
            if (buildMessageAtt) {
                buildMessage(synCtx);
            }
        } else if (buildMessage) {
            buildMessage(synCtx);
        }
        synCtx.getEnvelope().buildWithAttachments();
        // If the endpoint failed during the sending, we need to keep the original envelope and reuse that for other endpoints
        if (Boolean.TRUE.equals(((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
            synCtx.setProperty(SynapseConstants.LB_FO_ENDPOINT_ORIGINAL_MESSAGE, synCtx.getEnvelope());
        }
        mEndpointLog = new HashMap<String, Integer>();
        synCtx.setProperty(SynapseConstants.ENDPOINT_LOG, mEndpointLog);
    } else {
        isARetry = true;
        mEndpointLog = (Map<String, Integer>) synCtx.getProperty(SynapseConstants.ENDPOINT_LOG);
    }
    if (getChildren().isEmpty()) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "FailoverLoadbalance endpoint : " + getName() + " - no child endpoints");
        return;
    }
    // evaluate the endpoint properties
    evaluateProperties(synCtx);
    if (dynamic) {
        // Dynamic fail-over mode - Switch to a backup endpoint when an error occurs
        // in the primary endpoint. But switch back to the primary as soon as it becomes
        // active again.
        boolean foundEndpoint = false;
        for (Endpoint endpoint : getChildren()) {
            if (endpoint.readyToSend()) {
                foundEndpoint = true;
                if (isARetry && metricsMBean != null) {
                    metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                }
                synCtx.pushFaultHandler(this);
                if (endpoint instanceof AbstractEndpoint) {
                    org.apache.axis2.context.MessageContext axisMC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                    Pipe pipe = (Pipe) axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                    if (pipe != null) {
                        pipe.forceSetSerializationRest();
                    }
                    // allow the message to be content aware if the given message comes via PT
                    if (axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE) != null) {
                        ((AbstractEndpoint) endpoint).setContentAware(true);
                        ((AbstractEndpoint) endpoint).setForceBuildMC(true);
                        if (endpoint instanceof TemplateEndpoint && ((TemplateEndpoint) endpoint).getRealEndpoint() != null) {
                            if (((TemplateEndpoint) endpoint).getRealEndpoint() instanceof AbstractEndpoint) {
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setContentAware(true);
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setForceBuildMC(true);
                            }
                        }
                    }
                }
                if (endpoint.getName() != null) {
                    mEndpointLog.put(endpoint.getName(), null);
                }
                endpoint.send(synCtx);
                break;
            }
        }
        if (!foundEndpoint) {
            String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
            log.warn(msg);
            informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
        }
    } else {
        if (currentEndpoint == null) {
            currentEndpoint = getChildren().get(0);
        }
        if (currentEndpoint.readyToSend()) {
            if (isARetry && metricsMBean != null) {
                metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
            }
            synCtx.pushFaultHandler(this);
            currentEndpoint.send(synCtx);
        } else {
            boolean foundEndpoint = false;
            for (Endpoint endpoint : getChildren()) {
                if (endpoint.readyToSend()) {
                    foundEndpoint = true;
                    currentEndpoint = endpoint;
                    if (isARetry && metricsMBean != null) {
                        metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                    }
                    synCtx.pushFaultHandler(this);
                    currentEndpoint.send(synCtx);
                    break;
                }
            }
            if (!foundEndpoint) {
                String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
                log.warn(msg);
                informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
            }
        }
    }
}
Also used : Pipe(org.apache.synapse.transport.passthru.Pipe) Integer(org.apache.axis2.databinding.types.soapencoding.Integer) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with Pipe

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

the class DefaultEndpoint method send.

public void send(MessageContext synCtx) {
    // For setting Car name (still for Proxy)
    logSetter();
    org.apache.axis2.context.MessageContext messageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    final Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null && !Boolean.TRUE.equals(messageContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED)) && messageContext.getProperty("To") == null) {
        try {
            RelayUtils.buildMessage(((Axis2MessageContext) synCtx).getAxis2MessageContext(), false);
        } catch (Exception e) {
            handleException("Error while building message", e);
        }
    }
    if (getParentEndpoint() == null && !readyToSend()) {
        // if the this leaf endpoint is too a root endpoint and is in inactive
        informFailure(synCtx, SynapseConstants.ENDPOINT_ADDRESS_NONE_READY, "Currently , Default endpoint : " + getContext());
    } else {
        super.send(synCtx);
    }
}
Also used : Pipe(org.apache.synapse.transport.passthru.Pipe) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 4 with Pipe

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

the class RelaySecuirtyMessageBuilderDispatchandler method invoke.

@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
    InvocationResponse invocationResponse = super.invoke(messageContext);
    EndpointReference toEPR = messageContext.getTo();
    Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null) {
        if (toEPR != null) {
            ConfigurationContext configurationContext = messageContext.getConfigurationContext();
            AxisConfiguration registry = configurationContext.getAxisConfiguration();
            String filePart = toEPR.getAddress();
            if (filePart != null) {
                String serviceOpPart = Utils.getServiceAndOperationPart(filePart, messageContext.getConfigurationContext().getServiceContextPath());
                AxisService axisService = null;
                // only service context path onwards values will be taken
                if (messageContext.getConfigurationContext().getServiceContextPath() != null && serviceOpPart != null) {
                    axisService = registry.getService(serviceOpPart);
                    if (axisService != null) {
                        Parameter parameter = axisService.getParameter(SERVICE_TYPE);
                        if (parameter != null) {
                            if (!parameter.getValue().equals(PROXY)) {
                                build(messageContext);
                            }
                        } else {
                            build(messageContext);
                        }
                    }
                }
            }
        }
        if (messageContext.isEngaged(PassThroughConstants.SECURITY_MODULE_NAME)) {
            SOAPHeader header = null;
            if (messageContext.getEnvelope().getHeader() != null) {
                header = messageContext.getEnvelope().getHeader();
            }
            build(messageContext);
            this.handlePOXRequests(messageContext, header);
        }
    }
    return invocationResponse;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AxisService(org.apache.axis2.description.AxisService) Parameter(org.apache.axis2.description.Parameter) Pipe(org.apache.synapse.transport.passthru.Pipe) SOAPHeader(org.apache.axiom.soap.SOAPHeader) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 5 with Pipe

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

the class SourceResponseFactory method create.

public static SourceResponse create(MessageContext msgContext, SourceRequest sourceRequest, SourceConfiguration sourceConfiguration) {
    // determine the status code to be sent
    int statusCode = PassThroughTransportUtils.determineHttpStatusCode(msgContext);
    SourceResponse sourceResponse;
    String statusLine = PassThroughTransportUtils.determineHttpStatusLine(msgContext);
    if (msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_SC) != null && statusCode == ((Integer) msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_SC))) {
        sourceResponse = new SourceResponse(sourceConfiguration, statusCode, statusLine, sourceRequest);
    } else {
        if (msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_REASON_PHRASE) != null && (statusLine.equals(msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_REASON_PHRASE)))) {
            sourceResponse = new SourceResponse(sourceConfiguration, statusCode, sourceRequest);
        } else {
            sourceResponse = new SourceResponse(sourceConfiguration, statusCode, statusLine, sourceRequest);
        }
    }
    // set any transport headers
    Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
    boolean forceContentLength = msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_CONTENT_LENGTH);
    boolean forceContentLengthCopy = msgContext.isPropertyTrue(PassThroughConstants.COPY_CONTENT_LENGTH_FROM_INCOMING);
    if (forceContentLength && forceContentLengthCopy && msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH) != null) {
        sourceResponse.addHeader(HTTP.CONTENT_LEN, (String) msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH));
    }
    // set to sourceResponse.
    if (sourceRequest != null && PassThroughConstants.HTTP_HEAD.equalsIgnoreCase(sourceRequest.getRequest().getRequestLine().getMethod()) && msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH) != null) {
        sourceResponse.addHeader(PassThroughConstants.ORGINAL_CONTEN_LENGTH, (String) msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH));
    }
    if (transportHeaders != null && msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) != null) {
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString().contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
            transportHeaders.put(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
        } else {
            Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
            if (pipe != null && !Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
                transportHeaders.put(HTTP.CONTENT_TYPE, msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE));
            }
        }
    }
    if (transportHeaders != null) {
        addResponseHeader(sourceResponse, transportHeaders);
    } else {
        Boolean noEntityBody = (Boolean) msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY);
        if (noEntityBody == null || Boolean.FALSE == noEntityBody) {
            OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
            transportHeaders = new HashMap();
            MessageFormatter messageFormatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
            if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) == null) {
                transportHeaders.put(HTTP.CONTENT_TYPE, messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
            }
            addResponseHeader(sourceResponse, transportHeaders);
        }
    }
    // Add excess response 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)) {
                sourceResponse.addHeader(key, (String) excessVal);
            }
        }
    }
    // keep alive
    String noKeepAlive = (String) msgContext.getProperty(PassThroughConstants.NO_KEEPALIVE);
    if ("true".equals(noKeepAlive) || PassThroughConfiguration.getInstance().isKeepAliveDisabled()) {
        sourceResponse.setKeepAlive(false);
    } else {
        // that, disable keep-alive to avoid re-using the existing connection by client for the next request.
        if (sourceRequest != null) {
            String requestMethod = sourceRequest.getRequest().getRequestLine().getMethod();
            if (requestMethod != null && isPayloadOptionalMethod(requestMethod.toUpperCase()) && (sourceRequest.getHeaders().containsKey(HTTP.CONTENT_LEN) || sourceRequest.getHeaders().containsKey(HTTP.TRANSFER_ENCODING))) {
                if (log.isDebugEnabled()) {
                    log.debug("Disable keep-alive in the client connection : Content-length/Transfer-encoding" + " headers present for GET/HEAD/DELETE request");
                }
                sourceResponse.setKeepAlive(false);
            }
        }
    }
    return sourceResponse;
}
Also used : SourceResponse(org.apache.synapse.transport.passthru.SourceResponse) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Collection(java.util.Collection) Pipe(org.apache.synapse.transport.passthru.Pipe) MessageFormatter(org.apache.axis2.transport.MessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) HashMap(java.util.HashMap) Map(java.util.Map)

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