Search in sources :

Example 16 with API

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

the class MultiXMLConfigurationSerializer method serializeSynapseXML.

/**
 * Serialize only the elements defined in the top level synapse.xml file back to the
 * synapse.xml file. This method ignores the elements defined in files other than the
 * synapse.xml. Can be used in situations where only the synapse.xml file should be
 * updated at runtime.
 *
 * @param synapseConfig Current Synapse configuration
 * @throws Exception on file I/O error
 */
public void serializeSynapseXML(SynapseConfiguration synapseConfig) throws Exception {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMElement definitions = fac.createOMElement("definitions", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
    if (synapseConfig.getRegistry() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_REGISTRY_DEFINITION))) {
        RegistrySerializer.serializeRegistry(definitions, synapseConfig.getRegistry());
    }
    if (synapseConfig.getTaskManager() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_TASK_MANAGER_DEFINITION))) {
        TaskManagerSerializer.serializetaskManager(definitions, synapseConfig.getTaskManager());
    }
    Collection<ProxyService> proxyServices = synapseConfig.getProxyServices();
    Collection<SynapseEventSource> eventSources = synapseConfig.getEventSources();
    Collection<Startup> tasks = synapseConfig.getStartups();
    Collection localEntries = synapseConfig.getLocalRegistry().values();
    Collection<PriorityExecutor> executors = synapseConfig.getPriorityExecutors().values();
    Collection<MessageStore> messageStores = synapseConfig.getMessageStores().values();
    Collection<MessageProcessor> messageProcessors = synapseConfig.getMessageProcessors().values();
    Collection<API> apiCollection = synapseConfig.getAPIs();
    Collection<SynapseImport> synapseImportsCollection = synapseConfig.getSynapseImports().values();
    Collection<InboundEndpoint> inboundEndpoints = synapseConfig.getInboundEndpoints();
    Collection<String> comments = synapseConfig.getCommentedTextList();
    for (ProxyService service : proxyServices) {
        if (service.getFileName() == null) {
            ProxyServiceSerializer.serializeProxy(definitions, service);
        }
    }
    for (SynapseEventSource source : eventSources) {
        if (source.getFileName() == null) {
            EventSourceSerializer.serializeEventSource(definitions, source);
        }
    }
    for (Startup task : tasks) {
        if (task instanceof AbstractStartup && task.getFileName() == null) {
            StartupFinder.getInstance().serializeStartup(definitions, task);
        }
    }
    for (Object o : localEntries) {
        if (o instanceof TemplateMediator) {
            TemplateMediator template = (TemplateMediator) o;
            if (template.getFileName() == null) {
                MediatorSerializerFinder.getInstance().getSerializer(template).serializeMediator(definitions, template);
            }
        } else if (o instanceof SequenceMediator) {
            SequenceMediator seq = (SequenceMediator) o;
            if (seq.getFileName() == null) {
                MediatorSerializerFinder.getInstance().getSerializer(seq).serializeMediator(definitions, seq);
            }
        } else if (o instanceof Template) {
            Template templEndpoint = (Template) o;
            if (templEndpoint.getFileName() == null) {
                new TemplateSerializer().serializeEndpointTemplate(templEndpoint, definitions);
            }
        } else if (o instanceof AbstractEndpoint) {
            AbstractEndpoint endpoint = (AbstractEndpoint) o;
            if (endpoint.getFileName() == null) {
                OMElement endpointElem = EndpointSerializer.getElementFromEndpoint(endpoint);
                definitions.addChild(endpointElem);
            }
        } else if (o instanceof Entry) {
            Entry entry = (Entry) o;
            if (entry.getFileName() == null) {
                if ((SynapseConstants.SERVER_HOST.equals(entry.getKey()) || SynapseConstants.SERVER_IP.equals(entry.getKey())) || entry.getType() == Entry.REMOTE_ENTRY) {
                    continue;
                }
                EntrySerializer.serializeEntry(entry, definitions);
            }
        }
    }
    for (PriorityExecutor executor : executors) {
        PriorityExecutorSerializer.serialize(definitions, executor, SynapseConstants.SYNAPSE_NAMESPACE);
    }
    for (MessageStore messageStore : messageStores) {
        if (messageStore.getFileName() == null) {
            MessageStoreSerializer.serializeMessageStore(definitions, messageStore);
        }
    }
    for (MessageProcessor messageProcessor : messageProcessors) {
        if (messageProcessor.getFileName() == null) {
            MessageProcessorSerializer.serializeMessageProcessor(definitions, messageProcessor);
        }
    }
    for (API api : apiCollection) {
        if (api.getFileName() == null) {
            APISerializer.serializeAPI(definitions, api);
        }
    }
    for (SynapseImport synapseImport : synapseImportsCollection) {
        if (synapseImport.getFileName() == null) {
            SynapseImportSerializer.serializeImport(definitions, synapseImport);
        }
    }
    for (InboundEndpoint inboundEndpoint : inboundEndpoints) {
        if (inboundEndpoint.getFileName() == null) {
            InboundEndpointSerializer.serializeInboundEndpoint(definitions, inboundEndpoint);
        }
    }
    serializeComments(comments, definitions);
    serializeSynapseXML(definitions);
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) MessageProcessor(org.apache.synapse.message.processor.MessageProcessor) OMElement(org.apache.axiom.om.OMElement) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) Template(org.apache.synapse.endpoints.Template) SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) Entry(org.apache.synapse.config.Entry) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateSerializer(org.apache.synapse.config.xml.endpoints.TemplateSerializer) AbstractStartup(org.apache.synapse.startup.AbstractStartup) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) OMFactory(org.apache.axiom.om.OMFactory) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) ProxyService(org.apache.synapse.core.axis2.ProxyService) Collection(java.util.Collection) API(org.apache.synapse.api.API) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Startup(org.apache.synapse.Startup) AbstractStartup(org.apache.synapse.startup.AbstractStartup)

