Search in sources :

Example 21 with SynapseLog

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

the class CallMediator method handleBlockingCall.

/**
 * Send request in blocking manner
 *
 * @param synInCtx message context
 * @return  continue the mediation flow or not
 */
private boolean handleBlockingCall(MessageContext synInCtx) {
    SynapseLog synLog = getLog(synInCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Call mediator - Blocking Call");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synInCtx.getEnvelope());
        }
    }
    MessageContext resultMsgCtx = null;
    // set initClientOption with blockingMsgsender
    blockingMsgSender.setInitClientOptions(initClientOptions);
    // blocking sender.
    if (endpoint == null) {
        endpoint = new DefaultEndpoint();
        EndpointDefinition endpointDefinition = new EndpointDefinition();
        ((DefaultEndpoint) endpoint).setDefinition(endpointDefinition);
        isWrappingEndpointCreated = true;
    }
    try {
        if ("true".equals(synInCtx.getProperty(SynapseConstants.OUT_ONLY))) {
            blockingMsgSender.send(endpoint, synInCtx);
        } else {
            resultMsgCtx = blockingMsgSender.send(endpoint, synInCtx);
            if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
                handleFault(synInCtx, (Exception) resultMsgCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
            }
        }
    } catch (Exception ex) {
        handleFault(synInCtx, ex);
    }
    if (resultMsgCtx != null) {
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
        }
        try {
            synInCtx.setEnvelope(resultMsgCtx.getEnvelope());
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("End : Call mediator - Blocking Call");
            }
            return true;
        } catch (Exception e) {
            handleFault(synInCtx, e);
        }
    } else {
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Service returned a null response");
        }
    }
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) DefaultEndpoint(org.apache.synapse.endpoints.DefaultEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) MessageContext(org.apache.synapse.MessageContext) SynapseException(org.apache.synapse.SynapseException)

Example 22 with SynapseLog

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

