Search in sources :

Example 1 with FilterMediator

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;
}
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 2 with FilterMediator

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;
}
Also used : AttributeKey(io.netty.util.AttributeKey) HTTPEndpoint(org.apache.synapse.endpoints.HTTPEndpoint) SendMediator(org.apache.synapse.mediators.builtin.SendMediator) HashMap(java.util.HashMap) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) Endpoint(org.apache.synapse.endpoints.Endpoint) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Mediator(org.apache.synapse.Mediator) Map(java.util.Map) Resource(org.apache.synapse.api.Resource) Optional(java.util.Optional) MessageContext(org.apache.synapse.MessageContext) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) HTTPEndpoint(org.apache.synapse.endpoints.HTTPEndpoint) HTTPEndpoint(org.apache.synapse.endpoints.HTTPEndpoint) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) SendMediator(org.apache.synapse.mediators.builtin.SendMediator) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) Mediator(org.apache.synapse.Mediator) SendMediator(org.apache.synapse.mediators.builtin.SendMediator)

Example 3 with FilterMediator

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;
}
Also used : JaxenException(org.jaxen.JaxenException) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with FilterMediator

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;
}
Also used : FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) OMElement(org.apache.axiom.om.OMElement)

Aggregations

FilterMediator (org.apache.synapse.mediators.filters.FilterMediator)4 OMElement (org.apache.axiom.om.OMElement)2 Mediator (org.apache.synapse.Mediator)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 AttributeKey (io.netty.util.AttributeKey)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 MessageContext (org.apache.synapse.MessageContext)1 Resource (org.apache.synapse.api.Resource)1 SwitchCase (org.apache.synapse.config.xml.SwitchCase)1 EnclosedInlinedSequence (org.apache.synapse.debug.constructs.EnclosedInlinedSequence)1 Endpoint (org.apache.synapse.endpoints.Endpoint)1 HTTPEndpoint (org.apache.synapse.endpoints.HTTPEndpoint)1 IndirectEndpoint (org.apache.synapse.endpoints.IndirectEndpoint)1 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)1 CommentMediator (org.apache.synapse.mediators.builtin.CommentMediator)1 ForEachMediator (org.apache.synapse.mediators.builtin.ForEachMediator)1