Search in sources :

Example 76 with SequenceMediator

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

the class DebugManagerTest method testDebugManagerProcessCommandClearSkipProxyOutSequence.

public void testDebugManagerProcessCommandClearSkipProxyOutSequence() throws Exception {
    ProxyService ps = new ProxyService("test_proxy_8");
    TestMediator in1 = new TestMediator();
    TestMediator in2 = new TestMediator();
    TestMediator in3 = new TestMediator();
    SequenceMediator inSeq = new SequenceMediator();
    inSeq.addChild(in1);
    inSeq.addChild(in2);
    inSeq.addChild(in3);
    TestMediator out1 = new TestMediator();
    TestMediator out2 = new TestMediator();
    TestMediator out3 = new TestMediator();
    SequenceMediator outSeq = new SequenceMediator();
    outSeq.addChild(out1);
    outSeq.addChild(out2);
    outSeq.addChild(out3);
    ps.setTargetInLineInSequence(inSeq);
    ps.setTargetInLineOutSequence(inSeq);
    synConfig.addProxyService(ps.getName(), ps);
    String debug_command = "{\"command\":\"set\",\"command-argument\":\"skip\",\"mediation-component\":\"sequence\"," + "\"sequence\":{\"proxy\":{\"proxy-key\":\"test_proxy_8\",\"sequence-type\":\"proxy_outseq\"," + "\"mediator-position\":\"0\"}}}";
    dm.processDebugCommand(debug_command);
    debug_command = "{\"command\":\"clear\",\"command-argument\":\"skip\",\"mediation-component\":\"sequence\"," + "\"sequence\":{\"proxy\":{\"proxy-key\":\"test_proxy_8\",\"sequence-type\":\"proxy_outseq\"," + "\"mediator-position\":\"0\"}}}";
    dm.processDebugCommand(debug_command);
    assertTrue(!((AbstractMediator) ps.getTargetInLineOutSequence().getChild(0)).isSkipEnabled());
}
Also used : ProxyService(org.apache.synapse.core.axis2.ProxyService) TestMediator(org.apache.synapse.mediators.TestMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator)

Example 77 with SequenceMediator

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

the class ForEachMediatorFactory method createSpecificMediator.

@Override
protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ForEachMediator mediator = new ForEachMediator();
    processAuditStatus(mediator, elem);
    OMAttribute id = elem.getAttribute(ID_Q);
    if (id != null) {
        mediator.setId(id.getAttributeValue());
    }
    OMAttribute expression = elem.getAttribute(ATT_EXPRN);
    if (expression != null) {
        try {
            mediator.setExpression(SynapsePathFactory.getSynapsePath(elem, ATT_EXPRN));
        } catch (JaxenException e) {
            handleException("Unable to build the ForEach Mediator. " + "Invalid XPath/JSONPath " + expression.getAttributeValue(), e);
        }
    } else {
        handleException("XPath/JSONPath expression is required " + "for an ForEach Mediator under the \"expression\" attribute");
    }
    OMAttribute sequenceAttr = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "sequence"));
    OMElement sequence;
    if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null) {
        mediator.setSequenceRef(sequenceAttr.getAttributeValue());
    } else if ((sequence = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "sequence"))) != null) {
        SequenceMediatorFactory fac = new SequenceMediatorFactory();
        SequenceMediator sequenceMediator = fac.createAnonymousSequence(sequence, properties);
        if (validateSequence(sequenceMediator)) {
            mediator.setSequence(sequenceMediator);
        } else {
            handleException("Sequence cannot contain Call, Send or CallOut mediators");
        }
    }
    addAllCommentChildrenToList(elem, mediator.getCommentsList());
    return mediator;
}
Also used : ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) QName(javax.xml.namespace.QName) JaxenException(org.jaxen.JaxenException) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 78 with SequenceMediator

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

the class SynapseConfigUtils method setDefaultMainSequence.

/**
 * Return the main sequence if one is not defined. This implementation defaults to
 * a simple sequence with a <send/>
 *
 * @param config the configuration to be updated
 */
public static void setDefaultMainSequence(SynapseConfiguration config) {
    SequenceMediator main = new SequenceMediator();
    main.setName(SynapseConstants.MAIN_SEQUENCE_KEY);
    main.addChild(new LogMediator());
    main.addChild(new DropMediator());
    config.addSequence(SynapseConstants.MAIN_SEQUENCE_KEY, main);
    // set the aspect configuration
    AspectConfiguration configuration = new AspectConfiguration(main.getName());
    main.configure(configuration);
}
Also used : LogMediator(org.apache.synapse.mediators.builtin.LogMediator) DropMediator(org.apache.synapse.mediators.builtin.DropMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration)

Example 79 with SequenceMediator

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

