Search in sources :

Example 1 with EvaluatorException

use of org.apache.synapse.commons.evaluators.EvaluatorException in project wso2-synapse by wso2.

the class ConditionalRouterMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    Map<String, String> evaluatorHeaders = new HashMap<String, String>();
    if (headers != null && headers instanceof Map) {
        Map headersMap = (Map) headers;
        for (Object entryObj : headersMap.entrySet()) {
            Map.Entry entry = (Map.Entry) entryObj;
            if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
                evaluatorHeaders.put((String) entry.getKey(), (String) entry.getValue());
            }
        }
    }
    String url = synCtx.getTo().getAddress();
    EvaluatorContext context = new EvaluatorContext(url, evaluatorHeaders);
    context.setProperties(((Axis2MessageContext) synCtx).getProperties());
    context.setMessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext());
    try {
        for (ConditionalRoute conditionalRoute : conditionalRoutes) {
            if (conditionalRoute.getEvaluator().evaluate(context)) {
                conditionalRoute.getTarget().mediate(synCtx);
                if (conditionalRoute.isBreakRoute()) {
                    break;
                }
            }
        }
    } catch (EvaluatorException ee) {
        handleException("Couldn't evaluate the route condition", ee, synCtx);
    }
    return continueAfter;
}
Also used : HashMap(java.util.HashMap) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with EvaluatorException

use of org.apache.synapse.commons.evaluators.EvaluatorException in project wso2-synapse by wso2.

the class SOAPEnvelopeTextRetriever method getSourceText.

public String getSourceText(EvaluatorContext context) throws EvaluatorException {
    SOAPEnvelope envelope = context.getMessageContext().getEnvelope();
    Object result;
    try {
        if (compiledXPath == null) {
            compiledXPath = new AXIOMXPath(source);
        }
        result = compiledXPath.evaluate(envelope);
    } catch (JaxenException e) {
        throw new EvaluatorException("Error while parsing the XPath expression: " + source, e);
    }
    if (result instanceof List) {
        List list = (List) result;
        if (list.size() == 1 && list.get(0) == null) {
            return null;
        }
        StringBuffer textValue = new StringBuffer();
        for (Object o : list) {
            if (o instanceof OMTextImpl) {
                textValue.append(((OMTextImpl) o).getText());
            } else if (o instanceof OMElementImpl) {
                String s = ((OMElementImpl) o).getText();
                if (s.trim().length() == 0) {
                    s = o.toString();
                }
                textValue.append(s);
            } else if (o instanceof OMDocumentImpl) {
                textValue.append(((OMDocumentImpl) o).getOMDocumentElement().toString());
            } else if (o instanceof OMAttribute) {
                textValue.append(((OMAttribute) o).getAttributeValue());
            }
        }
        return textValue.toString();
    } else {
        return result.toString();
    }
}
Also used : OMElementImpl(org.apache.axiom.om.impl.llom.OMElementImpl) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) JaxenException(org.jaxen.JaxenException) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) List(java.util.List) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) AXIOMXPath(org.apache.axiom.om.xpath.AXIOMXPath) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 3 with EvaluatorException

use of org.apache.synapse.commons.evaluators.EvaluatorException in project wso2-synapse by wso2.

the class URLRewriteMediatorFactory method parseRule.

private RewriteRule parseRule(OMElement ruleElement) {
    Iterator actions = ruleElement.getChildrenWithName(ACTION_Q);
    if (actions == null) {
        handleException("At least one rewrite action is required per rule");
        return null;
    }
    RewriteRule rule = new RewriteRule();
    while (actions.hasNext()) {
        rule.addRewriteAction(parseAction((OMElement) actions.next()));
    }
    OMElement condition = ruleElement.getFirstChildWithName(CONDITION_Q);
    if (condition != null) {
        OMElement child = condition.getFirstElement();
        if (child != null) {
            try {
                Evaluator eval = EvaluatorFactoryFinder.getInstance().getEvaluator(child);
                rule.setCondition(eval);
            } catch (EvaluatorException e) {
                handleException("Error while parsing the rule condition", e);
            }
        }
    }
    return rule;
}
Also used : EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) Evaluator(org.apache.synapse.commons.evaluators.Evaluator) RewriteRule(org.apache.synapse.mediators.transform.url.RewriteRule)

Example 4 with EvaluatorException

use of org.apache.synapse.commons.evaluators.EvaluatorException in project wso2-synapse by wso2.

the class ListenerContextBuilder method createParser.

private Parser createParser(final OMElement definitions) throws AxisFault {
    OMElement conditionsElem = definitions.getFirstChildWithName(new QName(EvaluatorConstants.CONDITIONS));
    if (conditionsElem == null) {
        handleException("Conditions configuration is mandatory for priority based routing");
    }
    assert conditionsElem != null;
    OMAttribute defPriorityAttr = conditionsElem.getAttribute(new QName(EvaluatorConstants.DEFAULT_PRIORITY));
    Parser parser;
    if (defPriorityAttr != null) {
        parser = new Parser(Integer.parseInt(defPriorityAttr.getAttributeValue()));
    } else {
        parser = new Parser();
    }
    try {
        parser.init(conditionsElem);
    } catch (EvaluatorException e) {
        handleException("Invalid " + EvaluatorConstants.CONDITIONS + " configuration for priority based mediation", e);
    }
    return parser;
}
Also used : EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) Parser(org.apache.synapse.commons.evaluators.Parser)

Example 5 with EvaluatorException

use of org.apache.synapse.commons.evaluators.EvaluatorException 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)

Aggregations

EvaluatorException (org.apache.synapse.commons.evaluators.EvaluatorException)10 OMElement (org.apache.axiom.om.OMElement)6 Evaluator (org.apache.synapse.commons.evaluators.Evaluator)3 Iterator (java.util.Iterator)2 QName (javax.xml.namespace.QName)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 EvaluatorContext (org.apache.synapse.commons.evaluators.EvaluatorContext)2 ConditionalRoute (org.apache.synapse.mediators.filters.router.ConditionalRoute)2 ConditionalRouterMediator (org.apache.synapse.mediators.filters.router.ConditionalRouterMediator)2 RewriteRule (org.apache.synapse.mediators.transform.url.RewriteRule)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 OMDocumentImpl (org.apache.axiom.om.impl.llom.OMDocumentImpl)1 OMElementImpl (org.apache.axiom.om.impl.llom.OMElementImpl)1 OMTextImpl (org.apache.axiom.om.impl.llom.OMTextImpl)1 AXIOMXPath (org.apache.axiom.om.xpath.AXIOMXPath)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 Parser (org.apache.synapse.commons.evaluators.Parser)1 EvaluatorSerializer (org.apache.synapse.commons.evaluators.config.EvaluatorSerializer)1