Example 17 with API

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

the class MultiXMLConfigurationBuilder method createAPIs.

private static void createAPIs(SynapseConfiguration synapseConfig, String rootDirPath, Properties properties) {
    File apiDir = new File(rootDirPath, REST_API_DIR);
    if (apiDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading APIs from :" + apiDir.getPath());
        }
        Iterator apiIterator = FileUtils.iterateFiles(apiDir, extensions, false);
        while (apiIterator.hasNext()) {
            File file = (File) apiIterator.next();
            try {
                OMElement document = getOMElement(file);
                API api = SynapseXMLConfigurationFactory.defineAPI(synapseConfig, document, properties, false);
                if (api != null) {
                    api.setFileName(file.getName());
                    synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(), api.getName());
                }
            } catch (Exception e) {
                String msg = "API configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_API, msg, e);
            }
        }
        // order the apis based on context descending order
        try {
            SynapseXMLConfigurationFactory.reOrderAPIs(synapseConfig);
        } catch (Exception e) {
            String msg = "Error while re-ordering apis";
            handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_API, msg, e);
        }
    }
}
Also used : Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) API(org.apache.synapse.api.API) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException)

Example 18 with API

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

the class BasicAPIMediationTest method testRestURLPostfix2.

public void testRestURLPostfix2() 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);
    checkRestURLPostfix(synCtx, "/test");
    synCtx = getMessageContext(synapseConfig, false, "/test/me/now", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test/me/now");
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/test?a=5");
}
Also used : 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 19 with API

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

the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch2.

public void testBasicTemplateDispatch2() throws Exception {
    API api = new API("TestAPI", "/");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
    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, "/dictionary/c/cat", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/d/dog/", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    /* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
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 20 with API

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

the class URITemplateBasedDispatcherTest method testMultipleResourceDispatch.

public void testMultipleResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/");
    Resource resource1 = new Resource();
    resource1.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
    resource1.setInSequence(getTestSequence(PROP_NAME, "r1"));
    api.addResource(resource1);
    Resource resource2 = new Resource();
    resource2.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}"));
    resource2.setInSequence(getTestSequence(PROP_NAME, "r2"));
    api.addResource(resource2);
    Resource resource3 = new Resource();
    resource3.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}{#ref}"));
    resource3.setInSequence(getTestSequence(PROP_NAME, "r3"));
    api.addResource(resource3);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
    handler.process(synCtx);
    assertEquals("r1", synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/d", "GET");
    handler.process(synCtx);
    assertEquals("r2", synCtx.getProperty(PROP_NAME));
    assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/e#test", "GET");
    handler.process(synCtx);
    assertEquals("r3", synCtx.getProperty(PROP_NAME));
    assertEquals("e", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("test", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "ref"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat/test", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat#ref", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    /* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
    handler.process(synCtx);
    assertEquals("r1", synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
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)

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