Search in sources :

Example 51 with API

use of org.apache.synapse.api.API 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 52 with API

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

the class APISerializationTest method testAPISerialization2.

public void testAPISerialization2() throws Exception {
    String xml = "<api name=\"test\" context=\"/dictionary\" binds-to=\"\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource binds-to=\"\" url-mapping=\"/admin/view\" " + "inSequence=\"in\" outSequence=\"out\"/></api>";
    OMElement om = AXIOMUtil.stringToOM(xml);
    API api = APIFactory.createAPI(om);
    OMElement out = APISerializer.serializeAPI(api);
    assertXMLEqual(xml, out.toString());
}
Also used : OMElement(org.apache.axiom.om.OMElement) API(org.apache.synapse.api.API)

Example 53 with API

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

the class APISerializationTest method testAPISerialization4.

public void testAPISerialization4() throws Exception {
    String xml = "<api name=\"test\" context=\"/dictionary\" binds-to=\"\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource binds-to=\"\" url-mapping=\"/admin/view\" " + "outSequence=\"out\"><inSequence><log/><send/></inSequence></resource></api>";
    OMElement om = AXIOMUtil.stringToOM(xml);
    API api = APIFactory.createAPI(om);
    OMElement out = APISerializer.serializeAPI(api);
    assertXMLEqual(xml, out.toString());
}
Also used : OMElement(org.apache.axiom.om.OMElement) API(org.apache.synapse.api.API)

Example 54 with API

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

the class APISerializationTest method testAPISerialization5.

public void testAPISerialization5() throws Exception {
    String xml = "<api xmlns=\"http://ws.apache.org/ns/synapse\" name=\"test\" context=\"/dictionary\" " + "hostname=\"apache.org\" port=\"8243\" binds-to=\"\" transports=\"https\">" + "<resource binds-to=\"\" url-mapping=\"/admin/view/*\" " + "><inSequence><log/><send/></inSequence><outSequence><log/><send/></outSequence></resource>" + "<resource binds-to=\"\" url-mapping=\"/admin/*\"><inSequence><log/><send/></inSequence><outSequence>" + "<log/><send/></outSequence></resource><resource binds-to=\"\" uri-template=\"/{char}/{word}\">" + "<inSequence><send/></inSequence><faultSequence><log level=\"full\"/></faultSequence>" + "</resource></api>";
    OMElement om = AXIOMUtil.stringToOM(xml);
    API api = APIFactory.createAPI(om);
    OMElement out = APISerializer.serializeAPI(api);
    assertXMLEqual(xml, out.toString());
}
Also used : OMElement(org.apache.axiom.om.OMElement) API(org.apache.synapse.api.API)

Example 55 with API

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

the class APIFactory method createAPI.

public static API createAPI(OMElement apiElt, Properties properties) {
    OMAttribute nameAtt = apiElt.getAttribute(new QName("name"));
    if (nameAtt == null || "".equals(nameAtt.getAttributeValue())) {
        handleException("Attribute 'name' is required for an API definition");
    }
    OMAttribute contextAtt = apiElt.getAttribute(new QName("context"));
    if (contextAtt == null || "".equals(contextAtt.getAttributeValue())) {
        handleException("Attribute 'context' is required for an API definition");
    }
    API api = new API(nameAtt.getAttributeValue(), contextAtt.getAttributeValue());
    OMAttribute hostAtt = apiElt.getAttribute(new QName("hostname"));
    if (hostAtt != null && !"".equals(hostAtt.getAttributeValue())) {
        api.setHost(hostAtt.getAttributeValue());
    }
    VersionStrategy vStrategy = VersionStrategyFactory.createVersioningStrategy(api, apiElt);
    api.setVersionStrategy(vStrategy);
    OMAttribute portAtt = apiElt.getAttribute(new QName("port"));
    if (portAtt != null && !"".equals(portAtt.getAttributeValue())) {
        api.setPort(Integer.parseInt(portAtt.getAttributeValue()));
    }
    OMAttribute publishSwagger = apiElt.getAttribute(new QName("publishSwagger"));
    if (publishSwagger != null) {
        api.setSwaggerResourcePath(publishSwagger.getAttributeValue());
    }
    InboundApiUtils.addBindsTo(api, apiElt);
    Iterator resources = apiElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "resource"));
    boolean noResources = true;
    while (resources.hasNext()) {
        OMElement resourceElt = (OMElement) resources.next();
        api.addResource(ResourceFactory.createResource(resourceElt, properties));
        noResources = false;
    }
    if (noResources) {
        handleException("An API must contain at least one resource definition");
    }
    OMElement handlersElt = apiElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handlers"));
    if (handlersElt != null) {
        Iterator handlers = handlersElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handler"));
        while (handlers.hasNext()) {
            OMElement handlerElt = (OMElement) handlers.next();
            defineHandler(api, handlerElt);
        }
    }
    OMAttribute trans = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "transports"));
    if (trans != null) {
        String transports = trans.getAttributeValue();
        if (!"".equals(transports)) {
            if (Constants.TRANSPORT_HTTP.equals(transports)) {
                api.setProtocol(RESTConstants.PROTOCOL_HTTP_ONLY);
            } else if (Constants.TRANSPORT_HTTPS.equals(transports)) {
                api.setProtocol(RESTConstants.PROTOCOL_HTTPS_ONLY);
            } else {
                handleException("Invalid protocol name: " + transports);
            }
        }
    }
    String nameString = api.getName();
    if (nameString == null || "".equals(nameString)) {
        nameString = SynapseConstants.ANONYMOUS_API;
    }
    AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
    api.configure(aspectConfiguration);
    OMAttribute statistics = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.STATISTICS_ATTRIB_NAME));
    if (statistics != null) {
        String statisticsValue = statistics.getAttributeValue();
        if (statisticsValue != null) {
            if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
                aspectConfiguration.enableStatistics();
            }
        }
    }
    OMAttribute tracing = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.TRACE_ATTRIB_NAME));
    if (tracing != null) {
        String tracingValue = tracing.getAttributeValue();
        if (tracingValue != null) {
            if (XMLConfigConstants.TRACE_ENABLE.equals(tracingValue)) {
                aspectConfiguration.enableTracing();
            }
        }
    }
    CommentListUtil.populateComments(apiElt, api.getCommentsList());
    return api;
}
Also used : QName(javax.xml.namespace.QName) VersionStrategy(org.apache.synapse.api.version.VersionStrategy) Iterator(java.util.Iterator) API(org.apache.synapse.api.API) OMElement(org.apache.axiom.om.OMElement) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

API (org.apache.synapse.api.API)64 MessageContext (org.apache.synapse.MessageContext)33 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)33 Resource (org.apache.synapse.api.Resource)23 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)14 OMElement (org.apache.axiom.om.OMElement)10 URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)7 Test (org.junit.Test)6 URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)5 Cache (javax.cache.Cache)4 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 SynapseException (org.apache.synapse.SynapseException)4 ContextVersionStrategy (org.apache.synapse.api.version.ContextVersionStrategy)4 ProxyService (org.apache.synapse.core.axis2.ProxyService)4 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)4 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)4 CacheProvider (org.wso2.carbon.apimgt.impl.caching.CacheProvider)4 ArrayList (java.util.ArrayList)3