Search in sources :

Example 26 with API

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

the class URLMappingBasedDispatcherTest method testExtensionBasedDispatch.

public void testExtensionBasedDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("*.jsp"));
    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/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/welcome.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp?a=5&b=10", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 27 with API

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

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

the class ContinuationStackManager method pushRootFaultHandlerForSequence.

/**
 * Find the correct root fault handler for named sequences.
 *
 * If the message is initiated from a proxy, we need to assign the proxy fault sequence.
 * If the message is initiated from a API Resource, we need to assign the resource fault sequence.
 *
 * @param synCtx message context
 */
private static void pushRootFaultHandlerForSequence(MessageContext synCtx) {
    // For Proxy services
    String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
    if (proxyName != null && !"".equals(proxyName)) {
        ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
        if (proxyService != null) {
            proxyService.registerFaultHandler(synCtx);
        } else {
            handleException("Proxy service : " + proxyName + " not found");
        }
        return;
    }
    // For APIs
    String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
    if (apiName != null && !"".equals(apiName)) {
        API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
        if (api != null) {
            String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
            Resource resource = api.getResource(resourceName);
            if (resource != null) {
                resource.registerFaultHandler(synCtx);
            } else {
                handleException("Resource : " + resourceName + " not found");
            }
        } else {
            handleException("REST API : " + apiName + " not found");
        }
        return;
    }
    // For Inbound Endpoints
    if (synCtx.getProperty(SynapseConstants.IS_INBOUND) != null) {
        String inboundEndpointName = (String) synCtx.getProperty(SynapseConstants.INBOUND_ENDPOINT_NAME);
        if (inboundEndpointName != null) {
            InboundEndpoint inboundEndpoint = synCtx.getConfiguration().getInboundEndpoint(inboundEndpointName);
            if (inboundEndpoint != null) {
                String errorSeqName = inboundEndpoint.getOnErrorSeq();
                if (errorSeqName != null) {
                    SequenceMediator errorSeq = (SequenceMediator) synCtx.getConfiguration().getSequence(errorSeqName);
                    if (errorSeq != null) {
                        synCtx.pushFaultHandler(new MediatorFaultHandler(errorSeq));
                        return;
                    } else {
                        handleException("Sequence Mediator : " + errorSeqName + "not found");
                    }
                }
            } else {
                handleException("Inbound Endpoint : " + inboundEndpointName + "not found");
            }
        }
    }
    // For main sequence/MessageInjector etc, push the default fault handler
    synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
}
Also used : InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) ProxyService(org.apache.synapse.core.axis2.ProxyService) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 29 with API

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

the class APIDebugUtil method registerAPISequenceMediationFlowBreakPoint.

/**
 * Registers/Un-registers a breakpoint, point where mediation flow get suspended
 *
 * @param synCfg Synapse configuration
 * @param mapping either resource url-mapping or uri-template
 * @param method resource http method
 * @param sequenceType Synapse sequence type
 * @param apiKey name of the API
 * @param position array of integers that uniquely specifies a point in mediation route
 * @param registerMode specify whether register or un register
 */
