Search in sources :

Example 16 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method deleteSelectedApi.

/**
 * Delete Selected API
 * @param apiNames
 * @return
 * @throws APIException
 */
public void deleteSelectedApi(String[] apiNames) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        for (String apiName : apiNames) {
            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();
    }
}
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 17 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.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 18 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method generateAPIFromSwaggerByFormat.

/**
 * Function to generate API from swagger definition (from JSON representation)
 *
 * @param swaggerString swagger definition
 * @param isJSON        input in YAML / JSON format.
 * @return generated synapse API.
 * @throws APIException error occurred while retrieving host details.
 */
public String generateAPIFromSwaggerByFormat(String swaggerString, boolean isJSON) throws APIException {
    if (swaggerString == null || swaggerString.isEmpty()) {
        handleException(log, "Swagger provided is empty, hence unable to generate API", null);
    }
    if (!isJSON) {
        try {
            swaggerString = GenericApiObjectDefinition.convertYamlToJson(swaggerString);
        } catch (JsonProcessingException e) {
            handleException(log, "Error occurred while converting the provided YAML to JSON", null);
        }
    }
    JsonParser jsonParser = new JsonParser();
    JsonElement swaggerJson = jsonParser.parse(swaggerString);
    if (swaggerJson.isJsonObject()) {
        APIGenerator apiGenerator = new APIGenerator(swaggerJson.getAsJsonObject());
        try {
            API api = apiGenerator.generateSynapseAPI();
            return APISerializer.serializeAPI(api).toString();
        } catch (APIGenException e) {
            handleException(log, "Error occurred while generating API", e);
        }
    } else {
        handleException(log, "Error in swagger definition format: should be a json object", null);
    }
    // Definitely will not reach here
    return "";
}
Also used : APIGenException(org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException) APIGenerator(org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenerator) API(org.apache.synapse.api.API) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 19 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.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)

Example 20 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method updateSwaggerDocument.

/**
 * Create a registry resource for the swagger document and update the registry resource with the default swagger
 *
 * @param apiName Name of the API
 * @param swaggerJsonString
 * @param tenantId
 * @throws APIException
 */
public void updateSwaggerDocument(String apiName, String swaggerJsonString, 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);
    RegistryService registryService = RegistryServiceHolder.getInstance().getRegistryService();
    try {
        Registry registry = registryService.getConfigSystemRegistry(tenantId);
        if (resourcePath.startsWith(CONFIG_REG_PREFIX)) {
            resourcePath = resourcePath.substring(5);
            registry = registryService.getConfigSystemRegistry(tenantId);
        } else if (resourcePath.startsWith(GOV_REG_PREFIX)) {
            resourcePath = resourcePath.substring(4);
            registry = registryService.getGovernanceSystemRegistry(tenantId);
        } else {
            throw new APIException("Unable to update other sources. Only capable of updating swagger definitions " + "resides in configuration or governance registry");
        }
        org.wso2.carbon.registry.core.Resource resource;
        if (!registry.resourceExists(resourcePath)) {
            resource = registry.newResource();
            resource.setContent(swaggerJsonString);
            resource.setMediaType(APPLICATION_JSON_TYPE);
        } else {
            resource = registry.get(resourcePath);
            resource.setContent(swaggerJsonString);
        }
        registry.put(resourcePath, resource);
    } catch (RegistryException e) {
        handleException(log, "Could not update swagger document", e);
    }
}
Also used : APIException(org.wso2.carbon.rest.api.APIException) API(org.apache.synapse.api.API) Registry(org.wso2.carbon.registry.core.Registry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

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