the class AggregateMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    AggregateMediator mediator = new AggregateMediator();
    processAuditStatus(mediator, elem);
    OMAttribute id = elem.getAttribute(ID_Q);
    if (id != null) {
        mediator.setId(id.getAttributeValue());
    }
    OMElement corelateOn = elem.getFirstChildWithName(CORELATE_ON_Q);
    if (corelateOn != null) {
        OMAttribute corelateExpr = corelateOn.getAttribute(EXPRESSION_Q);
        if (corelateExpr != null) {
            try {
                mediator.setCorrelateExpression(SynapsePathFactory.getSynapsePath(corelateOn, EXPRESSION_Q));
            } catch (JaxenException e) {
                handleException("Unable to load the corelate XPATH expression", e);
            }
        }
    }
    OMElement completeCond = elem.getFirstChildWithName(COMPLETE_CONDITION_Q);
    if (completeCond != null) {
        OMAttribute completeTimeout = completeCond.getAttribute(TIMEOUT_Q);
        if (completeTimeout != null) {
            mediator.setCompletionTimeoutMillis(Long.parseLong(completeTimeout.getAttributeValue()) * 1000);
        }
        OMElement messageCount = completeCond.getFirstChildWithName(MESSAGE_COUNT_Q);
        if (messageCount != null) {
            OMAttribute min = messageCount.getAttribute(MIN_Q);
            if (min != null) {
                mediator.setMinMessagesToComplete(new ValueFactory().createValue("min", messageCount));
            }
            OMAttribute max = messageCount.getAttribute(MAX_Q);
            if (max != null) {
                mediator.setMaxMessagesToComplete(new ValueFactory().createValue("max", messageCount));
            }
        }
    }
    OMElement onComplete = elem.getFirstChildWithName(ON_COMPLETE_Q);
    if (onComplete != null) {
        OMAttribute aggregateElementType = onComplete.getAttribute(AGGREGATE_ELEMENT_TYPE);
        if (aggregateElementType != null) {
            mediator.setAggregateElementType(new ValueFactory().createValue("aggregateElementType", onComplete));
        }
        OMAttribute aggregateExpr = onComplete.getAttribute(EXPRESSION_Q);
        if (aggregateExpr != null) {
            try {
                mediator.setAggregationExpression(SynapsePathFactory.getSynapsePath(onComplete, EXPRESSION_Q));
            } catch (JaxenException e) {
                handleException("Unable to load the aggregating XPATH", e);
            }
        }
        OMAttribute enclosingElementPropertyName = onComplete.getAttribute(ENCLOSING_ELEMENT_PROPERTY);
        if (enclosingElementPropertyName != null) {
            mediator.setEnclosingElementPropertyName(enclosingElementPropertyName.getAttributeValue());
        }
        OMAttribute onCompleteSequence = onComplete.getAttribute(SEQUENCE_Q);
        if (onCompleteSequence != null) {
            mediator.setOnCompleteSequenceRef(onCompleteSequence.getAttributeValue());
        } else if (onComplete.getFirstElement() != null) {
            mediator.setOnCompleteSequence((new SequenceMediatorFactory()).createAnonymousSequence(onComplete, properties));
        } else {
            SequenceMediator sequence = new SequenceMediator();
            sequence.addChild(new DropMediator());
            mediator.setOnCompleteSequence(sequence);
        }
    }
    addAllCommentChildrenToList(elem, mediator.getCommentsList());
    return mediator;
}
Also used : AggregateMediator(org.apache.synapse.mediators.eip.aggregator.AggregateMediator) JaxenException(org.jaxen.JaxenException) DropMediator(org.apache.synapse.mediators.builtin.DropMediator) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 80 with SequenceMediator

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

the class MessageInjector method execute.

/**
 * This will be invoked by the scheduler to inject the message
 * in to the SynapseEnvironment
 */
