use of org.apache.synapse.mediators.filters.FilterMediator 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.FilterMediator in project carbon-apimgt by wso2.
the class WebSocketUtils method getEndpointUrl.
/**
* Gets the uri-template of the endpoint, which has been specified in the given resource.
* @param resource API Resource
* @param synCtx MessageContext
* @return URI template
*/
public static String getEndpointUrl(Resource resource, MessageContext synCtx) {
Optional<Mediator> filterMediator = resource.getInSequence().getList().stream().filter(m -> m instanceof FilterMediator).findFirst();
if (filterMediator.isPresent()) {
Optional<Mediator> sendMediator = ((FilterMediator) filterMediator.get()).getList().stream().filter(m -> m instanceof SendMediator).findFirst();
if (sendMediator.isPresent()) {
Endpoint endpoint = ((SendMediator) sendMediator.get()).getEndpoint();
if (endpoint instanceof IndirectEndpoint) {
String endpointKey = ((IndirectEndpoint) endpoint).getKey();
Endpoint directEndpoint = synCtx.getConfiguration().getEndpoint(endpointKey);
if (directEndpoint instanceof HTTPEndpoint) {
return ((HTTPEndpoint) synCtx.getConfiguration().getEndpoint(endpointKey)).getUriTemplate().getTemplate();
}
}
}
}
// Ideally we won't reach here
return null;
}
use of org.apache.synapse.mediators.filters.FilterMediator in project wso2-synapse by wso2.
the class FilterMediatorFactory method createSpecificMediator.
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
FilterMediator filter = new FilterMediator();
OMAttribute attXpath = elem.getAttribute(ATT_XPATH);
OMAttribute attSource = elem.getAttribute(ATT_SOURCE);
OMAttribute attRegex = elem.getAttribute(ATT_REGEX);
if (attXpath != null) {
if (attXpath.getAttributeValue() != null && attXpath.getAttributeValue().trim().length() == 0) {
handleException("Invalid attribute value specified for xpath");
} else {
try {
filter.setXpath(SynapsePathFactory.getSynapsePath(elem, ATT_XPATH));
} catch (JaxenException e) {
handleException("Invalid XPath expression for attribute xpath : " + attXpath.getAttributeValue(), e);
}
}
} else if (attSource != null && attRegex != null) {
if ((attSource.getAttributeValue() != null && attSource.getAttributeValue().trim().length() == 0) || (attRegex.getAttributeValue() != null && attRegex.getAttributeValue().trim().length() == 0)) {
handleException("Invalid attribute values for source and/or regex specified");
} else {
try {
filter.setSource(SynapsePathFactory.getSynapsePath(elem, ATT_SOURCE));
} catch (JaxenException e) {
handleException("Invalid XPath expression for attribute source : " + attSource.getAttributeValue(), e);
}
try {
filter.setRegex(Pattern.compile(attRegex.getAttributeValue()));
} catch (PatternSyntaxException pse) {
handleException("Invalid Regular Expression for attribute regex : " + attRegex.getAttributeValue(), pse);
}
}
} else {
handleException("An xpath or (source, regex) attributes are required for a filter");
}
// after successfully creating the mediator
// set its common attributes such as tracing etc
processAuditStatus(filter, elem);
OMElement thenElem = elem.getFirstChildWithName(THEN_Q);
if (thenElem != null) {
filter.setThenElementPresent(true);
OMAttribute sequenceAttr = thenElem.getAttribute(ATT_SEQUENCE);
if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null) {
filter.setThenKey(sequenceAttr.getAttributeValue());
} else {
addChildren(thenElem, filter, properties);
}
OMElement elseElem = elem.getFirstChildWithName(ELSE_Q);
if (elseElem != null) {
sequenceAttr = elseElem.getAttribute(ATT_SEQUENCE);
if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null) {
filter.setElseKey(sequenceAttr.getAttributeValue());
} else {
AnonymousListMediator listMediator = AnonymousListMediatorFactory.createAnonymousListMediator(elseElem, properties);
filter.setElseMediator(listMediator);
}
}
} else {
filter.setThenElementPresent(false);
addChildren(elem, filter, properties);
}
return filter;
}
use of org.apache.synapse.mediators.filters.FilterMediator in project wso2-synapse by wso2.
the class FilterMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof FilterMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
FilterMediator mediator = (FilterMediator) m;
OMElement filter = fac.createOMElement("filter", synNS);
if (mediator.getSource() != null && mediator.getRegex() != null) {
SynapsePathSerializer.serializePath(mediator.getSource(), filter, "source");
filter.addAttribute(fac.createOMAttribute("regex", nullNS, mediator.getRegex().pattern()));
} else if (mediator.getXpath() != null) {
SynapsePathSerializer.serializePath(mediator.getXpath(), filter, "xpath");
} else {
handleException("Invalid filter mediator. " + "Should have either a 'source' and a 'regex' OR an 'xpath' ");
}
saveTracingState(filter, mediator);
if (mediator.isThenElementPresent()) {
OMElement thenElem = fac.createOMElement("then", synNS);
filter.addChild(thenElem);
if (mediator.getThenKey() != null) {
thenElem.addAttribute(fac.createOMAttribute("sequence", nullNS, mediator.getThenKey()));
} else {
serializeChildren(thenElem, mediator.getList());
}
if (mediator.getElseMediator() != null || mediator.getElseKey() != null) {
OMElement elseElem = fac.createOMElement("else", synNS);
filter.addChild(elseElem);
if (mediator.getElseKey() != null) {
elseElem.addAttribute(fac.createOMAttribute("sequence", nullNS, mediator.getElseKey()));
} else {
serializeChildren(elseElem, mediator.getElseMediator().getList());
}
}
} else {
serializeChildren(filter, mediator.getList());
}
return filter;
}
Aggregations