Search in sources :

Example 1 with SequenceMediator

use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.

the class EnqueueMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog log = getLog(synCtx);
    if (log.isTraceOrDebugEnabled()) {
        log.traceOrDebug("Start: enqueue mediator");
    }
    assert executorName != null : "executor name shouldn't be null";
    PriorityExecutor executor = synCtx.getConfiguration().getPriorityExecutors().get(executorName);
    if (executor == null) {
        log.auditWarn("Cannot find executor " + executorName + ". Using existing thread for mediation");
        Mediator m = synCtx.getSequence(sequenceName);
        if (m != null && m instanceof SequenceMediator) {
            return m.mediate(synCtx);
        } else {
            handleException("Sequence cannot be found : " + sequenceName, synCtx);
            return false;
        }
    }
    Mediator m = synCtx.getSequence(sequenceName);
    if (m != null && m instanceof SequenceMediator) {
        MediatorWorker worker = new MediatorWorker(m, synCtx);
        try {
            // execute with the given priority
            executor.execute(worker, priority);
        } catch (RejectedExecutionException ex) {
            // if RejectedExecutionException, jump to fault handler
            handleException("Unable to process message in priority executor " + executorName + " with priority " + priority + ". Thread pool exhausted.", synCtx);
        }
        // with the nio transport, this causes the listener not to write a 202
        // Accepted response, as this implies that Synapse does not yet know if
        // a 202 or 200 response would be written back.
        ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
        if (log.isTraceOrDebugEnabled()) {
            log.traceOrDebug("End: enqueue mediator");
        }
        return true;
    } else {
        handleException("Sequence cannot be found : " + sequenceName, synCtx);
        return false;
    }
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) MediatorWorker(org.apache.synapse.mediators.MediatorWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with SequenceMediator

use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.

the class CloneMediator method init.

public void init(SynapseEnvironment se) {
    synapseEnv = se;
    for (Target target : targets) {
        ManagedLifecycle seq = target.getSequence();
        if (seq != null) {
            seq.init(se);
        } else if (target.getSequenceRef() != null) {
            SequenceMediator targetSequence = (SequenceMediator) se.getSynapseConfiguration().getSequence(target.getSequenceRef());
            if (targetSequence == null || targetSequence.isDynamic()) {
                se.addUnavailableArtifactRef(target.getSequenceRef());
            }
        }
        Endpoint endpoint = target.getEndpoint();
        if (endpoint != null) {
            endpoint.init(se);
        }
    }
}
Also used : Target(org.apache.synapse.mediators.eip.Target) Endpoint(org.apache.synapse.endpoints.Endpoint) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Example 3 with SequenceMediator

use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.

the class AggregateMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState contState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Aggregate mediator : Mediating from ContinuationState");
    }
    boolean result;
    SequenceMediator onCompleteSequence = getOnCompleteSequence();
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (!contState.hasChild()) {
        result = onCompleteSequence.mediate(synCtx, contState.getPosition() + 1);
    } else {
        FlowContinuableMediator mediator = (FlowContinuableMediator) onCompleteSequence.getChild(contState.getPosition());
        result = mediator.mediate(synCtx, contState.getChildContState());
        if (isStatisticsEnabled) {
            ((Mediator) mediator).reportCloseStatistics(synCtx, null);
        }
    }
    if (isStatisticsEnabled) {
        onCompleteSequence.reportCloseStatistics(synCtx, null);
    }
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 4 with SequenceMediator

use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.

the class Resource method process.

