use of org.apache.synapse.mediators.builtin.ForEachMediator in project wso2-synapse by wso2.
the class ForEachMediatorSerializer method serializeSpecificMediator.
@Override
protected OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof ForEachMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
OMElement forEachElem = fac.createOMElement("foreach", synNS);
saveTracingState(forEachElem, m);
ForEachMediator forEachMed = (ForEachMediator) m;
if (forEachMed.getId() != null) {
forEachElem.addAttribute("id", forEachMed.getId(), nullNS);
}
if (forEachMed.getExpression() != null) {
SynapsePathSerializer.serializePath(forEachMed.getExpression(), forEachElem, "expression");
} else {
handleException("Missing expression of the ForEach which is required.");
}
if (forEachMed.getSequenceRef() != null) {
forEachElem.addAttribute("sequence", forEachMed.getSequenceRef(), null);
} else if (forEachMed.getSequence() != null) {
SequenceMediatorSerializer seqSerializer = new SequenceMediatorSerializer();
OMElement seqElement = seqSerializer.serializeAnonymousSequence(null, forEachMed.getSequence());
seqElement.setLocalName("sequence");
forEachElem.addChild(seqElement);
}
serializeComments(forEachElem, forEachMed.getCommentsList());
return forEachElem;
}
use of org.apache.synapse.mediators.builtin.ForEachMediator 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;
}
use of org.apache.synapse.mediators.builtin.ForEachMediator 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;
}
Aggregations