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"));
}
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"));
}
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));
}
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()));
}
}
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"));
}
Aggregations