public void execute() {
    if (log.isDebugEnabled()) {
        log.debug("execute");
    }
    if (synapseEnvironment == null) {
        handleError("Synapse Environment not set");
        return;
    }
    if (synapseEnvironment.getTaskManager() != null && !synapseEnvironment.getTaskManager().isInitialized()) {
        log.warn("Task Manager not initialized. Not executing the cycle");
        return;
    }
    if (message == null && registryKey == null) {
        handleError("message or registry-key not set");
        return;
    }
    if (INJECT_TO_PROXY.equalsIgnoreCase(injectTo)) {
        if (proxyName == null || proxyName.equals("")) {
            handleError("Proxy service name not specified");
        }
        // Prepare axis2 message context
        org.apache.axis2.context.MessageContext axis2MsgCtx = new org.apache.axis2.context.MessageContext();
        ConfigurationContext configurationContext = ((Axis2SynapseEnvironment) synapseEnvironment).getAxis2ConfigurationContext();
        axis2MsgCtx.setConfigurationContext(configurationContext);
        axis2MsgCtx.setIncomingTransportName(Constants.TRANSPORT_LOCAL);
        axis2MsgCtx.setServerSide(true);
        axis2MsgCtx.setMessageID(UIDGenerator.generateURNString());
        try {
            AxisService axisService = configurationContext.getAxisConfiguration().getService(proxyName);
            if (axisService == null) {
                handleError("Proxy Service: " + proxyName + " not found");
            }
            axis2MsgCtx.setAxisService(axisService);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while attempting to find the Proxy Service");
        }
        if (to != null) {
            axis2MsgCtx.setTo(new EndpointReference(to));
        }
        SOAPEnvelope envelope = null;
        if (format == null) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope();
        } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (POX_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
        } else if (GET_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
            axis2MsgCtx.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
        } else {
            handleError("incorrect format specified");
        }
        try {
            PayloadHelper.setXMLPayload(envelope, message.cloneOMElement());
            axis2MsgCtx.setEnvelope(envelope);
        } catch (AxisFault axisFault) {
            handleError("Error in setting the message payload : " + message);
        }
        if (soapAction != null) {
            axis2MsgCtx.setSoapAction(soapAction);
        }
        try {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to proxy service : " + proxyName);
            }
            AxisEngine.receive(axis2MsgCtx);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while invoking proxy service : " + proxyName);
        }
    } else {
        MessageContext mc = synapseEnvironment.createMessageContext();
        mc.setMessageID(UIDGenerator.generateURNString());
        mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
        if (to != null) {
            mc.setTo(new EndpointReference(to));
        }
        if (registryKey == null) {
            if (format == null) {
                PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
            } else {
                try {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
                } catch (AxisFault axisFault) {
                    handleError("Error in setting the message payload : " + message);
                }
            }
        } else {
            Object entry = mc.getEntry(registryKey);
            if (entry == null) {
                handleError("Key " + registryKey + " not found ");
            }
            String text = "";
            if (entry instanceof OMElement) {
                OMElement e = (OMElement) entry;
                removeIndentations(e);
                text = e.toString();
            } else if (entry instanceof OMText) {
                text = ((OMText) entry).getText();
            } else if (entry instanceof String) {
                text = (String) entry;
            }
            OMElement omXML = null;
            try {
                omXML = AXIOMUtil.stringToOM(text);
                if (format == null) {
                    PayloadHelper.setXMLPayload(mc, omXML);
                } else {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, omXML);
                }
            } catch (XMLStreamException e) {
                handleError("Error parsing XML for JSON conversion, please check your property values return valid XML");
            } catch (AxisFault axisFault) {
                handleError("Error in setting the message payload : " + omXML);
            }
        }
        if (soapAction != null) {
            mc.setSoapAction(soapAction);
        }
        // Adding runtime properties to SynapseMessageContext, if exists
        if (runtimeProperties != null && runtimeProperties.size() > 0) {
            for (Map.Entry<String, Object> entry : runtimeProperties.entrySet()) {
                mc.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (INJECT_TO_SEQUENCE.equalsIgnoreCase(injectTo)) {
            if (sequenceName == null || sequenceName.equals("")) {
                handleError("Sequence name not specified");
            }
            SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(sequenceName);
            if (seq != null) {
                if (log.isDebugEnabled()) {
                    log.debug("injecting message to sequence : " + sequenceName);
                }
                if (Boolean.parseBoolean(invokeHandlers)) {
                    List handlers = mc.getEnvironment().getSynapseHandlers();
                    Iterator<SynapseHandler> iterator = handlers.iterator();
                    while (iterator.hasNext()) {
                        SynapseHandler handler = iterator.next();
                        if (!handler.handleRequestInFlow(mc)) {
                            return;
                        }
                    }
                }
                mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
                synapseEnvironment.injectAsync(mc, seq);
            } else {
                handleError("Sequence: " + sequenceName + " not found");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to main sequence");
            }
            if (Boolean.parseBoolean(invokeHandlers)) {
                List handlers = mc.getEnvironment().getSynapseHandlers();
                Iterator<SynapseHandler> iterator = handlers.iterator();
                while (iterator.hasNext()) {
                    SynapseHandler handler = iterator.next();
                    if (!handler.handleRequestInFlow(mc)) {
                        return;
                    }
                }
            }
            synapseEnvironment.injectMessage(mc);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) AxisService(org.apache.axis2.description.AxisService) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseHandler(org.apache.synapse.SynapseHandler) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) XMLStreamException(javax.xml.stream.XMLStreamException) OMText(org.apache.axiom.om.OMText) ArrayList(java.util.ArrayList) List(java.util.List) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)84 Mediator (org.apache.synapse.Mediator)24 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 ProxyService (org.apache.synapse.core.axis2.ProxyService)13 OMElement (org.apache.axiom.om.OMElement)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 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)8 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)8 OMAttribute (org.apache.axiom.om.OMAttribute)6 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 File (java.io.File)4