Search in sources :

Example 11 with API

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

the class APIDeployer method restoreSynapseArtifact.

@Override
public void restoreSynapseArtifact(String artifactName) {
    if (log.isDebugEnabled()) {
        log.debug("Restoring the API with name : " + artifactName + " : Started");
    }
    try {
        API api = getSynapseConfiguration().getAPI(artifactName);
        if (api != null) {
            api.setLogSetterValue();
        }
        OMElement apiElement = APISerializer.serializeAPI(api);
        if (api.getFileName() != null) {
            String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.REST_API_DIR + File.separator + api.getFileName();
            writeToFile(apiElement, fileName);
            if (log.isDebugEnabled()) {
                log.debug("Restoring the API with name : " + artifactName + " : Completed");
            }
            log.info("API named '" + artifactName + "' has been restored");
        } else {
            handleSynapseArtifactDeploymentError("Couldn't restore the API named '" + artifactName + "', filename cannot be found");
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("Restoring of the API named '" + artifactName + "' has failed", e);
    }
}
Also used : API(org.apache.synapse.api.API) OMElement(org.apache.axiom.om.OMElement) DeploymentException(org.apache.axis2.deployment.DeploymentException)

Example 12 with API

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

the class APIDispatcherTest method testAPIContextVersionBasedDispatchWithNoVersion.

public void testAPIContextVersionBasedDispatchWithNoVersion() throws Exception {
    API api = new API(TEST_API, "/test");
    api.setVersionStrategy(new ContextVersionStrategy(api, TEST_API_VERSION, null));
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Message with '/test' context & URL as a Query Parameter should be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test?a=http://localhost.com", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
Also used : ContextVersionStrategy(org.apache.synapse.api.version.ContextVersionStrategy) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 13 with API

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

the class APIDispatcherTest method testGeneralAPIDispatch.

public void testGeneralAPIDispatch() throws Exception {
    API api = new API(TEST_API, "/");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
}
Also used : API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 14 with API

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

the class SynapseConfigurationTest method testAPITableOrderWithAddAPIwithoutReOrder.

public void testAPITableOrderWithAddAPIwithoutReOrder() {
    SynapseConfiguration config = new SynapseConfiguration();
    API api1 = new API("API1", "/context/test");
    API api2 = new API("API2", "/context");
    API api3 = new API("API3", "/context/test/ctx");
    config.addAPI("API1", api1, false);
    config.addAPI("API2", api2, false);
    config.addAPI("API3", api3, false);
    // this should get the api table as it is.
    Collection<API> apis = config.getAPIs();
    API[] apisArray = apis.toArray(new API[apis.size()]);
    // api1 with context /context/test/ctx should be first in the list
    assertEquals("Order is not correct before sorting", api1, apisArray[0]);
    // calling explicitly to order the apitable
    config.reconstructAPITable();
    apis = config.getAPIs();
    API[] apisArray2 = apis.toArray(new API[apis.size()]);
    // api3 with context /context/test/ctx should be first in the list
    assertEquals("Order is not correct", api3, apisArray2[0]);
}
Also used : API(org.apache.synapse.api.API)

Example 15 with API

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

the class StatisticSynapseConfigurationObserverTest method testApiUpdated.

/**
 * Test apiUpdated method by hash generation.
 */
@Test
public void testApiUpdated() {
    final String apiName = "testName";
    final String apiContext = "/testContext";
    API api = new API(apiName, apiContext);
    observer.apiUpdated(api);
    Assert.assertNotNull("New hash must be set by the method", api.getAspectConfiguration().getHashCode());
}
Also used : API(org.apache.synapse.api.API) 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