Search in sources :

Example 81 with Mediator

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

the class Axis2MessageContext method getFaultSequence.

public Mediator getFaultSequence() {
    Object o = localEntries.get(SynapseConstants.FAULT_SEQUENCE_KEY);
    if (o != null && o instanceof Mediator) {
        return (Mediator) o;
    } else {
        Mediator fault = getConfiguration().getFaultSequence();
        localEntries.put(SynapseConstants.FAULT_SEQUENCE_KEY, fault);
        return fault;
    }
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 82 with Mediator

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

the class Axis2MessageContext method getSequenceTemplate.

public Mediator getSequenceTemplate(String key) {
    Object o = localEntries.get(key);
    if (o != null && o instanceof Mediator) {
        return (Mediator) o;
    } else {
        Mediator m = getConfiguration().getSequenceTemplate(key);
        if (m instanceof TemplateMediator) {
            TemplateMediator templateMediator = (TemplateMediator) m;
            synchronized (m) {
                if (!templateMediator.isInitialized()) {
                    templateMediator.init(synEnv);
                }
            }
        }
        localEntries.put(key, m);
        return m;
    }
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 83 with Mediator

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

the class ProxyService method registerFaultHandler.

/**
 * Register the fault handler for the message context
 *
 * @param synCtx Message Context
 */
public void registerFaultHandler(MessageContext synCtx) {
    boolean traceOn = trace();
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    if (targetFaultSequence != null) {
        Mediator faultSequence = synCtx.getSequence(targetFaultSequence);
        if (faultSequence != null) {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Setting the fault-sequence to : " + faultSequence);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getSequence(targetFaultSequence)));
        } else {
            // fault sequence and the message mediation can still continue
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Unable to find fault-sequence : " + targetFaultSequence + "; using default fault sequence");
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
        }
    } else if (targetInLineFaultSequence != null) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Setting specified anonymous fault-sequence for proxy");
        }
        synCtx.pushFaultHandler(new MediatorFaultHandler(targetInLineFaultSequence));
    } else {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Setting default fault-sequence for proxy");
        }
        synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
    }
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 84 with Mediator

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

the class ProxyServiceMessageReceiver method receive.

