Search in sources :

Example 21 with API

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

the class APIDebugUtil method registerAPISequenceMediationFlowSkip.

/**
 * Registers/Un-registers a skip, point where mediator disables from the mediation flow
 *
 * @param synCfg Synapse configuration
 * @param mapping either resource url-mapping or uri-template
 * @param method resource http method
 * @param seqType 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 registerAPISequenceMediationFlowSkip(SynapseConfiguration synCfg, String mapping, String method, String seqType, String apiKey, int[] position, boolean registerMode) {
    SynapseSequenceType synapseSequenceType = SynapseSequenceType.valueOf(seqType.toUpperCase());
    APIMediationFlowPoint skipPoint = new APIMediationFlowPoint();
    skipPoint.setSynapseMediationComponent(SynapseMediationComponent.SEQUENCE);
    skipPoint.setKey(apiKey);
    skipPoint.setMediatorPosition(position);
    skipPoint.setSynapseSequenceType(synapseSequenceType);
    skipPoint.setSequenceBaseType(SynapseDebugCommandConstants.DEBUG_COMMAND_MEDIATION_COMPONENT_SEQUENCE_API);
    skipPoint.setResourceMapping(mapping);
    skipPoint.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 " + skipPoint.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 " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.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) {
            skipPoint.setMediatorReference(current_mediator);
            if (registerMode) {
                if (!((AbstractMediator) current_mediator).isSkipEnabled()) {
                    ((AbstractMediator) current_mediator).setSkipEnabled(true);
                    ((AbstractMediator) current_mediator).registerMediationFlowPoint(skipPoint);
                    if (log.isDebugEnabled()) {
                        log.debug("Registered skip at mediator position " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed register skip. Already skip enabled at mediator position " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_SKIP_ENABLED).toString());
                }
            } else {
                if (((AbstractMediator) current_mediator).isSkipEnabled()) {
                    ((AbstractMediator) current_mediator).unregisterMediationFlowPoint();
                    ((AbstractMediator) current_mediator).setSkipEnabled(false);
                    if (log.isDebugEnabled()) {
                        log.debug("Unregistered skip at mediator position " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed unregister skip. Already skip disabled at mediator position " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.getSynapseSequenceType().toString());
                    }
                    debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_SKIP_DISABLED).toString());
                }
            }
        } else {
            if (registerMode) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed register skip. Non existing mediator position at " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.getSynapseSequenceType().toString());
                }
                debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Failed unregister skip. Non existing mediator position at " + logMediatorPosition(skipPoint) + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod() + " sequence type " + skipPoint.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 skip. Non existing sequence " + skipPoint.getSynapseSequenceType().toString() + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.getResourceHTTPMethod());
            }
            debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Failed unregister skip. Non existing sequence " + skipPoint.getSynapseSequenceType().toString() + " for API key " + skipPoint.getKey() + " resource mapping " + skipPoint.getResourceMapping() + " resource HTTP method " + skipPoint.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.rest.Resource) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) API(org.apache.synapse.rest.API) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) APIMediationFlowPoint(org.apache.synapse.debug.constructs.APIMediationFlowPoint) SynapseMediationFlowPoint(org.apache.synapse.debug.constructs.SynapseMediationFlowPoint)

Example 22 with API

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

the class SynapseConfiguration method init.

/**
 * This method will be called in the startup of Synapse or in an initiation
 * and will initialize all the managed parts of the Synapse Configuration
 *
 * @param se SynapseEnvironment specifying the env to be initialized
 */