the class CalloutMediator 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 : Callout mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    try {
        if (!initClientOptions) {
            blockingMsgSender.setInitClientOptions(false);
        }
        if (endpointKey != null) {
            endpoint = synCtx.getEndpoint(endpointKey);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            if (!isWrappingEndpointCreated) {
                synLog.traceOrDebug("Using the defined endpoint : " + endpoint.getName());
            } else {
                if (serviceURL != null) {
                    synLog.traceOrDebug("Using the serviceURL : " + serviceURL);
                } else {
                    synLog.traceOrDebug("Using the To header as the EPR ");
                }
                if (securityOn) {
                    synLog.traceOrDebug("Security enabled within the Callout Mediator config");
                    if (wsSecPolicyKey != null) {
                        synLog.traceOrDebug("Using security policy key : " + wsSecPolicyKey);
                    } else {
                        if (inboundWsSecPolicyKey != null) {
                            synLog.traceOrDebug("Using inbound security policy key : " + inboundWsSecPolicyKey);
                        }
                        if (outboundWsSecPolicyKey != null) {
                            synLog.traceOrDebug("Using outbound security policy key : " + outboundWsSecPolicyKey);
                        }
                    }
                }
            }
        }
        if (isWrappingEndpointCreated) {
            org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
            if (Constants.VALUE_TRUE.equals(axis2MsgCtx.getProperty(Constants.Configuration.ENABLE_MTOM))) {
                ((AbstractEndpoint) endpoint).getDefinition().setUseMTOM(true);
            }
        }
        if (this.serviceURL != null && this.serviceURL.contains(DISTRIBUTED_TX_BEGIN_CHECK_STR)) {
            try {
                initContext(synCtx);
                try {
                    TranscationManger.lookUp(txContext);
                } catch (Exception e) {
                    handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
                }
                TranscationManger.beginTransaction();
                org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION, TranscationManger.getTransaction());
                axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION_MANAGER, TranscationManger.getTransactionManager());
            } catch (Exception e) {
                handleException("Error starting transaction", synCtx);
            }
        }
        MessageContext synapseOutMsgCtx = MessageHelper.cloneMessageContext(synCtx);
        // Send the SOAP Header Blocks to support WS-Addressing
        setSoapHeaderBlock(synapseOutMsgCtx);
        if (!useEnvelopeAsSource && // if the payload is JSON, we do not consider the request (ie. source) path. Instead, we use the complete payload.
        !JsonUtil.hasAJsonPayload(((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext())) {
            SOAPBody soapBody = synapseOutMsgCtx.getEnvelope().getBody();
            for (Iterator itr = soapBody.getChildElements(); itr.hasNext(); ) {
                OMElement child = (OMElement) itr.next();
                child.detach();
            }
            soapBody.addChild(getRequestPayload(synCtx));
        }
        if (action != null) {
            synapseOutMsgCtx.setWSAAction(action);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("About to invoke the service");
            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Request message payload : " + synapseOutMsgCtx.getEnvelope());
            }
        }
        MessageContext resultMsgCtx = null;
        try {
            if ("true".equals(synCtx.getProperty(SynapseConstants.OUT_ONLY))) {
                blockingMsgSender.send(endpoint, synapseOutMsgCtx);
            } else {
                resultMsgCtx = blockingMsgSender.send(endpoint, synapseOutMsgCtx);
                setResponseHttpSc(resultMsgCtx, synCtx);
                if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
                    handleFault(synCtx, (Exception) resultMsgCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
                }
            }
        } catch (Exception ex) {
            handleFault(synCtx, ex);
        }
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
        }
        if (resultMsgCtx != null && resultMsgCtx.getEnvelope() != null) {
            org.apache.axis2.context.MessageContext resultAxisMsgCtx = ((Axis2MessageContext) resultMsgCtx).getAxis2MessageContext();
            org.apache.axis2.context.MessageContext inAxisMsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
            if (JsonUtil.hasAJsonPayload(resultAxisMsgCtx)) {
                JsonUtil.cloneJsonPayload(resultAxisMsgCtx, inAxisMsgCtx);
            } else {
                if (targetXPath != null) {
                    Object o = targetXPath.evaluate(synCtx);
                    OMElement result = resultMsgCtx.getEnvelope().getBody().getFirstElement();
                    if (o != null && o instanceof OMElement) {
                        OMNode tgtNode = (OMElement) o;
                        tgtNode.insertSiblingAfter(result);
                        tgtNode.detach();
                    } else if (o != null && o instanceof List && !((List) o).isEmpty()) {
                        // Always fetches *only* the first
                        OMNode tgtNode = (OMElement) ((List) o).get(0);
                        tgtNode.insertSiblingAfter(result);
                        tgtNode.detach();
                    } else {
                        handleException("Evaluation of target XPath expression : " + targetXPath.toString() + " did not yeild an OMNode", synCtx);
                    }
                } else if (targetKey != null) {
                    OMElement result = resultMsgCtx.getEnvelope().getBody().getFirstElement();
                    synCtx.setProperty(targetKey, result);
                } else {
                    synCtx.setEnvelope(resultMsgCtx.getEnvelope());
                }
            }
            // Set HTTP Status code
            inAxisMsgCtx.setProperty(SynapseConstants.HTTP_SC, resultAxisMsgCtx.getProperty(SynapseConstants.HTTP_SC));
            if ("false".equals(synCtx.getProperty(SynapseConstants.BLOCKING_SENDER_PRESERVE_REQ_HEADERS))) {
                inAxisMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, resultAxisMsgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
            }
        } else {
            synLog.traceOrDebug("Service returned a null response");
        }
    } catch (AxisFault e) {
        handleException("Error invoking service : " + serviceURL + (action != null ? " with action : " + action : ""), e, synCtx);
    } catch (JaxenException e) {
        handleException("Error while evaluating the XPath expression: " + targetXPath, e, synCtx);
    }
    synLog.traceOrDebug("End : Callout mediator");
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) OMElement(org.apache.axiom.om.OMElement) JaxenException(org.jaxen.JaxenException) NamingException(javax.naming.NamingException) SynapseException(org.apache.synapse.SynapseException) OMException(org.apache.axiom.om.OMException) OMNode(org.apache.axiom.om.OMNode) SOAPBody(org.apache.axiom.soap.SOAPBody) SynapseLog(org.apache.synapse.SynapseLog) JaxenException(org.jaxen.JaxenException) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 23 with SynapseLog

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

the class SequenceMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Mediating using the SeqContinuationState type : " + ((SeqContinuationState) continuationState).getSeqType() + " name : " + ((SeqContinuationState) continuationState).getSeqName());
    }
    Mediator errorHandlerMediator = null;
    // push the errorHandler sequence into the current message as the fault handler
    if (errorHandler != null) {
        errorHandlerMediator = synCtx.getSequence(errorHandler);
        if (errorHandlerMediator != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Setting the onError handler : " + errorHandler + " for the sequence : " + name);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
        } else {
            synLog.auditWarn("onError handler : " + errorHandler + " for sequence : " + name + " cannot be found");
        }
    }
    boolean result;
    if (!continuationState.hasChild()) {
        result = super.mediate(synCtx, continuationState.getPosition() + 1);
    } else {
        // if children exists first mediate from them starting from grandchild.
        do {
            FlowContinuableMediator mediator = (FlowContinuableMediator) getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
            if (result) {
                // if flow completed remove leaf child
                continuationState.removeLeafChild();
            }
        } while (result && continuationState.hasChild());
        if (result) {
            // after mediating from children, mediate from current SeqContinuationState
            result = super.mediate(synCtx, continuationState.getPosition() + 1);
        }
    }
    if (result) {
        // if flow completed, remove top ContinuationState from stack
        ContinuationStackManager.popContinuationStateStack(synCtx);
    }
    // before we exit normally without an exception
    if (errorHandlerMediator != null) {
        Stack faultStack = synCtx.getFaultStack();
        if (faultStack != null && !faultStack.isEmpty()) {
            Object o = faultStack.peek();
            if (o instanceof MediatorFaultHandler && errorHandlerMediator.equals(((MediatorFaultHandler) o).getFaultMediator())) {
                faultStack.pop();
            }
        }
    }
    return result;
}
Also used : SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Example 24 with SynapseLog

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

