Search in sources :

Example 1 with ConditionalRouterMediator

use of org.apache.synapse.mediators.filters.router.ConditionalRouterMediator in project wso2-synapse by wso2.

the class ConditionalRouterMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ConditionalRouterMediator conditionalRouterMediator = new ConditionalRouterMediator();
    processAuditStatus(conditionalRouterMediator, elem);
    if (elem.getAttribute(CONTINUE_AFTER_ATTR) != null) {
        if (JavaUtils.isTrueExplicitly(elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim())) {
            conditionalRouterMediator.setContinueAfter(true);
        } else if (JavaUtils.isFalseExplicitly(elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim())) {
            conditionalRouterMediator.setContinueAfter(false);
        } else {
            handleException("continueAfter attribute value of the conditionalRouter must " + "be either 'true' or 'false', the value found is : " + elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim());
        }
    }
    Iterator itr = elem.getChildrenWithName(ROUTE_Q);
    while (itr.hasNext()) {
        OMElement routeElem = (OMElement) itr.next();
        ConditionalRoute conditionalRoute = new ConditionalRoute();
        if (routeElem.getAttribute(BREAK_ROUTE_ATTR) != null) {
            if (JavaUtils.isTrueExplicitly(routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim())) {
                conditionalRoute.setBreakRoute(true);
            } else if (JavaUtils.isFalseExplicitly(routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim())) {
                conditionalRoute.setBreakRoute(false);
            } else {
                handleException("breakRoute attribute value of the conditionalRoute element must " + "be either 'true' or 'false', the value found is : " + routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim());
            }
        }
        OMElement conditionElem = routeElem.getFirstChildWithName(CONDITION_Q);
        if (conditionElem == null) {
            handleException("Couldn't find the condition of the conditional router");
            return null;
        }
        try {
            Evaluator evaluator = EvaluatorFactoryFinder.getInstance().getEvaluator(conditionElem.getFirstElement());
            conditionalRoute.setEvaluator(evaluator);
        } catch (EvaluatorException ee) {
            handleException("Couldn't build the condition of the conditional router", ee);
        }
        OMElement targetElem = routeElem.getFirstChildWithName(TARGET_Q);
        Target target = TargetFactory.createTarget(targetElem, properties);
        if (JavaUtils.isTrueExplicitly(routeElem.getAttributeValue(ASYNCHRONOUS_ATTR))) {
            target.setAsynchronous(true);
        } else {
            target.setAsynchronous(false);
        }
        conditionalRoute.setTarget(target);
        conditionalRouterMediator.addRoute(conditionalRoute);
    }
    return conditionalRouterMediator;
}
Also used : Target(org.apache.synapse.mediators.eip.Target) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) ConditionalRoute(org.apache.synapse.mediators.filters.router.ConditionalRoute) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) Evaluator(org.apache.synapse.commons.evaluators.Evaluator) ConditionalRouterMediator(org.apache.synapse.mediators.filters.router.ConditionalRouterMediator)

Example 2 with ConditionalRouterMediator

use of org.apache.synapse.mediators.filters.router.ConditionalRouterMediator in project wso2-synapse by wso2.

the class ConditionalRouterMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    OMElement conditionalRouterElem = fac.createOMElement("conditionalRouter", synNS);
    saveTracingState(conditionalRouterElem, m);
    ConditionalRouterMediator conditionalRouterMediator = (ConditionalRouterMediator) m;
    if (conditionalRouterMediator.isContinueAfterExplicitlySet()) {
        conditionalRouterElem.addAttribute("continueAfter", Boolean.toString(conditionalRouterMediator.isContinueAfter()), nullNS);
    }
    for (ConditionalRoute conditionalRoute : conditionalRouterMediator.getConditionalRoutes()) {
        OMElement routeElem = fac.createOMElement("conditionalRoute", synNS);
        if (conditionalRoute.isBreakRouteExplicitlySet()) {
            routeElem.addAttribute("breakRoute", Boolean.toString(conditionalRoute.isBreakRoute()), nullNS);
        }
        if (conditionalRoute.getEvaluator() != null) {
            EvaluatorSerializer evaluatorSerializer = EvaluatorSerializerFinder.getInstance().getSerializer(conditionalRoute.getEvaluator().getName());
            if (evaluatorSerializer != null) {
                OMElement conditionElement = fac.createOMElement("condition", synNS);
                try {
                    evaluatorSerializer.serialize(conditionElement, conditionalRoute.getEvaluator());
                } catch (EvaluatorException e) {
                    handleException("Cannot serialize the Evaluator", e);
                }
                routeElem.addChild(conditionElement);
            }
        }
        if (conditionalRoute.getTarget() != null) {
            routeElem.addChild(TargetSerializer.serializeTarget(conditionalRoute.getTarget()));
        } else {
            handleException("ConditionalRoute in a conditional router has to have a target");
        }
        if (conditionalRoute.getTarget().isAsynchronous()) {
            routeElem.addAttribute(fac.createOMAttribute("asynchronous", nullNS, "true"));
        }
        conditionalRouterElem.addChild(routeElem);
    }
    return conditionalRouterElem;
}
Also used : EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) ConditionalRoute(org.apache.synapse.mediators.filters.router.ConditionalRoute) OMElement(org.apache.axiom.om.OMElement) ConditionalRouterMediator(org.apache.synapse.mediators.filters.router.ConditionalRouterMediator) EvaluatorSerializer(org.apache.synapse.commons.evaluators.config.EvaluatorSerializer)

Aggregations

OMElement (org.apache.axiom.om.OMElement)2 EvaluatorException (org.apache.synapse.commons.evaluators.EvaluatorException)2 ConditionalRoute (org.apache.synapse.mediators.filters.router.ConditionalRoute)2 ConditionalRouterMediator (org.apache.synapse.mediators.filters.router.ConditionalRouterMediator)2 Iterator (java.util.Iterator)1 Evaluator (org.apache.synapse.commons.evaluators.Evaluator)1 EvaluatorSerializer (org.apache.synapse.commons.evaluators.config.EvaluatorSerializer)1 Target (org.apache.synapse.mediators.eip.Target)1