public static void registerAPISequenceMediationFlowBreakPoint(SynapseConfiguration synCfg, String mapping, String method, String sequenceType, String apiKey, int[] position, boolean registerMode) {
    SynapseSequenceType synapseSequenceType = SynapseSequenceType.valueOf(sequenceType.toUpperCase());
    APIMediationFlowPoint breakPoint = new APIMediationFlowPoint();
    breakPoint.setSynapseMediationComponent(SynapseMediationComponent.SEQUENCE);
    breakPoint.setKey(apiKey);
    breakPoint.setMediatorPosition(position);
    breakPoint.setSynapseSequenceType(synapseSequenceType);
    breakPoint.setSequenceBaseType(SynapseDebugCommandConstants.DEBUG_COMMAND_MEDIATION_COMPONENT_SEQUENCE_API);
    breakPoint.setResourceMapping(mapping);
    breakPoint.setResourceHTTPMethod(method);
    Mediator seqMediator = null;
    Resource api_resource = null;
    API api = synCfg.getAPI(apiKey);
    if (api == null) {
        if (log.isDebugEnabled()) {
            log.debug("Non existing API for the key " + breakPoint.getKey());
        }
        debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_API_NOT_FOUND).toString());
        return;
    }
    Resource[] resource_array = api.getResources();
    for (int counter = 0; counter < resource_array.length; counter++) {
        if (resource_array[counter].getDispatcherHelper() != null && mapping != null) {
            if (mapping.equals(resource_array[counter].getDispatcherHelper().getString())) {
                for (String m1 : resource_array[counter].getMethods()) {
                    if (m1.equals(method)) {
                        api_resource = resource_array[counter];
                        break;
                    }
                }
                if (api_resource != null) {
                    break;
                }
            }
        } else if (resource_array[counter].getDispatcherHelper() == null && mapping == null) {
            for (String m1 : resource_array[counter].getMethods()) {
                if (m1.equals(method)) {
                    api_resource = resource_array[counter];
                    break;
                }
            }
            if (api_resource != null) {
                break;
            }
        }
    }
    if (api_resource != null) {
        if (synapseSequenceType.equals(SynapseSequenceType.API_INSEQ)) {
            seqMediator = api_resource.getInSequence();
        } else if (synapseSequenceType.equals(SynapseSequenceType.API_OUTSEQ)) {
            seqMediator = api_resource.getOutSequence();
        } else if (synapseSequenceType.equals(SynapseSequenceType.API_FAULTSEQ)) {
            seqMediator = api_resource.getFaultSequence();
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Resource not found for the API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
        }
        debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_API_RESOURCE_NOT_FOUND).toString());
        return;
    }
    if (seqMediator != null) {
        Mediator current_mediator = null;
        current_mediator = MediatorTreeTraverseUtil.getMediatorReference(synCfg, seqMediator, position);
        if (current_mediator != null) {
            breakPoint.setMediatorReference(current_mediator);
            if (registerMode) {
                if (!((AbstractMediator) current_mediator).isBreakPoint()) {
                    ((AbstractMediator) current_mediator).setBreakPoint(true);
                    ((AbstractMediator) current_mediator).registerMediationFlowPoint(breakPoint);
                    if (log.isDebugEnabled()) {
                        log.info("Registered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed register breakpoint. Already breakpoint enabled at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_ENABLED).toString());
                }
            } else {
                if (((AbstractMediator) current_mediator).isBreakPoint()) {
                    ((AbstractMediator) current_mediator).unregisterMediationFlowPoint();
                    ((AbstractMediator) current_mediator).setBreakPoint(false);
                    if (log.isDebugEnabled()) {
                        log.debug("Unregistered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed unregister breakpoint. Already breakpoint disabled at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_DISABLED).toString());
                }
            }
        } else {
            if (registerMode) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed register breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                }
                debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Failed unregister breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
                }
                debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
            }
        }
    } else {
        if (registerMode) {
            if (log.isDebugEnabled()) {
                log.debug("Failed register breakpoint. Non existing sequence " + breakPoint.getSynapseSequenceType().toString() + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
            }
            debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Failed unregister breakpoint. Non existing sequence " + breakPoint.getSynapseSequenceType().toString() + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
            }
            debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
        }
    }
}
Also used : SynapseSequenceType(org.apache.synapse.debug.constructs.SynapseSequenceType) APIMediationFlowPoint(org.apache.synapse.debug.constructs.APIMediationFlowPoint) Resource(org.apache.synapse.api.Resource) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) API(org.apache.synapse.api.API) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) APIMediationFlowPoint(org.apache.synapse.debug.constructs.APIMediationFlowPoint) SynapseMediationFlowPoint(org.apache.synapse.debug.constructs.SynapseMediationFlowPoint)

Example 30 with API

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

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration11.

/**
 * Test serializeConfigurationMethod with API added for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration11() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    API api = new API("testAPI", "/");
    synapseConfiguration.addAPI(api.getName(), api);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("testAPI added is not serialized.", element.getChildren().next().toString().contains("name=\"testAPI\""));
}
Also used : API(org.apache.synapse.api.API) OMElement(org.apache.axiom.om.OMElement) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

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