void process(MessageContext synCtx) {
    Integer statisticReportingIndex = null;
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (!synCtx.isResponse()) {
        if (getDispatcherHelper() != null) {
            synCtx.setProperty(RESTConstants.REST_URL_PATTERN, getDispatcherHelper().getString());
        }
    }
    if (isStatisticsEnabled) {
        statisticReportingIndex = OpenEventCollector.reportChildEntryEvent(synCtx, getResourceName(synCtx, name), ComponentType.RESOURCE, getAspectConfiguration(), true);
    }
    if (log.isDebugEnabled()) {
        log.debug("Processing message with ID: " + synCtx.getMessageID() + " through the " + "resource: " + name);
    }
    if (!synCtx.isResponse()) {
        String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD);
        if (RESTConstants.METHOD_OPTIONS.equals(method) && sendOptions(synCtx)) {
            if (isStatisticsEnabled) {
                CloseEventCollector.closeEntryEvent(synCtx, getResourceName(synCtx, name), ComponentType.RESOURCE, statisticReportingIndex, true);
            }
            return;
        }
        synCtx.setProperty(RESTConstants.SYNAPSE_RESOURCE, name);
        String path = RESTUtils.getFullRequestPath(synCtx);
        int queryIndex = path.indexOf('?');
        if (queryIndex != -1) {
            String query = path.substring(queryIndex + 1);
            String[] entries = query.split(RESTConstants.QUERY_PARAM_DELIMITER);
            String name = null, value;
            for (String entry : entries) {
                int index = entry.indexOf('=');
                if (index != -1) {
                    try {
                        name = entry.substring(0, index);
                        value = URLDecoder.decode(entry.substring(index + 1), RESTConstants.DEFAULT_ENCODING);
                        synCtx.setProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + name, value);
                    } catch (UnsupportedEncodingException ignored) {
                    }
                } else {
                    // If '=' sign isn't presnet in the entry means that the '&' character is part of
                    // the query parameter value. If so query parameter value should be updated appending
                    // the remaining characters.
                    String existingValue = (String) synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + name);
                    value = RESTConstants.QUERY_PARAM_DELIMITER + entry;
                    synCtx.setProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + name, existingValue + value);
                }
            }
        }
    }
    SequenceMediator sequence = synCtx.isResponse() ? outSequence : inSequence;
    if (sequence != null) {
        registerFaultHandler(synCtx);
        sequence.mediate(synCtx);
        if (isStatisticsEnabled) {
            CloseEventCollector.closeEntryEvent(synCtx, getResourceName(synCtx, name), ComponentType.RESOURCE, statisticReportingIndex, true);
        }
        return;
    }
    String sequenceKey = synCtx.isResponse() ? outSequenceKey : inSequenceKey;
    if (sequenceKey != null) {
        registerFaultHandler(synCtx);
        Mediator referredSequence = synCtx.getSequence(sequenceKey);
        if (referredSequence != null) {
            referredSequence.mediate(synCtx);
        } else {
            throw new SynapseException("Specified sequence: " + sequenceKey + " cannot " + "be found");
        }
        if (isStatisticsEnabled) {
            CloseEventCollector.closeEntryEvent(synCtx, getResourceName(synCtx, name), ComponentType.RESOURCE, statisticReportingIndex, true);
        }
        return;
    }
    // response, simply send it back to the client.
    if (synCtx.isResponse()) {
        if (log.isDebugEnabled()) {
            log.debug("No out-sequence configured. Sending the response back.");
        }
        registerFaultHandler(synCtx);
        Axis2Sender.sendBack(synCtx);
    } else if (log.isDebugEnabled()) {
        log.debug("No in-sequence configured. Dropping the request.");
    }
    if (isStatisticsEnabled) {
        CloseEventCollector.closeEntryEvent(synCtx, getResourceName(synCtx, name), ComponentType.RESOURCE, statisticReportingIndex, true);
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 5 with SequenceMediator

use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.

the class SequenceDeployer method undeploySynapseArtifact.

@Override
public void undeploySynapseArtifact(String artifactName) {
    if (log.isDebugEnabled()) {
        log.debug("Sequence Undeployment of the sequence named : " + artifactName + " : Started");
    }
    try {
        SequenceMediator seq = getSynapseConfiguration().getDefinedSequences().get(artifactName);
        if (seq != null) {
            CustomLogSetter.getInstance().setLogAppender(seq.getArtifactContainerName());
            if (SynapseConstants.MAIN_SEQUENCE_KEY.equals(seq.getName()) || SynapseConstants.FAULT_SEQUENCE_KEY.equals(seq.getName())) {
                handleSynapseArtifactDeploymentError("Cannot Undeploy the " + seq.getName() + " sequence");
            }
            getSynapseConfiguration().removeSequence(artifactName);
            if (log.isDebugEnabled()) {
                log.debug("Destroying the sequence named : " + artifactName);
            }
            seq.destroy();
            if (log.isDebugEnabled()) {
                log.debug("Sequence Undeployment of the sequence named : " + artifactName + " : Completed");
            }
            log.info("Sequence named '" + seq.getName() + "' has been undeployed");
        } else if (log.isDebugEnabled()) {
            log.debug("Sequence " + artifactName + " has already been undeployed");
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("Sequence Undeployement of sequence named : " + artifactName + " : Failed", e);
    }
}
Also used : SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) DeploymentException(org.apache.axis2.deployment.DeploymentException)

Aggregations

SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)82 Mediator (org.apache.synapse.Mediator)23 MessageContext (org.apache.synapse.MessageContext)21 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)19 TestMediator (org.apache.synapse.mediators.TestMediator)18 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)16 Test (org.junit.Test)14 Map (java.util.Map)13 OMElement (org.apache.axiom.om.OMElement)12 ProxyService (org.apache.synapse.core.axis2.ProxyService)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 EndpointReference (org.apache.axis2.addressing.EndpointReference)11 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)11 Options (org.apache.axis2.client.Options)9 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)8 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)7 OMAttribute (org.apache.axiom.om.OMAttribute)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)6 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)6 File (java.io.File)4