public void receive(org.apache.axis2.context.MessageContext mc) throws AxisFault {
    boolean traceOn = proxy.getAspectConfiguration().isTracingEnabled();
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    CustomLogSetter.getInstance().setLogAppender(proxy.getArtifactContainerName());
    String remoteAddr = (String) mc.getProperty(org.apache.axis2.context.MessageContext.REMOTE_ADDR);
    if (traceOrDebugOn) {
        traceOrDebug(traceOn, "Proxy Service " + name + " received a new message" + (remoteAddr != null ? " from : " + remoteAddr : "..."));
        traceOrDebug(traceOn, ("Message To: " + (mc.getTo() != null ? mc.getTo().getAddress() : "null")));
        traceOrDebug(traceOn, ("SOAPAction: " + (mc.getSoapAction() != null ? mc.getSoapAction() : "null")));
        traceOrDebug(traceOn, ("WSA-Action: " + (mc.getWSAAction() != null ? mc.getWSAAction() : "null")));
        if (traceOn && trace.isTraceEnabled()) {
            String[] cids = null;
            try {
                cids = mc.getAttachmentMap().getAllContentIDs();
            } catch (Exception ex) {
                // partially read stream could lead to corrupted attachment map and hence this exception
                // corrupted attachment map leads to inconsistent runtime exceptions and behavior
                // discard the attachment map for the fault handler invocation
                // ensure the successful completion for fault handler flow
                mc.setAttachmentMap(null);
                log.error("Synapse encountered an exception when reading attachments from bytes stream. " + "Hence Attachments map is dropped from the message context.", ex);
            }
            if (cids != null && cids.length > 0) {
                for (String cid : cids) {
                    trace.trace("With attachment content ID : " + cid);
                }
            }
            trace.trace("Envelope : " + mc.getEnvelope());
        }
    }
    MessageContext synCtx = MessageContextCreatorForAxis2.getSynapseMessageContext(mc);
    Integer statisticReportingIndex = null;
    // Statistic reporting
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (isStatisticsEnabled) {
        statisticReportingIndex = OpenEventCollector.reportEntryEvent(synCtx, this.name, proxy.getAspectConfiguration(), ComponentType.PROXYSERVICE);
    }
    Object inboundServiceParam = proxy.getParameterMap().get(SynapseConstants.INBOUND_PROXY_SERVICE_PARAM);
    Object inboundMsgCtxParam = mc.getProperty(SynapseConstants.IS_INBOUND);
    // check whether the message is from Inbound EP
    if (inboundMsgCtxParam == null || !(boolean) inboundMsgCtxParam) {
        // check whether service parameter is set to true or null, then block this request
        if (inboundServiceParam != null && (Boolean.valueOf((String) inboundServiceParam))) {
            /*
                return because same proxy is exposed via InboundEP and service parameter(inbound.only) is set to
                true, which disable normal http transport proxy
                */
            if (!synCtx.getFaultStack().isEmpty()) {
                if (log.isDebugEnabled()) {
                    log.debug("Executing fault handler - message discarded due to the proxy is allowed only via InboundEP");
                }
                (synCtx.getFaultStack().pop()).handleFault(synCtx, new Exception("Proxy Service " + name + " message discarded due to the proxy is allowed only via InboundEP"));
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Proxy Service " + name + " message discarded due to the proxy is " + "allowed only via InboundEP");
                }
            }
            return;
        }
    }
    TenantInfoConfigurator configurator = synCtx.getEnvironment().getTenantInfoConfigurator();
    if (configurator != null) {
        configurator.extractTenantInfo(synCtx);
    }
    TransportInDescription trpInDesc = mc.getTransportIn();
    if (trpInDesc != null) {
        synCtx.setProperty(SynapseConstants.TRANSPORT_IN_NAME, trpInDesc.getName());
    }
    // get service log for this message and attach to the message context also set proxy name
    Log serviceLog = LogFactory.getLog(SynapseConstants.SERVICE_LOGGER_PREFIX + name);
    ((Axis2MessageContext) synCtx).setServiceLog(serviceLog);
    synCtx.setProperty(SynapseConstants.PROXY_SERVICE, name);
    // synCtx.setTracingState(proxy.getTraceState());
    synCtx.setProperty(SynapseConstants.IS_CLIENT_DOING_REST, mc.isDoingREST());
    synCtx.setProperty(SynapseConstants.IS_CLIENT_DOING_SOAP11, mc.isSOAP11());
    try {
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.acquireMediationFlowLock();
            debugManager.advertiseMediationFlowStartPoint(synCtx);
            if (!synCtx.isResponse()) {
                SynapseWireLogHolder wireLogHolder = (SynapseWireLogHolder) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY);
                if (wireLogHolder == null) {
                    wireLogHolder = new SynapseWireLogHolder();
                }
                if (synCtx.getProperty(SynapseConstants.PROXY_SERVICE) != null && !synCtx.getProperty(SynapseConstants.PROXY_SERVICE).toString().isEmpty()) {
                    wireLogHolder.setProxyName(synCtx.getProperty(SynapseConstants.PROXY_SERVICE).toString());
                }
                ((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY, wireLogHolder);
            }
        }
        synCtx.setProperty(SynapseConstants.RESPONSE_STATE, new ResponseState());
        List handlers = synCtx.getEnvironment().getSynapseHandlers();
        Iterator<SynapseHandler> iterator = handlers.iterator();
        while (iterator.hasNext()) {
            SynapseHandler handler = iterator.next();
            if (!handler.handleRequestInFlow(synCtx)) {
                return;
            }
        }
        Mediator mandatorySeq = synCtx.getConfiguration().getMandatorySequence();
        if (mandatorySeq != null) {
            if (log.isDebugEnabled()) {
                log.debug("Start mediating the message in the " + "pre-mediate state using the mandatory sequence");
            }
            if (!mandatorySeq.mediate(synCtx)) {
                if (log.isDebugEnabled()) {
                    log.debug("Request message for the proxy service " + name + " dropped in " + "the pre-mediation state by the mandatory sequence : \n" + synCtx);
                }
                return;
            }
        }
        // setup fault sequence - i.e. what happens when something goes wrong with this message
        proxy.registerFaultHandler(synCtx);
        boolean inSequenceResult = true;
        // Using inSequence for the incoming message mediation
        if (proxy.getTargetInSequence() != null) {
            Mediator inSequence = synCtx.getSequence(proxy.getTargetInSequence());
            if (inSequence != null) {
                traceOrDebug(traceOn, "Using sequence named : " + proxy.getTargetInSequence() + " for incoming message mediation");
                inSequenceResult = inSequence.mediate(synCtx);
            } else {
                handleException("Unable to find in-sequence : " + proxy.getTargetInSequence(), synCtx);
            }
        } else if (proxy.getTargetInLineInSequence() != null) {
            traceOrDebug(traceOn, "Using the anonymous " + "in-sequence of the proxy service for mediation");
            inSequenceResult = proxy.getTargetInLineInSequence().mediate(synCtx);
        }
        // if inSequence returns true, forward message to endpoint
        if (inSequenceResult) {
            if (proxy.getTargetEndpoint() != null) {
                Endpoint endpoint = synCtx.getEndpoint(proxy.getTargetEndpoint());
                if (endpoint != null) {
                    traceOrDebug(traceOn, "Forwarding message to the endpoint : " + proxy.getTargetEndpoint());
                    endpoint.send(synCtx);
                } else {
                    handleException("Unable to find the endpoint specified : " + proxy.getTargetEndpoint(), synCtx);
                }
            } else if (proxy.getTargetInLineEndpoint() != null) {
                traceOrDebug(traceOn, "Forwarding the message to the anonymous " + "endpoint of the proxy service");
                proxy.getTargetInLineEndpoint().send(synCtx);
            }
        }
    } catch (SynapseException syne) {
        if (!synCtx.getFaultStack().isEmpty()) {
            warn(traceOn, "Executing fault handler due to exception encountered", synCtx);
            ((FaultHandler) synCtx.getFaultStack().pop()).handleFault(synCtx, syne);
        } else {
            warn(traceOn, "Exception encountered but no fault handler found - " + "message dropped", synCtx);
        }
    } finally {
        // Statistic reporting
        if (isStatisticsEnabled) {
            CloseEventCollector.tryEndFlow(synCtx, this.name, ComponentType.PROXYSERVICE, statisticReportingIndex, true);
        }
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.advertiseMediationFlowTerminatePoint(synCtx);
            debugManager.releaseMediationFlowLock();
        }
        doPostInjectUpdates(synCtx);
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) Log(org.apache.commons.logging.Log) SynapseWireLogHolder(org.apache.synapse.transport.http.conn.SynapseWireLogHolder) TransportInDescription(org.apache.axis2.description.TransportInDescription) SynapseHandler(org.apache.synapse.SynapseHandler) SynapseDebugManager(org.apache.synapse.debug.SynapseDebugManager) SynapseException(org.apache.synapse.SynapseException) TenantInfoConfigurator(org.apache.synapse.carbonext.TenantInfoConfigurator) Endpoint(org.apache.synapse.endpoints.Endpoint) List(java.util.List) Mediator(org.apache.synapse.Mediator) MessageContext(org.apache.synapse.MessageContext)

