Search in sources :

Example 61 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class APIDispatcherTest method testAPIContextVersionBasedDispatchWithNoVersion.

public void testAPIContextVersionBasedDispatchWithNoVersion() throws Exception {
    API api = new API(TEST_API, "/test");
    api.setVersionStrategy(new ContextVersionStrategy(api, TEST_API_VERSION, null));
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Message with '/test' context & URL as a Query Parameter should be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test?a=http://localhost.com", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
Also used : ContextVersionStrategy(org.apache.synapse.rest.version.ContextVersionStrategy) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 62 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class BasicAPIMediationTest method testRestURLPostfix2.

public void testRestURLPostfix2() throws Exception {
    API api = new API(TEST_API, "/");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test");
    synCtx = getMessageContext(synapseConfig, false, "/test/me/now", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test/me/now");
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test?a=5");
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 63 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class RESTMediationTestCase method getMessageContext.

protected MessageContext getMessageContext(SynapseConfiguration synapseConfig, boolean https, String url, String method) throws Exception {
    MessageContext synCtx = TestUtils.createSynapseMessageContext("<foo/>", synapseConfig);
    org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    if (https) {
        msgCtx.setIncomingTransportName("https");
    } else {
        msgCtx.setIncomingTransportName("http");
    }
    msgCtx.setProperty(Constants.Configuration.HTTP_METHOD, method);
    msgCtx.setProperty(Constants.Configuration.TRANSPORT_IN_URL, url);
    msgCtx.setProperty(NhttpConstants.REST_URL_POSTFIX, url.substring(1));
    return synCtx;
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 64 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class RestUtilsTest method testGetFullRequestPath.

public void testGetFullRequestPath() throws Exception {
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    MessageContext msgCtx = getMessageContext(synapseConfig, false, "http://localhos:9443/test/admin?PARAM1=1&PARAM2=2", "GET");
    String url = RESTUtils.getFullRequestPath(msgCtx);
    Assert.assertTrue(url.contains("PARAM1=1&PARAM2=2"));
}
Also used : MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 65 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class URITemplateBasedDispatcherTest method testMultipleResourceDispatch.

public void testMultipleResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/");
    Resource resource1 = new Resource();
    resource1.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
    resource1.setInSequence(getTestSequence(PROP_NAME, "r1"));
    api.addResource(resource1);
    Resource resource2 = new Resource();
    resource2.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}"));
    resource2.setInSequence(getTestSequence(PROP_NAME, "r2"));
    api.addResource(resource2);
    Resource resource3 = new Resource();
    resource3.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}{#ref}"));
    resource3.setInSequence(getTestSequence(PROP_NAME, "r3"));
    api.addResource(resource3);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
    handler.process(synCtx);
    assertEquals("r1", synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/d", "GET");
    handler.process(synCtx);
    assertEquals("r2", synCtx.getProperty(PROP_NAME));
    assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/e#test", "GET");
    handler.process(synCtx);
    assertEquals("r3", synCtx.getProperty(PROP_NAME));
    assertEquals("e", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("test", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "ref"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat/test", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat#ref", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    /* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
    handler.process(synCtx);
    assertEquals("r1", synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
Also used : URITemplateHelper(org.apache.synapse.rest.dispatch.URITemplateHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Aggregations

MessageContext (org.apache.synapse.MessageContext)220 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)86 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)54 SynapseException (org.apache.synapse.SynapseException)29 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)26 ArrayList (java.util.ArrayList)24 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)24 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)18 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 OMElement (org.apache.axiom.om.OMElement)17 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)17 HashMap (java.util.HashMap)16 Mediator (org.apache.synapse.Mediator)16 TestMessageContext (org.apache.synapse.TestMessageContext)16 Map (java.util.Map)15 Properties (java.util.Properties)15 Test (org.junit.Test)15 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)14 QName (javax.xml.namespace.QName)13