Search in sources :

Example 1 with IterateMediator

use of org.apache.synapse.mediators.eip.splitter.IterateMediator in project wso2-synapse by wso2.

the class IterateMediatorSerializer method serializeSpecificMediator.

/**
 * This method will implement the serialization logic of the IterateMediator class to the
 * relevant xml configuration
 *
 * @param m
 *          IterateMediator to be serialized
 *
 * @return OMElement describing the serialized configuration of the IterateMediator
 */
public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof IterateMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    OMElement itrElem = fac.createOMElement("iterate", synNS);
    saveTracingState(itrElem, m);
    IterateMediator itrMed = (IterateMediator) m;
    if (itrMed.isContinueParent()) {
        itrElem.addAttribute("continueParent", Boolean.toString(true), nullNS);
    }
    if (itrMed.getId() != null) {
        itrElem.addAttribute("id", itrMed.getId(), nullNS);
    }
    if (itrMed.isPreservePayload()) {
        itrElem.addAttribute("preservePayload", Boolean.toString(true), nullNS);
    }
    if (itrMed.isAttachPathPresent()) {
        SynapsePathSerializer.serializePath(itrMed.getAttachPath(), itrElem, "attachPath");
    }
    if (itrMed.getExpression() != null) {
        SynapsePathSerializer.serializePath(itrMed.getExpression(), itrElem, "expression");
    } else {
        handleException("Missing expression of the IterateMediator which is required.");
    }
    if (itrMed.getTarget() != null && !itrMed.getTarget().isAsynchronous()) {
        itrElem.addAttribute("sequential", "true", nullNS);
    }
    itrElem.addChild(TargetSerializer.serializeTarget(itrMed.getTarget()));
    serializeComments(itrElem, itrMed.getCommentsList());
    return itrElem;
}
Also used : OMElement(org.apache.axiom.om.OMElement) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator)

Example 2 with IterateMediator

use of org.apache.synapse.mediators.eip.splitter.IterateMediator in project wso2-synapse by wso2.

the class MediatorTreeTraverseUtil method getMediatorReference.

/**
 * Returns mediator referece associated with position while traversing the mediator tree.
 *
 * @param synCfg      synapse configuration reference
 * @param seqMediator sequence mediator which traverse happens
 * @param position    array of tree nodes specifying position integer with respect to it's parent node
 *                    starting from the root parent.
 * @return Mediator reference
 */
