Search in sources :

Example 36 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class FilterMediator method mediate.

/**
 * Executes the list of sub/child mediators, if the filter condition is satisfied
 *
 * @param synCtx the current message
 * @return true if filter condition fails. else returns as per List mediator semantics
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Filter mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    boolean result = false;
    if (test(synCtx)) {
        if (thenKey != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug((xpath == null ? "Source : " + source + " against : " + regex.pattern() + " matches" : "XPath expression : " + xpath + " evaluates to true") + " - executing then sequence with key : " + thenKey);
            }
            ContinuationStackManager.updateSeqContinuationState(synCtx, getMediatorPosition());
            Mediator seq = synCtx.getSequence(thenKey);
            if (seq != null) {
                result = seq.mediate(synCtx);
            } else {
                handleException("Couldn't find the referred then sequence with key : " + thenKey, synCtx);
            }
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug((xpath == null ? "Source : " + source + " against : " + regex.pattern() + " matches" : "XPath expression : " + xpath + " evaluates to true") + " - executing child mediators");
            }
            ContinuationStackManager.addReliantContinuationState(synCtx, 0, getMediatorPosition());
            result = super.mediate(synCtx);
            if (result) {
                ContinuationStackManager.removeReliantContinuationState(synCtx);
            }
        }
    } else {
        if (elseKey != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug((xpath == null ? "Source : " + source + " against : " + regex.pattern() + " does not match" : "XPath expression : " + xpath + " evaluates to false") + " - executing the else sequence with key : " + elseKey);
            }
            ContinuationStackManager.updateSeqContinuationState(synCtx, getMediatorPosition());
            Mediator elseSeq = synCtx.getSequence(elseKey);
            if (elseSeq != null) {
                result = elseSeq.mediate(synCtx);
            } else {
                handleException("Couldn't find the referred else sequence with key : " + elseKey, synCtx);
            }
        } else if (elseMediator != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug((xpath == null ? "Source : " + source + " against : " + regex.pattern() + " does not match" : "XPath expression : " + xpath + " evaluates to false") + " - executing the else path child mediators");
            }
            ContinuationStackManager.addReliantContinuationState(synCtx, 1, getMediatorPosition());
            result = elseMediator.mediate(synCtx);
            if (result) {
                ContinuationStackManager.removeReliantContinuationState(synCtx);
            }
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug((xpath == null ? "Source : " + source + " against : " + regex.pattern() + " does not match" : "XPath expression : " + xpath + " evaluates to false and no else path") + " - skipping child mediators");
            }
            result = true;
        }
    }
    synLog.traceOrDebug("End : Filter mediator ");
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) ListMediator(org.apache.synapse.mediators.ListMediator) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator)

Example 37 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class FaultMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Fault mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    switch(soapVersion) {
        case SOAP11:
            makeSOAPFault(synCtx, SOAP11, synLog);
            break;
        case SOAP12:
            makeSOAPFault(synCtx, SOAP12, synLog);
            break;
        case POX:
            makePOXFault(synCtx, synLog);
            break;
        default:
            {
                // if this is a POX or REST message then make a POX fault
                if (synCtx.isDoingPOX() || synCtx.isDoingGET()) {
                    makePOXFault(synCtx, synLog);
                } else {
                    // determine from current message's SOAP envelope namespace
                    SOAPEnvelope envelop = synCtx.getEnvelope();
                    if (envelop != null) {
                        if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelop.getNamespace().getNamespaceURI())) {
                            makeSOAPFault(synCtx, SOAP12, synLog);
                        } else {
                            makeSOAPFault(synCtx, SOAP11, synLog);
                        }
                    } else {
                        // default to SOAP 11
                        makeSOAPFault(synCtx, SOAP11, synLog);
                    }
                }
            }
    }
    final Pipe pipe = (Pipe) (((Axis2MessageContext) synCtx)).getAxis2MessageContext().getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null) {
        // cleaning the OUTPUT PIPE with older references
        // if there is a [protocal violation when sending out message etc.]
        pipe.getBuffer().clear();
        pipe.resetOutputStream();
    }
    // if the message has to be marked as a response mark it as response
    if (markAsResponse) {
        synCtx.setResponse(true);
        synCtx.setTo(synCtx.getReplyTo());
    }
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) Pipe(org.apache.synapse.transport.passthru.Pipe)

Example 38 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class HeaderMediator method mediate.

/**
 * Sets/Removes a SOAP header on the current message
 *
 * @param synCtx the current message which is altered as necessary
 * @return true always
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Header mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    String value = (getExpression() == null ? getValue() : expression.stringValueOf(synCtx));
    if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
        if (action == ACTION_SET) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Set SOAP header : " + qName + " to : " + value);
            }
            if (!isImplicit() && (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI()))) {
                // is this a "well known" Synapse header?
                if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
                    synCtx.setTo(new EndpointReference(value));
                } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
                    synCtx.setFrom(new EndpointReference(value));
                } else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) {
                    synCtx.setWSAAction(value);
                } else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) {
                    synCtx.setFaultTo(new EndpointReference(value));
                } else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) {
                    synCtx.setReplyTo(new EndpointReference(value));
                } else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) {
                    synCtx.setRelatesTo(new RelatesTo[] { new RelatesTo(value) });
                } else {
                    addCustomHeader(synCtx, value);
                }
            } else {
                addCustomHeader(synCtx, value);
            }
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Removing SOAP Header : " + qName);
            }
            if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) {
                // is this a "well known" Synapse header?
                if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
                    synCtx.setTo(null);
                } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
                    synCtx.setFrom(null);
                } else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) {
                    synCtx.setWSAAction(null);
                } else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) {
                    synCtx.setFaultTo(null);
                } else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) {
                    synCtx.setReplyTo(null);
                } else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) {
                    synCtx.setRelatesTo(null);
                } else {
                    SOAPEnvelope envelope = synCtx.getEnvelope();
                    if (envelope != null) {
                        SOAPHeader header = envelope.getHeader();
                        if (header != null) {
                            removeFromHeaderList(header.getHeaderBlocksWithNSURI(""));
                        }
                    }
                }
            } else {
                SOAPEnvelope envelope = synCtx.getEnvelope();
                if (envelope != null) {
                    SOAPHeader header = envelope.getHeader();
                    if (header != null) {
                        removeFromHeaderList(header.getHeaderBlocksWithNSURI(qName.getNamespaceURI()));
                    }
                }
            }
        }
    } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope)) {
        String headerName = qName.getLocalPart();
        if (action == ACTION_SET) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Set HTTP header : " + headerName + " to : " + value);
            }
            // Setting Transport Headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                headersMap.put(headerName, value);
            }
            if (headers == null) {
                Map headersMap = new HashMap();
                headersMap.put(headerName, value);
                axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
            }
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Removing HTTP Header : " + qName);
            }
            // Removing transport headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                headersMap.remove(headerName);
            } else {
                synLog.traceOrDebug("No transport headers found for the message");
            }
        }
    }
    // NOTE: We dont' use an else here because the HTTPMediatorFactory should capture cases where scope is not default or transport.
    synLog.traceOrDebug("End : Header mediator");
    return true;
}
Also used : HashMap(java.util.HashMap) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) RelatesTo(org.apache.axis2.addressing.RelatesTo) EndpointReference(org.apache.axis2.addressing.EndpointReference) SynapseLog(org.apache.synapse.SynapseLog) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) SOAPHeader(org.apache.axiom.soap.SOAPHeader) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 39 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class XSLTMediator method mediate.

/**
 * Transforms this message (or its element specified as the source) using the
 * given XSLT transformation
 *
 * @param synCtx the current message where the transformation will apply
 * @return true always
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    synLog.traceOrDebug("Start : XSLT mediator");
    if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace("Message : " + synCtx.getEnvelope());
    }
    if (source.getXPath() == null && synCtx.getEnvelope().getBody().getFirstElement() == null) {
        synLog.auditWarn("Found empty soap body, skipping XSLT transformation and continuing the mediation");
        return true;
    }
    try {
        performXSLT(synCtx, synLog);
    } catch (Exception e) {
        handleException("Unable to perform XSLT transformation using : " + xsltKey + " against source XPath : " + source + " reason : " + e.getMessage(), e, synCtx);
    }
    synLog.traceOrDebug("End : XSLT mediator");
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SynapseException(org.apache.synapse.SynapseException)

Example 40 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class TransactionMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    // UserTransaction tx = null;
    final SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Transaction mediator (" + action + ")");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    initContext(synCtx);
    try {
        // tx = (UserTransaction) txContext.lookup(USER_TX_LOOKUP_STR);
        TranscationManger.lookUp(txContext);
    } catch (Exception e) {
        handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
    }
    if (action.equals(ACTION_COMMIT)) {
        try {
            // tx.commit();
            long key = Thread.currentThread().getId();
            TranscationManger.endTransaction(true, key);
        } catch (Exception e) {
            handleException("Unable to commit transaction", e, synCtx);
        }
    } else if (action.equals(ACTION_ROLLBACK)) {
        try {
            // tx.rollback();
            long key = Thread.currentThread().getId();
            TranscationManger.rollbackTransaction(true, key);
        } catch (Exception e) {
            handleException("Unable to rollback transaction", e, synCtx);
        }
    } else if (action.equals(ACTION_NEW)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (!(status == Status.STATUS_NO_TRANSACTION || status == Status.STATUS_UNKNOWN)) {
            throw new SynapseException("Require to begin a new transaction, " + "but a transaction already exist");
        }
        beginNewTransaction(synCtx);
    } else if (action.equals(ACTION_USE_EXISTING_OR_NEW)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (status == Status.STATUS_NO_TRANSACTION) {
            beginNewTransaction(synCtx);
        }
    } else if (action.equals(ACTION_FAULT_IF_NO_TX)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (status != Status.STATUS_ACTIVE)
            throw new SynapseException("No active transaction. Require an active transaction");
    } else {
        handleException("Invalid transaction mediator action : " + action, synCtx);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("End : Transaction mediator");
    }
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException) NamingException(javax.naming.NamingException)

Aggregations

SynapseLog (org.apache.synapse.SynapseLog)53 Mediator (org.apache.synapse.Mediator)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)10 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)9 MessageContext (org.apache.synapse.MessageContext)8 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)8 OMNode (org.apache.axiom.om.OMNode)7 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)6 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)6 JaxenException (org.jaxen.JaxenException)5 PreparedStatement (java.sql.PreparedStatement)4 Set (java.util.Set)4 AxisFault (org.apache.axis2.AxisFault)4 OperationContext (org.apache.axis2.context.OperationContext)4 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 ReliantContinuationState (org.apache.synapse.continuation.ReliantContinuationState)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2