use of org.apache.synapse.mediators.filters.SwitchMediator 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.filters.SwitchMediator in project wso2-synapse by wso2.
the class SwitchMediatorFactory method createSpecificMediator.
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
SwitchMediator switchMediator = new SwitchMediator();
OMAttribute source = elem.getAttribute(ATT_SOURCE);
if (source == null) {
String msg = "A 'source' XPath attribute is required for a switch mediator";
log.error(msg);
throw new SynapseException(msg);
} else {
try {
switchMediator.setSource(SynapsePathFactory.getSynapsePath(elem, ATT_SOURCE));
} catch (JaxenException e) {
String msg = "Invalid XPath for attribute 'source' : " + source.getAttributeValue();
log.error(msg);
throw new SynapseException(msg);
}
}
// after successfully creating the mediator
// set its common attributes such as tracing etc
processAuditStatus(switchMediator, elem);
Iterator iter = elem.getChildrenWithName(CASE_Q);
while (iter.hasNext()) {
OMElement caseElem = (OMElement) iter.next();
SwitchCase aCase = new SwitchCase();
OMAttribute regex = caseElem.getAttribute(ATT_REGEX);
if (regex == null) {
String msg = "The 'regex' attribute is required for a switch case definition";
log.error(msg);
throw new SynapseException(msg);
}
try {
aCase.setRegex(Pattern.compile(regex.getAttributeValue()));
} catch (PatternSyntaxException pse) {
String msg = "Invalid Regular Expression for attribute 'regex' : " + regex.getAttributeValue();
log.error(msg);
throw new SynapseException(msg);
}
aCase.setCaseMediator(AnonymousListMediatorFactory.createAnonymousListMediator(caseElem, properties));
switchMediator.addCase(aCase);
}
iter = elem.getChildrenWithName(DEFAULT_Q);
while (iter.hasNext()) {
SwitchCase aCase = new SwitchCase();
aCase.setCaseMediator(AnonymousListMediatorFactory.createAnonymousListMediator((OMElement) iter.next(), properties));
switchMediator.setDefaultCase(aCase);
// add only the *first* default if multiple are specified, ignore rest if any
break;
}
return switchMediator;
}
use of org.apache.synapse.mediators.filters.SwitchMediator in project wso2-synapse by wso2.
the class SwitchMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof SwitchMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
SwitchMediator mediator = (SwitchMediator) m;
OMElement switchMed = fac.createOMElement("switch", synNS);
saveTracingState(switchMed, mediator);
if (mediator.getSource() != null) {
SynapsePathSerializer.serializePath(mediator.getSource(), switchMed, "source");
} else {
handleException("Invalid switch mediator. Source required");
}
for (SwitchCase aCase : mediator.getCases()) {
OMElement caseElem = fac.createOMElement("case", synNS);
if (aCase.getRegex() != null) {
caseElem.addAttribute(fac.createOMAttribute("regex", nullNS, aCase.getRegex().pattern()));
} else {
handleException("Invalid switch case. Regex required");
}
AnonymousListMediator caseMediator = aCase.getCaseMediator();
if (caseMediator != null) {
new AnonymousListMediatorSerializer().serializeMediator(caseElem, caseMediator);
switchMed.addChild(caseElem);
}
}
SwitchCase defaultCase = mediator.getDefaultCase();
if (defaultCase != null) {
OMElement caseDefaultElem = fac.createOMElement("default", synNS);
AnonymousListMediator caseDefaultMediator = defaultCase.getCaseMediator();
if (caseDefaultMediator != null) {
new AnonymousListMediatorSerializer().serializeMediator(caseDefaultElem, caseDefaultMediator);
switchMed.addChild(caseDefaultElem);
}
}
return switchMed;
}
Aggregations