Search in sources :

Example 6 with URITemplateHelper

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

the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch1.

public void testBasicTemplateDispatch1() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
    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", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("foo", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "user"));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/~foo/bar", "GET");
    handler.process(synCtx);
    assertNull(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 7 with URITemplateHelper

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

the class ResourceSerializer method serializeResource.

public static OMElement serializeResource(Resource resource) {
    OMElement resourceElt = fac.createOMElement("resource", SynapseConstants.SYNAPSE_OMNAMESPACE);
    String[] methods = resource.getMethods();
    if (methods.length > 0) {
        String value = "";
        for (String method : methods) {
            value += method + " ";
        }
        resourceElt.addAttribute("methods", value.trim(), null);
    }
    if (resource.getBindsTo() != null) {
        resourceElt.addAttribute(ApiConstants.BINDS_TO, String.join(",", resource.getBindsTo()), null);
    }
    if (resource.getContentType() != null) {
        resourceElt.addAttribute("content-type", resource.getContentType(), null);
    }
    if (resource.getUserAgent() != null) {
        resourceElt.addAttribute("user-agent", resource.getUserAgent(), null);
    }
    if (resource.getProtocol() == RESTConstants.PROTOCOL_HTTP_ONLY) {
        resourceElt.addAttribute("protocol", "http", null);
    } else if (resource.getProtocol() == RESTConstants.PROTOCOL_HTTPS_ONLY) {
        resourceElt.addAttribute("protocol", "https", null);
    }
    DispatcherHelper helper = resource.getDispatcherHelper();
    if (helper != null) {
        if (helper instanceof URLMappingHelper) {
            resourceElt.addAttribute("url-mapping", helper.getString(), null);
        } else if (helper instanceof URITemplateHelper) {
            resourceElt.addAttribute("uri-template", helper.getString(), null);
        }
    }
    SequenceMediatorSerializer seqSerializer = new SequenceMediatorSerializer();
    if (resource.getInSequenceKey() != null) {
        resourceElt.addAttribute("inSequence", resource.getInSequenceKey(), null);
    } else if (resource.getInSequence() != null) {
        OMElement inSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getInSequence());
        inSeqElement.setLocalName("inSequence");
        resourceElt.addChild(inSeqElement);
    }
    if (resource.getOutSequenceKey() != null) {
        resourceElt.addAttribute("outSequence", resource.getOutSequenceKey(), null);
    } else if (resource.getOutSequence() != null) {
        OMElement outSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getOutSequence());
        outSeqElement.setLocalName("outSequence");
        resourceElt.addChild(outSeqElement);
    }
    if (resource.getFaultSequenceKey() != null) {
        resourceElt.addAttribute("faultSequence", resource.getFaultSequenceKey(), null);
    } else if (resource.getFaultSequence() != null) {
        OMElement faultSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getFaultSequence());
        faultSeqElement.setLocalName("faultSequence");
        resourceElt.addChild(faultSeqElement);
    }
    return resourceElt;
}
Also used : URITemplateHelper(org.apache.synapse.api.dispatch.URITemplateHelper) DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper) URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) OMElement(org.apache.axiom.om.OMElement) SequenceMediatorSerializer(org.apache.synapse.config.xml.SequenceMediatorSerializer)

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