Search in sources :

Example 16 with Mediator

use of org.apache.synapse.Mediator 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 17 with Mediator

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

the class SequenceDeployer method updateSynapseArtifact.

@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
    Mediator m = MediatorFactoryFinder.getInstance().getMediator(artifactConfig, properties);
    CustomLogSetter.getInstance().setLogAppender((m != null) ? ((SequenceMediator) m).getArtifactContainerName() : "");
    if (log.isDebugEnabled()) {
        log.debug("Sequence update from file : " + fileName + " has started");
    }
    try {
        if (m == null || !(m instanceof SequenceMediator)) {
            handleSynapseArtifactDeploymentError("Sequence update failed. The artifact " + "defined in the file: " + fileName + " is not a valid sequence.");
            return null;
        }
        SequenceMediator seq = (SequenceMediator) m;
        seq.setFileName(new File(fileName).getName());
        if ((SynapseConstants.MAIN_SEQUENCE_KEY.equals(existingArtifactName) || SynapseConstants.FAULT_SEQUENCE_KEY.equals(existingArtifactName)) && !existingArtifactName.equals(seq.getName())) {
            handleSynapseArtifactDeploymentError(existingArtifactName + " sequence cannot be renamed");
        }
        if (log.isDebugEnabled()) {
            log.debug("Sequence: " + seq.getName() + " has been built from the file: " + fileName);
        }
        seq.init(getSynapseEnvironment());
        SequenceMediator existingSeq = getSynapseConfiguration().getDefinedSequences().get(existingArtifactName);
        if (existingArtifactName.equals(seq.getName())) {
            getSynapseConfiguration().updateSequence(existingArtifactName, seq);
        } else {
            getSynapseConfiguration().addSequence(seq.getName(), seq);
            getSynapseConfiguration().removeSequence(existingArtifactName);
            log.info("Sequence: " + existingArtifactName + " has been undeployed");
        }
        log.info("Sequence: " + seq.getName() + " has been updated from the file: " + fileName);
        // Give some time for worker threads to release the old sequence
        waitForCompletion();
        existingSeq.destroy();
        return seq.getName();
    } catch (DeploymentException e) {
        handleSynapseArtifactDeploymentError("Error while updating the sequence from the " + "file: " + fileName);
    }
    return null;
}
Also used : Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) DeploymentException(org.apache.axis2.deployment.DeploymentException) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) File(java.io.File)

Example 18 with Mediator

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

the class AbstractListMediator method init.

/**
 * Initialize child mediators recursively
 * @param se synapse environment
 */
public void init(SynapseEnvironment se) {
    if (log.isDebugEnabled()) {
        log.debug("Initializing child mediators of mediator : " + getType());
    }
    for (int i = 0; i < mediators.size(); i++) {
        Mediator mediator = mediators.get(i);
        mediator.setMediatorPosition(i);
        if (mediator instanceof ManagedLifecycle) {
            ((ManagedLifecycle) mediator).init(se);
        }
        if (mediator.isContentAware()) {
            if (log.isDebugEnabled()) {
                log.debug(mediator.getType() + " is content aware, setting sequence <" + getType() + "> as content aware");
            }
            sequenceContentAware = true;
        }
    }
}
Also used : Mediator(org.apache.synapse.Mediator) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Example 19 with Mediator

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

the class TemplateDeployer method updateSynapseArtifact.

@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("Template update from file : " + fileName + " has started");
    }
    try {
        OMElement element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
        if (element != null) {
            org.apache.synapse.config.xml.endpoints.TemplateFactory templateFactory = new org.apache.synapse.config.xml.endpoints.TemplateFactory();
            Template tm = templateFactory.createEndpointTemplate(artifactConfig, properties);
            tm.setFileName(new File(fileName).getName());
            if (log.isDebugEnabled()) {
                log.debug("Endpoint Template named '" + tm.getName() + "' has been built from the file " + fileName);
            }
            Template existingSt = getSynapseConfiguration().getEndpointTemplate(existingArtifactName);
            if (existingArtifactName.equals(tm.getName())) {
                getSynapseConfiguration().updateEndpointTemplate(tm.getName(), tm);
            } else {
                getSynapseConfiguration().addEndpointTemplate(tm.getName(), tm);
                getSynapseConfiguration().removeEndpointTemplate(existingSt.getName());
                log.info("Template: " + existingArtifactName + " has been undeployed");
            }
            log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
            return tm.getName();
        } else {
            element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sequence"));
            if (element != null) {
                Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(artifactConfig, properties);
                if (mediator instanceof TemplateMediator) {
                    TemplateMediator tm = (TemplateMediator) mediator;
                    tm.setFileName((new File(fileName)).getName());
                    if (log.isDebugEnabled()) {
                        log.debug("Sequence Template named '" + tm.getName() + "' has been built from the file " + fileName);
                    }
                    tm.init(getSynapseEnvironment());
                    if (log.isDebugEnabled()) {
                        log.debug("Initialized the Template : " + tm.getName());
                    }
                    TemplateMediator existingSt = getSynapseConfiguration().getSequenceTemplate(existingArtifactName);
                    if (existingArtifactName.equals(tm.getName())) {
                        getSynapseConfiguration().updateSequenceTemplate(tm.getName(), tm);
                    } else {
                        getSynapseConfiguration().addSequenceTemplate(tm.getName(), tm);
                        getSynapseConfiguration().removeSequenceTemplate(existingSt.getName());
                        log.info("Template: " + existingArtifactName + " has been undeployed");
                    }
                    existingSt.destroy();
                    log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
                    return tm.getName();
                }
            }
        }
    } catch (DeploymentException e) {
        handleSynapseArtifactDeploymentError("Error while updating the Template from the " + "file: " + fileName);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) Template(org.apache.synapse.endpoints.Template) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File)

Example 20 with Mediator

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

the class ForEachMediatorTest method testForEachXpathNodeRelativePath.

/**
 * Testing when the xpath returns only one element
 *
 * @throws Exception
 */
public void testForEachXpathNodeRelativePath() throws Exception {
    // Clear envelope
    if (testCtx.getEnvelope().getBody().getFirstElement() != null) {
        testCtx.getEnvelope().getBody().getFirstElement().detach();
    }
    testCtx.getEnvelope().getBody().addChild(createOMElement("<original>" + "<itr id=\"one\">test-split-context-itr1-body</itr>" + "<itr>test-split-context-itr2-body</itr>" + "</original>"));
    MediatorFactory fac = new ForEachMediatorFactory();
    Mediator foreach = fac.createMediator(createOMElement("<foreach " + "expression=\"//itr[@id='one']\" sequence=\"seqRef\" />"), new Properties());
    helperMediator.clearMediatedContexts();
    foreach.mediate(testCtx);
    assertEquals(1, helperMediator.getMsgCount());
    assertEquals("<itr id=\"one\">test-split-context-itr1-body</itr>", helperMediator.getMediatedContext(0).getEnvelope().getBody().getFirstElement().toString());
}
Also used : ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) MediatorFactory(org.apache.synapse.config.xml.MediatorFactory) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Properties(java.util.Properties)

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