public synchronized void init(SynapseEnvironment se) {
    SynapseConfiguration previouseConfiguration = null;
    if (log.isDebugEnabled()) {
        log.debug("Initializing the Synapse Configuration using the SynapseEnvironment");
    }
    // initialize registry
    if (registry != null && registry instanceof ManagedLifecycle) {
        ((ManagedLifecycle) registry).init(se);
    }
    // we initialize xpath extensions here since synapse environment is available
    initXpathExtensions(se);
    initCarbonTenantConfigurator(se);
    // initialize endpoints
    for (Endpoint endpoint : getDefinedEndpoints().values()) {
        try {
            endpoint.init(se);
        } catch (Exception e) {
            log.error(" Error in initializing endpoint [" + endpoint.getName() + "] " + e.getMessage());
        }
    }
    // initialize sequence templates
    for (TemplateMediator seqTemplate : getSequenceTemplates().values()) {
        try {
            seqTemplate.init(se);
        } catch (Exception e) {
            log.error(" Error in initializing Sequence Template [" + seqTemplate.getName() + "] " + e.getMessage());
        }
    }
    String tenantDomain = getTenantDomain(se);
    if (tenantDomain != null) {
        previouseConfiguration = SynapseConfigUtils.getSynapseConfiguration(tenantDomain);
        SynapseConfigUtils.addSynapseConfiguration(tenantDomain, this);
    }
    if (previouseConfiguration != null) {
        destroyExistingInbounds(previouseConfiguration);
    }
    for (InboundEndpoint endpoint : getInboundEndpoints()) {
        try {
            endpoint.init(se);
        } catch (Exception e) {
            inboundEndpointMap.remove(endpoint.getName());
            log.error(" Error in initializing inbound endpoint [" + endpoint.getName() + "] " + e.getMessage());
        }
    }
    // initialize managed mediators
    for (ManagedLifecycle seq : getDefinedSequences().values()) {
        if (seq != null) {
            try {
                seq.init(se);
            } catch (Exception e) {
                log.error(" Error in initializing Sequence " + e.getMessage());
            }
        }
    }
    // initialize all the proxy services
    for (ProxyService proxy : getProxyServices()) {
        try {
            if (proxy.getTargetInLineEndpoint() != null) {
                proxy.getTargetInLineEndpoint().init(se);
            }
            if (proxy.getTargetInLineInSequence() != null) {
                proxy.getTargetInLineInSequence().init(se);
            }
            if (proxy.getTargetInLineOutSequence() != null) {
                proxy.getTargetInLineOutSequence().init(se);
            }
            if (proxy.getTargetInLineFaultSequence() != null) {
                proxy.getTargetInLineFaultSequence().init(se);
            }
        } catch (Exception e) {
            log.error(" Error in initializing Proxy Service [ " + proxy.getName() + "] " + e.getMessage());
        }
    }
    // initialize the startups
    for (ManagedLifecycle stp : getStartups()) {
        if (stp != null) {
            try {
                stp.init(se);
            } catch (Exception e) {
                log.error(" Error in initializing Stratups " + e.getMessage());
            }
        }
    }
    // initialize sequence executors
    for (PriorityExecutor executor : getPriorityExecutors().values()) {
        try {
            executor.init();
        } catch (Exception e) {
            log.error(" Error in initializing Executor [ " + executor.getName() + "] " + e.getMessage());
        }
    }
    // initialize message stores
    for (MessageStore messageStore : messageStores.values()) {
        try {
            messageStore.init(se);
        } catch (Exception e) {
            log.error(" Error in initializing Message Store [ " + messageStore.getName() + "] " + e.getMessage());
        }
    }
    // initialize message processors
    for (MessageProcessor messageProcessor : messageProcessors.values()) {
        try {
            messageProcessor.init(se);
        } catch (Exception e) {
            log.error(" Error in initializing Message Processor [ " + messageProcessor.getName() + "] " + e.getMessage());
        }
    }
    for (API api : apiTable.values()) {
        try {
            api.init(se);
        } catch (Exception e) {
            log.error(" Error in initializing API [ " + api.getName() + "] " + e.getMessage());
        }
    }
    initImportedLibraries(se);
}
Also used : InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) MessageStore(org.apache.synapse.message.store.MessageStore) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) ProxyService(org.apache.synapse.core.axis2.ProxyService) AbstractMessageProcessor(org.apache.synapse.message.processor.impl.AbstractMessageProcessor) MessageProcessor(org.apache.synapse.message.processor.MessageProcessor) API(org.apache.synapse.rest.API) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) ManagedLifecycle(org.apache.synapse.ManagedLifecycle) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

Example 23 with API

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

the class SynapseConfiguration method destroy.

