Search in sources :

Example 1 with URITemplateHelper

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

the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch2.

public void testBasicTemplateDispatch2() throws Exception {
    API api = new API("TestAPI", "/");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
    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, "/dictionary/c/cat", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, 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/dog/", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "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(PROP_VALUE, 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.api.dispatch.URITemplateHelper) 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 URITemplateHelper

use of org.apache.synapse.api.dispatch.URITemplateHelper 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.api.dispatch.URITemplateHelper) 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 URITemplateHelper

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

the class URITemplateBasedDispatcherTest method testDefaultDispatch.

public void testDefaultDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/"));
    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 : URITemplateHelper(org.apache.synapse.api.dispatch.URITemplateHelper) 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 URITemplateHelper

use of org.apache.synapse.api.dispatch.URITemplateHelper 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 URITemplateHelper

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

the class ResourceTest method testFaultSequence.

public void testFaultSequence() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
    SequenceMediator inSequence = getTestSequence("seq.in", "seq.in.value");
    ((PropertyMediator) inSequence.getChild(0)).setScope("axis2");
    XSLTMediator xsltMediator = new XSLTMediator();
    xsltMediator.setXsltKey(new Value("/bogus/key"));
    inSequence.addChild(xsltMediator);
    resource.setInSequence(inSequence);
    SequenceMediator faultSequence = getTestSequence("seq.fault", "seq.fault.value");
    ((PropertyMediator) faultSequence.getChild(0)).setScope("axis2");
    resource.setFaultSequence(faultSequence);
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    synapseConfig.addSequence("main", getTestSequence("main.in", "main.value"));
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
    MessageContextCreatorForAxis2.setSynConfig(synapseConfig);
    MessageContextCreatorForAxis2.setSynEnv(synCtx.getEnvironment());
    org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    mc.setConfigurationContext(((Axis2SynapseEnvironment) synCtx.getEnvironment()).getAxis2ConfigurationContext());
    new SynapseMessageReceiver().receive(mc);
    assertEquals("seq.in.value", mc.getProperty("seq.in"));
    assertEquals("seq.fault.value", mc.getProperty("seq.fault"));
}
Also used : Resource(org.apache.synapse.api.Resource) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URITemplateHelper(org.apache.synapse.api.dispatch.URITemplateHelper) XSLTMediator(org.apache.synapse.mediators.transform.XSLTMediator) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) Value(org.apache.synapse.mediators.Value) API(org.apache.synapse.api.API) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseMessageReceiver(org.apache.synapse.core.axis2.SynapseMessageReceiver) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)7 MessageContext (org.apache.synapse.MessageContext)5 API (org.apache.synapse.api.API)5 Resource (org.apache.synapse.api.Resource)5 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)5 URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)2 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 DispatcherHelper (org.apache.synapse.api.dispatch.DispatcherHelper)1 SequenceMediatorSerializer (org.apache.synapse.config.xml.SequenceMediatorSerializer)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 SynapseMessageReceiver (org.apache.synapse.core.axis2.SynapseMessageReceiver)1 Value (org.apache.synapse.mediators.Value)1 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)1 PropertyMediator (org.apache.synapse.mediators.builtin.PropertyMediator)1 XSLTMediator (org.apache.synapse.mediators.transform.XSLTMediator)1