Search in sources :

Example 1 with EvaluatorContext

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

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

the class SourceTextRetrieverTest method testHeaderTextRetriever.

public void testHeaderTextRetriever() throws EvaluatorException {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("key1", "value1");
    headers.put("key2", "value2");
    EvaluatorContext context = new EvaluatorContext(null, headers);
    HeaderTextRetriever txtRtvr = new HeaderTextRetriever("key1");
    assertEquals(headers.get("key1"), txtRtvr.getSourceText(context));
    txtRtvr = new HeaderTextRetriever("bogusKey");
    assertNull(txtRtvr.getSourceText(context));
}
Also used : HashMap(java.util.HashMap) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext)

Example 3 with EvaluatorContext

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

the class SourceTextRetrieverTest method testURLTextRetriever.

public void testURLTextRetriever() throws EvaluatorException {
    try {
        URI uri = new URI("http://host:9000/path");
        EvaluatorContext context = new EvaluatorContext(uri.toString(), null);
        URLTextRetriever txtRtvr = new URLTextRetriever();
        assertEquals(uri.toString(), txtRtvr.getSourceText(context));
        txtRtvr.setSource(EvaluatorConstants.URI_FRAGMENTS.host.name());
        assertEquals(uri.getHost(), txtRtvr.getSourceText(context));
        txtRtvr.setSource(EvaluatorConstants.URI_FRAGMENTS.port.name());
        assertEquals(String.valueOf(uri.getPort()), txtRtvr.getSourceText(context));
        txtRtvr.setSource(EvaluatorConstants.URI_FRAGMENTS.protocol.name());
        assertEquals(uri.getScheme(), txtRtvr.getSourceText(context));
        txtRtvr.setSource(EvaluatorConstants.URI_FRAGMENTS.path.name());
        assertEquals(uri.getPath(), txtRtvr.getSourceText(context));
    } catch (URISyntaxException ignore) {
    }
}
Also used : EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 4 with EvaluatorContext

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

the class SourceTextRetrieverTest method testPropertyTextRetriver.

public void testPropertyTextRetriver() throws EvaluatorException {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("key1", "value1");
    props.put("key2", "value2");
    EvaluatorContext context = new EvaluatorContext(null, null);
    context.setProperties(props);
    PropertyTextRetriever txtRtvr = new PropertyTextRetriever("key1");
    assertEquals(props.get("key1"), txtRtvr.getSourceText(context));
    txtRtvr = new PropertyTextRetriever("key2");
    assertEquals(props.get("key2"), txtRtvr.getSourceText(context));
    txtRtvr = new PropertyTextRetriever("bogusKey");
    assertNull(txtRtvr.getSourceText(context));
}
Also used : HashMap(java.util.HashMap) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext)

Example 5 with EvaluatorContext

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

the class SourceTextRetrieverTest method testParameterTextRetriever.

public void testParameterTextRetriever() throws EvaluatorException {
    String url = "http://host:9000/path?key1=value1&key2=value2";
    EvaluatorContext context = new EvaluatorContext(url, null);
    ParameterTextRetriever txtRtvr = new ParameterTextRetriever("key1");
    assertEquals("value1", txtRtvr.getSourceText(context));
    txtRtvr = new ParameterTextRetriever("key2");
    assertEquals("value2", txtRtvr.getSourceText(context));
    txtRtvr = new ParameterTextRetriever("bogusKey");
    assertNull(txtRtvr.getSourceText(context));
}
Also used : EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext)

Aggregations

EvaluatorContext (org.apache.synapse.commons.evaluators.EvaluatorContext)8 HashMap (java.util.HashMap)4 Map (java.util.Map)2 EvaluatorException (org.apache.synapse.commons.evaluators.EvaluatorException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 OMElement (org.apache.axiom.om.OMElement)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPFactory (org.apache.axiom.soap.SOAPFactory)1 MessageContext (org.apache.axis2.context.MessageContext)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)1 ContentInputStream (org.apache.http.nio.entity.ContentInputStream)1 ContentOutputStream (org.apache.http.nio.entity.ContentOutputStream)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 ServerConnectionDebug (org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug)1