Example 85 with Mediator

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

the class SequenceDebugUtil method registerSequenceMediationFlowBreakPoint.

/**
 * Registers/Un-registers a skip, point where mediator disables from mediation flow
 *
 * @param synCfg Synapse configuration
 * @param sequenceType Synapse sequence type
 * @param seqKey name of the Sequence
 * @param position array of integers that uniquely specifies a point in mediation route
 * @param registerMode specify whether register or un register
 */
public static void registerSequenceMediationFlowBreakPoint(SynapseConfiguration synCfg, String sequenceType, String seqKey, int[] position, boolean registerMode) {
    SynapseSequenceType synapseSequenceType = SynapseSequenceType.valueOf(sequenceType.toUpperCase());
    SynapseMediationFlowPoint breakPoint = new SequenceMediationFlowPoint();
    breakPoint.setSynapseMediationComponent(SynapseMediationComponent.SEQUENCE);
    breakPoint.setKey(seqKey);
    breakPoint.setMediatorPosition(position);
    ((SequenceMediationFlowPoint) breakPoint).setSynapseSequenceType(synapseSequenceType);
    ((SequenceMediationFlowPoint) breakPoint).setSequenceBaseType(SynapseDebugCommandConstants.DEBUG_COMMAND_MEDIATION_COMPONENT_SEQUENCE);
    Mediator seqMediator = null;
    if (synapseSequenceType.equals(SynapseSequenceType.NAMED)) {
        seqMediator = synCfg.getSequence(seqKey);
    }
    if (seqMediator != null) {
        Mediator current_mediator = null;
        current_mediator = MediatorTreeTraverseUtil.getMediatorReference(synCfg, seqMediator, position);
        if (current_mediator != null) {
            breakPoint.setMediatorReference(current_mediator);
            if (registerMode) {
                if (!((AbstractMediator) current_mediator).isBreakPoint()) {
                    ((AbstractMediator) current_mediator).setBreakPoint(true);
                    ((AbstractMediator) current_mediator).registerMediationFlowPoint(breakPoint);
                    if (log.isDebugEnabled()) {
                        log.debug("Registered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed register breakpoint. Already breakpoint enabled at mediator position " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_ENABLED).toString());
                }
            } else {
                if (((AbstractMediator) current_mediator).isBreakPoint()) {
                    ((AbstractMediator) current_mediator).unregisterMediationFlowPoint();
                    ((AbstractMediator) current_mediator).setBreakPoint(false);
                    if (log.isDebugEnabled()) {
                        log.debug("Unregistered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed unregister breakpoint. Already breakpoint disabled at mediator position " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_DISABLED).toString());
                }
            }
        } else {
            if (registerMode) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed register breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                }
                debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Failed unregister breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for Sequence " + breakPoint.getKey());
                }
                debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
            }
        }
    } else {
        if (registerMode) {
            if (log.isDebugEnabled()) {
                log.debug("Failed register breakpoint. Non existing sequence for key " + breakPoint.getKey());
            }
            debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Failed unregister breakpoint. Non existing sequence for key " + breakPoint.getKey());
            }
            debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
        }
    }
}
Also used : SequenceMediationFlowPoint(org.apache.synapse.debug.constructs.SequenceMediationFlowPoint) SynapseSequenceType(org.apache.synapse.debug.constructs.SynapseSequenceType) SynapseMediationFlowPoint(org.apache.synapse.debug.constructs.SynapseMediationFlowPoint) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator)

Aggregations

Mediator (org.apache.synapse.Mediator)108 Properties (java.util.Properties)30 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)24 OMElement (org.apache.axiom.om.OMElement)22 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)22 MessageContext (org.apache.synapse.MessageContext)16 SynapseLog (org.apache.synapse.SynapseLog)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)12 TestMessageContext (org.apache.synapse.TestMessageContext)12 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)12 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)12 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)12 Test (org.junit.Test)10 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)9 SynapseSequenceType (org.apache.synapse.debug.constructs.SynapseSequenceType)8 SequenceMediationFlowPoint (org.apache.synapse.debug.constructs.SequenceMediationFlowPoint)6 ForEachMediatorFactory (org.apache.synapse.config.xml.ForEachMediatorFactory)5 MediatorFactory (org.apache.synapse.config.xml.MediatorFactory)5 MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)5