public static Mediator getMediatorReference(SynapseConfiguration synCfg, Mediator seqMediator, int[] position) {
    Mediator current_mediator = null;
    for (int counter = 0; counter < position.length; counter++) {
        if (counter == 0) {
            int mediatorCount = ((AbstractListMediator) seqMediator).getList().size();
            int correctedPosition = getCorrectedPossition((AbstractListMediator) seqMediator, position[counter]);
            if (mediatorCount > correctedPosition) {
                current_mediator = ((AbstractListMediator) seqMediator).getChild(correctedPosition);
            } else {
                log.warn("Mediator position requested is larger than last index : " + position[counter]);
            }
        }
        if (current_mediator != null && counter != 0) {
            if (current_mediator instanceof InvokeMediator) {
                current_mediator = synCfg.getSequenceTemplate(((InvokeMediator) current_mediator).getTargetTemplate());
            } else if (current_mediator instanceof FilterMediator) {
                if (position[counter] == 0) {
                    if (((FilterMediator) current_mediator).getElseMediator() != null) {
                        current_mediator = ((FilterMediator) current_mediator).getElseMediator();
                    } else if (((FilterMediator) current_mediator).getElseKey() != null) {
                        current_mediator = synCfg.getSequence(((FilterMediator) current_mediator).getElseKey());
                    }
                    continue;
                } else if (position[counter] == 1) {
                    if (((FilterMediator) current_mediator).getThenKey() != null) {
                        current_mediator = synCfg.getSequence(((FilterMediator) current_mediator).getThenKey());
                    } else {
                        counter = counter + 1;
                        if (counter < position.length) {
                            int mediatorCount = ((AbstractListMediator) current_mediator).getList().size();
                            int correctedPosition = getCorrectedPossition((AbstractListMediator) current_mediator, position[counter]);
                            if (mediatorCount > correctedPosition) {
                                current_mediator = ((AbstractListMediator) current_mediator).getChild(correctedPosition);
                            } else {
                                log.warn("Mediator position requested is larger than last index : " + position[counter]);
                            }
                        }
                    }
                    continue;
                }
            } else if (current_mediator instanceof SwitchMediator) {
                if (position[counter] == 0) {
                    SwitchCase switchCase = ((SwitchMediator) current_mediator).getDefaultCase();
                    if (switchCase != null) {
                        current_mediator = switchCase.getCaseMediator();
                    } else {
                        current_mediator = null;
                    }
                } else {
                    SwitchCase switchCase = ((SwitchMediator) current_mediator).getCases().get(position[counter] - 1);
                    if (switchCase != null) {
                        current_mediator = switchCase.getCaseMediator();
                    } else {
                        current_mediator = null;
                    }
                }
                continue;
            } else if (current_mediator instanceof AggregateMediator) {
                if (((AggregateMediator) current_mediator).getOnCompleteSequence() != null) {
                    current_mediator = ((AggregateMediator) current_mediator).getOnCompleteSequence();
                } else if (((AggregateMediator) current_mediator).getOnCompleteSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((AggregateMediator) current_mediator).getOnCompleteSequenceRef());
                }
            } else if (current_mediator instanceof ForEachMediator) {
                if (((ForEachMediator) current_mediator).getSequence() != null) {
                    current_mediator = ((ForEachMediator) current_mediator).getSequence();
                } else if (((ForEachMediator) current_mediator).getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((ForEachMediator) current_mediator).getSequenceRef());
                }
            } else if (current_mediator instanceof IterateMediator) {
                if (((IterateMediator) current_mediator).getTarget().getSequence() != null) {
                    current_mediator = ((IterateMediator) current_mediator).getTarget().getSequence();
                } else if (((IterateMediator) current_mediator).getTarget().getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((IterateMediator) current_mediator).getTarget().getSequenceRef());
                }
            } else if (current_mediator instanceof CloneMediator) {
                if (((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequence() != null) {
                    current_mediator = ((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequence();
                } else if (((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequenceRef());
                }
                continue;
            } else if (current_mediator.getType().equals("ThrottleMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, position[counter]);
                continue;
            } else if (current_mediator.getType().equals("EntitlementMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, position[counter]);
                continue;
            } else if (current_mediator.getType().equals("CacheMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, 0);
            }
            if (current_mediator != null && (current_mediator instanceof AbstractListMediator)) {
                int mediatorCount = ((AbstractListMediator) current_mediator).getList().size();
                int correctedPosition = getCorrectedPossition((AbstractListMediator) current_mediator, position[counter]);
                if (mediatorCount > correctedPosition) {
                    current_mediator = ((AbstractListMediator) current_mediator).getChild(correctedPosition);
                } else {
                    log.warn("Mediator position requested is larger than last index : " + position[counter]);
                }
            } else {
                current_mediator = null;
                break;
            }
        }
    }
    return current_mediator;
}
Also used : AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) SwitchCase(org.apache.synapse.config.xml.SwitchCase) AggregateMediator(org.apache.synapse.mediators.eip.aggregator.AggregateMediator) ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) EnclosedInlinedSequence(org.apache.synapse.debug.constructs.EnclosedInlinedSequence) InvokeMediator(org.apache.synapse.mediators.template.InvokeMediator) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) SwitchMediator(org.apache.synapse.mediators.filters.SwitchMediator) InvokeMediator(org.apache.synapse.mediators.template.InvokeMediator) ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator) CommentMediator(org.apache.synapse.mediators.builtin.CommentMediator) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) CloneMediator(org.apache.synapse.mediators.eip.splitter.CloneMediator) Mediator(org.apache.synapse.Mediator) AggregateMediator(org.apache.synapse.mediators.eip.aggregator.AggregateMediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) SwitchMediator(org.apache.synapse.mediators.filters.SwitchMediator) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator) CloneMediator(org.apache.synapse.mediators.eip.splitter.CloneMediator)

Example 3 with IterateMediator

use of org.apache.synapse.mediators.eip.splitter.IterateMediator in project wso2-synapse by wso2.

the class IterateMediatorFactory method createSpecificMediator.

