Search in sources :

Example 21 with Resource

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

Example 22 with Resource

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

the class ResourceTest method testQueryParams.

public void testQueryParams() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=5&b=10&user=bar", "GET");
    handler.process(synCtx);
    assertEquals("5", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
    assertEquals("10", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "b"));
    assertEquals("bar", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "user"));
    synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=5", "GET");
    handler.process(synCtx);
    assertEquals("5", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
    synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=Hello%20World&b=10&c=/foo/bar", "GET");
    handler.process(synCtx);
    assertEquals("Hello World", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
    assertEquals("10", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "b"));
    assertEquals("/foo/bar", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "c"));
}
Also used : Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 23 with Resource

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

the class ResourceTest method testQueryParamWithURL.

public void testQueryParamWithURL() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=http://test.com", "GET");
    handler.process(synCtx);
    // verify query parameters with URLs as values
    assertEquals("http://test.com", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
    synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=http://test.com&b=10&c=/foo/bar", "GET");
    handler.process(synCtx);
    // verify query parameters with URLs as values
    assertEquals("http://test.com", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
    assertEquals("10", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "b"));
    assertEquals("/foo/bar", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "c"));
}
Also used : Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 24 with Resource

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

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

the class APISerializer method serializeAPI.

public static OMElement serializeAPI(API api) {
    OMElement apiElt = fac.createOMElement("api", SynapseConstants.SYNAPSE_OMNAMESPACE);
    apiElt.addAttribute("name", api.getAPIName(), null);
    apiElt.addAttribute("context", api.getContext(), null);
    VersionStrategySerializer.serializeVersioningStrategy(api.getVersionStrategy(), apiElt);
    if (api.getHost() != null) {
        apiElt.addAttribute("hostname", api.getHost(), null);
    }
    if (api.getPort() != -1) {
        apiElt.addAttribute("port", String.valueOf(api.getPort()), null);
    }
    if (api.getSwaggerResourcePath() != null) {
        apiElt.addAttribute("publishSwagger", api.getSwaggerResourcePath(), null);
    }
    if (api.getBindsTo() != null) {
        apiElt.addAttribute(ApiConstants.BINDS_TO, String.join(",", api.getBindsTo()), null);
    }
    StatisticsConfigurable statisticsConfigurable = api.getAspectConfiguration();
    if (statisticsConfigurable != null && statisticsConfigurable.isStatisticsEnable()) {
        apiElt.addAttribute(XMLConfigConstants.STATISTICS_ATTRIB_NAME, XMLConfigConstants.STATISTICS_ENABLE, null);
    }
    if (statisticsConfigurable != null && statisticsConfigurable.isTracingEnabled()) {
        apiElt.addAttribute(XMLConfigConstants.TRACE_ATTRIB_NAME, XMLConfigConstants.TRACE_ENABLE, null);
    }
    Resource[] resources = api.getResources();
    for (Resource r : resources) {
        OMElement resourceElt = ResourceSerializer.serializeResource(r);
        apiElt.addChild(resourceElt);
    }
    Handler[] handlers = api.getHandlers();
    if (handlers.length > 0) {
        OMElement handlersElt = fac.createOMElement("handlers", SynapseConstants.SYNAPSE_OMNAMESPACE);
        for (Handler handler : handlers) {
            OMElement handlerElt = fac.createOMElement("handler", SynapseConstants.SYNAPSE_OMNAMESPACE);
            handlerElt.addAttribute("class", handler.getClass().getName(), null);
            handlersElt.addChild(handlerElt);
            if (handler.getProperties() != null) {
                Map propertyMap = handler.getProperties();
                if (propertyMap != null) {
                    Iterator itr = propertyMap.keySet().iterator();
                    while (itr.hasNext()) {
                        String propName = (String) itr.next();
                        Object o = handler.getProperties().get(propName);
                        OMElement prop = fac.createOMElement(APIFactory.PROP_Q, handlerElt);
                        prop.addAttribute(fac.createOMAttribute(APIFactory.ATT_NAME.getLocalPart(), nullNS, propName));
                        if (o instanceof String) {
                            prop.addAttribute(fac.createOMAttribute(APIFactory.ATT_VALUE.getLocalPart(), nullNS, (String) o));
                        } else {
                            prop.addChild((OMNode) o);
                        }
                        handlerElt.addChild(prop);
                    }
                }
            }
        }
        apiElt.addChild(handlersElt);
    }
    if (api.getProtocol() == RESTConstants.PROTOCOL_HTTP_ONLY) {
        apiElt.addAttribute("transports", Constants.TRANSPORT_HTTP, null);
    } else if (api.getProtocol() == RESTConstants.PROTOCOL_HTTPS_ONLY) {
        apiElt.addAttribute("transports", Constants.TRANSPORT_HTTPS, null);
    }
    CommentListUtil.serializeComments(apiElt, api.getCommentsList());
    return apiElt;
}
Also used : Resource(org.apache.synapse.api.Resource) Iterator(java.util.Iterator) Handler(org.apache.synapse.rest.Handler) StatisticsConfigurable(org.apache.synapse.aspects.statistics.StatisticsConfigurable) Map(java.util.Map)

Aggregations

Resource (org.apache.synapse.api.Resource)29 API (org.apache.synapse.api.API)23 MessageContext (org.apache.synapse.MessageContext)19 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)17 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)10 URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)7 URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Mediator (org.apache.synapse.Mediator)4 RESTDispatcher (org.apache.synapse.api.dispatch.RESTDispatcher)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Cache (javax.cache.Cache)3 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)3 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)3 CacheProvider (org.wso2.carbon.apimgt.impl.caching.CacheProvider)3 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)3