the class SequenceMediator method mediate.

/**
 * If this mediator refers to another named Sequence, execute that. Else
 * execute the list of mediators (children) contained within this. If a referenced
 * named sequence mediator instance cannot be found at runtime, an exception is
 * thrown. This may occur due to invalid configuration of an erroneous runtime
 * change of the synapse configuration. It is the responsibility of the
 * SynapseConfiguration builder to ensure that dead references are not present.
 *
 * @param synCtx the synapse message
 * @return as per standard mediator result
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Sequence " + (name == null ? (key == null ? "<anonymous" : "key=<" + key) : "<" + name) + ">");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (key == null) {
        // The onError sequence for handling errors which may occur during the
        // mediation through this sequence
        Mediator errorHandlerMediator = null;
        Integer statisticReportingIndex = null;
        if (RuntimeStatisticCollector.isStatisticsEnabled()) {
            statisticReportingIndex = reportOpenStatistics(synCtx, false);
        }
        try {
            // push the errorHandler sequence into the current message as the fault handler
            if (errorHandler != null) {
                errorHandlerMediator = synCtx.getSequence(errorHandler);
                if (errorHandlerMediator != null) {
                    if (synLog.isTraceOrDebugEnabled()) {
                        synLog.traceOrDebug("Setting the onError handler : " + errorHandler + " for the sequence : " + name);
                    }
                    synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
                } else {
                    synLog.auditWarn("onError handler : " + errorHandler + " for sequence : " + name + " cannot be found");
                }
            }
            // Add a new SeqContinuationState as we branched to new Sequence.
            boolean skipAddition = ContinuationStackManager.isSkipSeqContinuationStateAddition(synCtx);
            if (!skipAddition) {
                if (dynamic && registryKey != null) {
                    ContinuationStackManager.addSeqContinuationState(synCtx, registryKey, sequenceType);
                } else {
                    ContinuationStackManager.addSeqContinuationState(synCtx, name, sequenceType);
                }
            }
            boolean result = super.mediate(synCtx);
            if (result && !skipAddition) {
                // if flow completed remove the previously added SeqContinuationState
                ContinuationStackManager.removeSeqContinuationState(synCtx, sequenceType);
            }
            // before we exit normally without an exception
            if (errorHandlerMediator != null) {
                Stack faultStack = synCtx.getFaultStack();
                if (faultStack != null && !faultStack.isEmpty()) {
                    Object o = faultStack.peek();
                    if (o instanceof MediatorFaultHandler && errorHandlerMediator.equals(((MediatorFaultHandler) o).getFaultMediator())) {
                        faultStack.pop();
                    }
                }
            }
            if (synLog.isTraceOrDebugEnabled()) {
                if (synLog.isTraceTraceEnabled()) {
                    synLog.traceTrace("Message : " + synCtx.getEnvelope());
                }
                synLog.traceOrDebug("End : Sequence <" + (name == null ? "anonymous" : name) + ">");
            }
            return result;
        } finally {
            // End Statistics
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                reportCloseStatistics(synCtx, statisticReportingIndex);
            }
        }
    } else {
        String sequenceKey = key.evaluateValue(synCtx);
        // Mediator m = synCtx.getSequence(key);
        Mediator m = synCtx.getSequence(sequenceKey);
        if (m == null) {
            handleException("Sequence named " + key + " cannot be found", synCtx);
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Executing with key " + key);
            }
            // Update the SeqContinuationState position with the mediator position of
            // sequence mediator in the sequence
            ContinuationStackManager.updateSeqContinuationState(synCtx, getMediatorPosition());
            boolean result = m.mediate(synCtx);
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("End : Sequence key=<" + key + ">");
            }
            return result;
        }
    }
    return false;
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Example 25 with SynapseLog

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

the class ScriptMediator method mediate.

/**
 * Perform Script mediation.
 *
 * @param synCtx the Synapse message context
 * @return the boolean result from the script invocation
 */
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 : Script mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Scripting language : " + language + " source " + (key == null ? ": specified inline " : " loaded with key : " + key) + (function != null ? " function : " + function : ""));
    }
    boolean returnValue;
    if (multiThreadedEngine) {
        returnValue = invokeScript(synCtx);
    } else {
        // TODO: change to use a pool of script engines (requires an update to BSF)
        synchronized (scriptEngine.getClass()) {
            returnValue = invokeScript(synCtx);
        }
    }
    if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace("Result message after execution of script : " + synCtx.getEnvelope());
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("End : Script mediator return value : " + returnValue);
    }
    return returnValue;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog)

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