Search in sources :

Example 16 with APIException

use of org.wso2.carbon.rest.api.APIException in project carbon-mediation by wso2.

the class RestApiAdmin method disableStatistics.

public String disableStatistics(String apiName) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        API api = synapseConfiguration.getAPI(apiName);
        if (api != null) {
            if (api.getAspectConfiguration() == null) {
                AspectConfiguration config = new AspectConfiguration(apiName);
                config.disableStatistics();
                api.configure(config);
            } else {
                api.getAspectConfiguration().disableStatistics();
            }
            /**
             * Persist the api service if it is not deployed via an artifact container
             */
            if (api.getArtifactContainerName() == null) {
                persistApi(api);
            }
            return apiName;
        } else {
            handleException(log, "No defined API with name " + apiName + " found to disable statistics in the Synapse configuration", null);
        }
    } catch (Exception fault) {
        handleException(log, "Couldn't disable statistics of the API " + apiName + " : " + fault.getMessage(), fault);
    } finally {
        lock.unlock();
    }
    return null;
}
Also used : API(org.apache.synapse.api.API) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIException(org.wso2.carbon.rest.api.APIException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIGenException(org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException) Lock(java.util.concurrent.locks.Lock)

Example 17 with APIException

use of org.wso2.carbon.rest.api.APIException in project carbon-mediation by wso2.

the class RestApiAdmin method handleException.

private void handleException(Log log, String message, Exception e) throws APIException {
    if (e == null) {
        APIException apiException = new APIException(message);
        log.error(message, apiException);
        throw apiException;
    } else {
        message = message + " :: " + e.getMessage();
        log.error(message, e);
        throw new APIException(message, e);
    }
}
Also used : APIException(org.wso2.carbon.rest.api.APIException)

Example 18 with APIException

use of org.wso2.carbon.rest.api.APIException in project carbon-mediation by wso2.

the class RestApiAdmin method getSwaggerDocument.

/**
 * Return the registry resource for the provided location
 *
 * @param apiName
 * @param tenantId
 * @return
 * @throws APIException
 */
public String getSwaggerDocument(String apiName, int tenantId) throws APIException {
    API api = getSynapseAPIByName(apiName);
    if (api == null) {
        throw new APIException("API with name \"" + apiName + "\" does not exists.");
    }
    String resourcePath = getSwaggerResourcePath(api);
    String swaggerJsonString = null;
    if (resourcePath.startsWith(CONFIG_REG_PREFIX) || resourcePath.startsWith(GOV_REG_PREFIX)) {
        try {
            swaggerJsonString = getResourceFromRegistry(resourcePath, tenantId);
        } catch (RegistryException e) {
            handleException(log, "Could not get swagger document", e);
        }
    } else {
        // Read from URI
        try {
            swaggerJsonString = readFromURI(resourcePath);
        } catch (IOException e) {
            log.error("Error occurred while reading swagger definition from: " + resourcePath, e);
        }
    }
    // Generate if not available
    if (swaggerJsonString == null) {
        if (log.isDebugEnabled()) {
            log.debug("Generate swagger definition for the API : " + apiName);
        }
        swaggerJsonString = generateSwaggerFromSynapseAPI(api);
    }
    return swaggerJsonString;
}
Also used : APIException(org.wso2.carbon.rest.api.APIException) API(org.apache.synapse.api.API) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 19 with APIException

use of org.wso2.carbon.rest.api.APIException in project carbon-mediation by wso2.

the class RestApiAdmin method deleteApi.

public boolean deleteApi(String apiName) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        assertNameNotEmpty(apiName);
        apiName = apiName.trim();
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        API api = synapseConfiguration.getAPI(apiName);
        if (api.getArtifactContainerName() == null) {
            if (log.isDebugEnabled()) {
                log.debug("Deleting API : " + apiName + " from the configuration");
            }
            api.destroy();
            synapseConfiguration.removeAPI(apiName);
            if (!Boolean.parseBoolean(System.getProperty("NonRegistryMode")) && saveRuntimeArtifacts) {
                MediationPersistenceManager pm = getMediationPersistenceManager();
                String fileName = api.getFileName();
                pm.deleteItem(apiName, fileName, ServiceBusConstants.ITEM_TYPE_REST_API);
            }
            if (log.isDebugEnabled()) {
                log.debug("Api : " + apiName + " removed from the configuration");
            }
        }
    } finally {
        lock.unlock();
    }
    return true;
}
Also used : MediationPersistenceManager(org.wso2.carbon.mediation.initializer.persistence.MediationPersistenceManager) API(org.apache.synapse.api.API) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Lock(java.util.concurrent.locks.Lock)

Example 20 with APIException

use of org.wso2.carbon.rest.api.APIException in project carbon-mediation by wso2.

the class RestApiAdmin method enableStatistics.

public String enableStatistics(String apiName) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        API api = synapseConfiguration.getAPI(apiName);
        if (api != null) {
            if (api.getAspectConfiguration() == null) {
                AspectConfiguration config = new AspectConfiguration(apiName);
                config.enableStatistics();
                api.configure(config);
            } else {
                api.getAspectConfiguration().enableStatistics();
            }
            /**
             * Persist the api service if it is not deployed via an artifact container
             */
            if (api.getArtifactContainerName() == null) {
                persistApi(api);
            }
            return apiName;
        } else {
            handleException(log, "No defined API with name " + apiName + " found to enable statistics in the Synapse configuration", null);
        }
    } catch (Exception fault) {
        handleException(log, "Couldn't enable statistics of the API " + apiName + " : " + fault.getMessage(), fault);
    } finally {
        lock.unlock();
    }
    return null;
}
Also used : API(org.apache.synapse.api.API) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIException(org.wso2.carbon.rest.api.APIException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIGenException(org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException) Lock(java.util.concurrent.locks.Lock)

Aggregations

API (org.apache.synapse.api.API)16 Lock (java.util.concurrent.locks.Lock)10 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)10 APIException (org.wso2.carbon.rest.api.APIException)9 APIGenException (org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 ApiException (org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException)7 IOException (java.io.IOException)6 MalformedURLException (java.net.MalformedURLException)6 SocketException (java.net.SocketException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Pair (org.wso2.carbon.identity.mgt.endpoint.util.client.Pair)6 Property (org.wso2.carbon.identity.mgt.endpoint.util.client.model.Property)6 MediationPersistenceManager (org.wso2.carbon.mediation.initializer.persistence.MediationPersistenceManager)6 GenericType (com.sun.jersey.api.client.GenericType)4 AspectConfiguration (org.apache.synapse.aspects.AspectConfiguration)4 QName (javax.xml.namespace.QName)3