/**
 * This method will create the IterateMediator by parsing the given xml configuration
 *
 * @param elem OMElement describing the configuration of the IterateMediaotr
 * @param properties properties passed
 * @return IterateMediator created from the given configuration
 */
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    IterateMediator mediator = new IterateMediator();
    processAuditStatus(mediator, elem);
    OMAttribute id = elem.getAttribute(ID_Q);
    if (id != null) {
        mediator.setId(id.getAttributeValue());
    }
    OMAttribute continueParent = elem.getAttribute(ATT_CONTPAR);
    if (continueParent != null) {
        mediator.setContinueParent(Boolean.valueOf(continueParent.getAttributeValue()));
    }
    OMAttribute preservePayload = elem.getAttribute(ATT_PREPLD);
    if (preservePayload != null) {
        mediator.setPreservePayload(Boolean.valueOf(preservePayload.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 IterateMediator. " + "Invalid XPATH " + expression.getAttributeValue(), e);
        }
    } else {
        handleException("XPATH expression is required " + "for an IterateMediator under the \"expression\" attribute");
    }
    OMAttribute attachPath = elem.getAttribute(ATT_ATTACHPATH);
    if (attachPath != null && !mediator.isPreservePayload()) {
        handleException("Wrong configuration for the iterate mediator :: if the iterator " + "should not preserve payload, then attachPath can not be present");
    }
    try {
        SynapsePath attachSynapsePath;
        if (attachPath != null) {
            attachSynapsePath = SynapsePathFactory.getSynapsePath(elem, ATT_ATTACHPATH);
            mediator.setAttachPathPresent(true);
            if (mediator.getExpression().getClass() != attachSynapsePath.getClass()) {
                handleException("Wrong configuraton for the iterate mediator :: both expression and " + "attachPath should be either jsonpath or xpath");
            }
        } else {
            if (mediator.getExpression() instanceof SynapseJsonPath) {
                attachSynapsePath = new SynapseJsonPath(DEFAULT_JSON_ATTACHPATH);
            } else {
                attachSynapsePath = new SynapseXPath(DEFAULT_XML_ATTACHPATH);
            }
            mediator.setAttachPathPresent(false);
        }
        OMElementUtils.addNameSpaces(attachSynapsePath, elem, log);
        mediator.setAttachPath(attachSynapsePath);
    } catch (JaxenException e) {
        handleException("Unable to build the IterateMediator. Invalid PATH " + attachPath.getAttributeValue(), e);
    }
    boolean asynchronous = true;
    OMAttribute asynchronousAttr = elem.getAttribute(ATT_SEQUENCIAL);
    if (asynchronousAttr != null && asynchronousAttr.getAttributeValue().equals("true")) {
        asynchronous = false;
    }
    OMElement targetElement = elem.getFirstChildWithName(TARGET_Q);
    if (targetElement != null) {
        Target target = TargetFactory.createTarget(targetElement, properties);
        if (target != null) {
            target.setAsynchronous(asynchronous);
            mediator.setTarget(target);
        }
    } else {
        handleException("Target for an iterate mediator is required :: missing target");
    }
    addAllCommentChildrenToList(elem, mediator.getCommentsList());
    return mediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Target(org.apache.synapse.mediators.eip.Target) JaxenException(org.jaxen.JaxenException) SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) OMElement(org.apache.axiom.om.OMElement) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

IterateMediator (org.apache.synapse.mediators.eip.splitter.IterateMediator)3 OMElement (org.apache.axiom.om.OMElement)2 OMAttribute (org.apache.axiom.om.OMAttribute)1 Mediator (org.apache.synapse.Mediator)1 SwitchCase (org.apache.synapse.config.xml.SwitchCase)1 EnclosedInlinedSequence (org.apache.synapse.debug.constructs.EnclosedInlinedSequence)1 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)1 CommentMediator (org.apache.synapse.mediators.builtin.CommentMediator)1 ForEachMediator (org.apache.synapse.mediators.builtin.ForEachMediator)1 Target (org.apache.synapse.mediators.eip.Target)1 AggregateMediator (org.apache.synapse.mediators.eip.aggregator.AggregateMediator)1 CloneMediator (org.apache.synapse.mediators.eip.splitter.CloneMediator)1 FilterMediator (org.apache.synapse.mediators.filters.FilterMediator)1 SwitchMediator (org.apache.synapse.mediators.filters.SwitchMediator)1 InvokeMediator (org.apache.synapse.mediators.template.InvokeMediator)1 SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)1 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)1 JaxenException (org.jaxen.JaxenException)1