Search in sources :

Example 1 with URLMappingHelper

use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testDefaultResourceDispatch.

public void testDefaultResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 2 with URLMappingHelper

use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testPathBasedDispatch.

public void testPathBasedDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/baz?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars?a=b", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 3 with URLMappingHelper

use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testExtensionBasedDispatch.

public void testExtensionBasedDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("*.jsp"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/welcome.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp?a=5&b=10", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 4 with URLMappingHelper

use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.

the class ResourceFactory method configureURLMappings.

private static void configureURLMappings(Resource resource, OMElement resourceElt) {
    OMAttribute urlMappingAtt = resourceElt.getAttribute(new QName("url-mapping"));
    OMAttribute uriTemplateAtt = resourceElt.getAttribute(new QName("uri-template"));
    if (urlMappingAtt != null && !"".equals(urlMappingAtt.getAttributeValue())) {
        resource.setDispatcherHelper(new URLMappingHelper(urlMappingAtt.getAttributeValue()));
    } else if (uriTemplateAtt != null && !"".equals(uriTemplateAtt.getAttributeValue())) {
        resource.setDispatcherHelper(new URITemplateHelper(uriTemplateAtt.getAttributeValue()));
    }
}
Also used : URITemplateHelper(org.apache.synapse.api.dispatch.URITemplateHelper) QName(javax.xml.namespace.QName) URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 5 with URLMappingHelper

use of org.apache.synapse.api.dispatch.URLMappingHelper in project carbon-apimgt by wso2.

the class CORSRequestHandlerTestCase method testHandleRequest.

@Test
public void testHandleRequest() throws Exception {
    PowerMockito.mockStatic(Utils.class);
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
    Map<String, String> headers = new HashMap<>();
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/ishara");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin-AT-wso2.com--PizzaShackAPI");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
    SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
    API api = Mockito.mock(API.class);
    Mockito.when(api.getContext()).thenReturn("/ishara");
    Resource resource = Mockito.mock(Resource.class);
    Resource[] resources = new Resource[1];
    resources[0] = resource;
    Mockito.when(api.getResources()).thenReturn(resources);
    VersionStrategy versionStrategy = Mockito.mock(VersionStrategy.class);
    Mockito.when(versionStrategy.getVersionType()).thenReturn("url");
    Mockito.when(versionStrategy.getVersion()).thenReturn("1.0");
    Mockito.when(api.getVersionStrategy()).thenReturn(versionStrategy);
    Mockito.when(Utils.getSelectedAPI(messageContext)).thenReturn(api);
    Mockito.when(synapseConfiguration.getAPI("admin-AT-wso2.com--PizzaShackAPI")).thenReturn(api);
    Mockito.when(messageContext.getConfiguration()).thenReturn(synapseConfiguration);
    CORSRequestHandler corsRequestHandler = createCORSRequestHandler();
    corsRequestHandler.init(synapseEnvironment);
    // test ResourceNotFound path
    Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
    // test for resource that is found
    String[] methods = { "GET", "POST" };
    Mockito.when(resource.getMethods()).thenReturn(methods);
    DispatcherHelper dispatcherHelper = new URLMappingHelper("/ishara/1.0") {

        @Override
        public String getString() {
            return "/xx";
        }
    };
    Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper);
    Mockito.when(messageContext.getProperty("REST_SUB_REQUEST_PATH")).thenReturn("/ishara/1.0");
    TreeMap transportHeaders = new TreeMap();
    transportHeaders.put("Origin", "");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
    Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
    DispatcherHelper dispatcherHelper1 = new URLMappingHelper("/ishara/1.0") {

        @Override
        public String getString() {
            return "/xx";
        }
    };
    Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper1);
    Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("OPTIONS");
    // test for OPTIONS request when OPTIONS is not supported by SupportedHTTPVerbs
    Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
    // test for OPTIONS request when OPTIONS is supported by SupportedHTTPVerbs
    String[] methodsWithOptions = { "GET", "POST", "OPTIONS" };
    Mockito.when(resource.getMethods()).thenReturn(methodsWithOptions);
    Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
}
Also used : HashMap(java.util.HashMap) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper) URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) TreeMap(java.util.TreeMap) VersionStrategy(org.apache.synapse.api.version.VersionStrategy) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)9 MessageContext (org.apache.synapse.MessageContext)7 API (org.apache.synapse.api.API)7 Resource (org.apache.synapse.api.Resource)7 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)7 DispatcherHelper (org.apache.synapse.api.dispatch.DispatcherHelper)2 URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 VersionStrategy (org.apache.synapse.api.version.VersionStrategy)1 SequenceMediatorSerializer (org.apache.synapse.config.xml.SequenceMediatorSerializer)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1