public synchronized void destroy(boolean preserverState) {
    if (log.isDebugEnabled()) {
        log.debug("Destroying the Synapse Configuration");
    }
    // clear the timer tasks of Synapse
    synapseTimer.cancel();
    synapseTimer = null;
    // stop and shutdown all the proxy services
    for (ProxyService p : getProxyServices()) {
        if (p.getTargetInLineInSequence() != null) {
            p.getTargetInLineInSequence().destroy();
        }
        if (p.getTargetInLineOutSequence() != null) {
            p.getTargetInLineOutSequence().destroy();
        }
    }
    // destroy the managed mediators
    for (ManagedLifecycle seq : getDefinedSequences().values()) {
        seq.destroy();
    }
    // destroy sequence templates
    for (TemplateMediator seqTemplate : getSequenceTemplates().values()) {
        seqTemplate.destroy();
    }
    // destroy inbound endpoint
    for (InboundEndpoint endpoint : getInboundEndpoints()) {
        // This path trigger from server shutdown hook. So we don't want to remove scheduled inbound tasks
        // from registry. Only un-deployment should remove task from registry. Ref product-ei#1206
        endpoint.destroy(false);
    }
    // destroy the managed endpoints
    for (Endpoint endpoint : getDefinedEndpoints().values()) {
        endpoint.destroy();
    }
    // destroy the startups
    for (ManagedLifecycle stp : startups.values()) {
        stp.destroy();
    }
    // clear session information used for SA load balancing
    try {
        SALSessions.getInstance().reset();
        DataSourceRepositoryHolder.getInstance().getDataSourceRepositoryManager().clear();
    } catch (Throwable ignored) {
    }
    // destroy the priority executors.
    for (PriorityExecutor pe : executors.values()) {
        pe.destroy();
    }
    // destroy the Message Stores
    for (MessageStore ms : messageStores.values()) {
        if (ms instanceof AbstractMessageProcessor) {
            ((AbstractMessageProcessor) ms).destroy(preserverState);
        } else {
            ms.destroy();
        }
    }
    // destroy the Message processors
    for (MessageProcessor mp : messageProcessors.values()) {
        mp.destroy();
    }
    for (API api : apiTable.values()) {
        api.destroy();
    }
}
Also used : InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) MessageStore(org.apache.synapse.message.store.MessageStore) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) ProxyService(org.apache.synapse.core.axis2.ProxyService) AbstractMessageProcessor(org.apache.synapse.message.processor.impl.AbstractMessageProcessor) MessageProcessor(org.apache.synapse.message.processor.MessageProcessor) AbstractMessageProcessor(org.apache.synapse.message.processor.impl.AbstractMessageProcessor) API(org.apache.synapse.rest.API) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Example 24 with API

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

the class APISerializationTest method testAPISerialization5.

public void testAPISerialization5() throws Exception {
    String xml = "<api name=\"test\" context=\"/dictionary\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource url-mapping=\"/admin/view/*\" " + "><inSequence><log/><send/></inSequence><outSequence><log/><send/></outSequence></resource>" + "<resource url-mapping=\"/admin/*\"><inSequence><log/><send/></inSequence><outSequence>" + "<log/><send/></outSequence></resource><resource 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.rest.API)

Example 25 with API

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

the class APISerializationTest method testAPISerialization2.

public void testAPISerialization2() throws Exception {
    String xml = "<api name=\"test\" context=\"/dictionary\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource 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.rest.API)

Aggregations

API (org.apache.synapse.rest.API)26 OMElement (org.apache.axiom.om.OMElement)10 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 SynapseException (org.apache.synapse.SynapseException)4 ProxyService (org.apache.synapse.core.axis2.ProxyService)4 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)4 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)3 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)3 MessageStore (org.apache.synapse.message.store.MessageStore)3 Resource (org.apache.synapse.rest.Resource)3 File (java.io.File)2 Iterator (java.util.Iterator)2 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)2 Mediator (org.apache.synapse.Mediator)2 APIMediationFlowPoint (org.apache.synapse.debug.constructs.APIMediationFlowPoint)2 SynapseMediationFlowPoint (org.apache.synapse.debug.constructs.SynapseMediationFlowPoint)2 SynapseSequenceType (org.apache.synapse.debug.constructs.SynapseSequenceType)2 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)2