Search in sources :

Example 11 with ApiException

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

the class RestApiAdmin method enableTracing.

public String enableTracing(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.enableTracing();
                // Need to enable statistics for tracing
                config.enableStatistics();
                api.configure(config);
            } else {
                api.getAspectConfiguration().enableTracing();
                // Need to enable statistics for tracing
                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 tracing in the Synapse configuration", null);
        }
    } catch (Exception fault) {
        handleException(log, "Couldn't enable tracing 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 12 with ApiException

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

the class RestApiAdmin method disableTracing.

public String disableTracing(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.disableTracing();
                api.configure(config);
            } else {
                api.getAspectConfiguration().disableTracing();
            }
            /**
             * 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 tracing in the Synapse configuration", null);
        }
    } catch (Exception fault) {
        handleException(log, "Couldn't disable tracing 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 13 with ApiException

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

the class RestApiAdmin method updateApiFromString.

public boolean updateApiFromString(String apiName, String apiData) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        assertNameNotEmpty(apiName);
        OMElement apiElement = AXIOMUtil.stringToOM(apiData);
        // Set API name to old value since we do not allow editing the API name.
        OMAttribute nameAttribute = apiElement.getAttribute(new QName("name"));
        if (nameAttribute == null || nameAttribute.getAttributeValue().trim().isEmpty()) {
            apiElement.addAttribute("name", apiName, null);
        }
        // Need to attach synapse configuration to properties to identify synapse imports (Connectors)
        // From Synapse level.
        Properties properties = new Properties();
        properties.put(SYNAPSE_CONFIGURATION, getSynapseConfiguration());
        API api = APIFactory.createAPI(apiElement, properties);
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        API oldAPI = synapseConfiguration.getAPI(apiName);
        if (oldAPI != null) {
            oldAPI.destroy();
            api.setFileName(oldAPI.getFileName());
        }
        synapseConfiguration.removeAPI(apiName);
        synapseConfiguration.addAPI(api.getName(), api);
        api.init(getSynapseEnvironment());
        if (oldAPI.getArtifactContainerName() != null) {
            api.setArtifactContainerName(oldAPI.getArtifactContainerName());
            api.setIsEdited(true);
            getApiByName(api.getName()).setIsEdited(true);
        } else {
            if (!Boolean.parseBoolean(System.getProperty("NonRegistryMode")) && saveRuntimeArtifacts) {
                MediationPersistenceManager pm = getMediationPersistenceManager();
                String fileName = api.getFileName();
                pm.deleteItem(apiName, fileName, ServiceBusConstants.ITEM_TYPE_REST_API);
                pm.saveItem(apiName, ServiceBusConstants.ITEM_TYPE_REST_API);
            }
        }
        return true;
    } catch (XMLStreamException e) {
        handleException(log, "Could not parse String to OMElement", e);
        return false;
    } finally {
        lock.unlock();
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) MediationPersistenceManager(org.wso2.carbon.mediation.initializer.persistence.MediationPersistenceManager) OMElement(org.apache.axiom.om.OMElement) API(org.apache.synapse.api.API) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) OMAttribute(org.apache.axiom.om.OMAttribute) Lock(java.util.concurrent.locks.Lock)

Example 14 with ApiException

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

the class RestApiAdmin method updateApi.

public boolean updateApi(String apiName, APIData apiData) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        assertNameNotEmpty(apiName);
        API oldAPI = null;
        // Need to attach synapse configuration to properties to identify synapse imports (Connectors)
        // From Synapse level.
        Properties properties = new Properties();
        properties.put(SYNAPSE_CONFIGURATION, getSynapseConfiguration());
        API api = APIFactory.createAPI(RestApiAdminUtils.retrieveAPIOMElement(apiData), properties);
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        oldAPI = synapseConfiguration.getAPI(apiName);
        if (oldAPI != null) {
            oldAPI.destroy();
            api.setFileName(oldAPI.getFileName());
        }
        synapseConfiguration.updateAPI(apiName, api);
        api.init(getSynapseEnvironment());
        if (oldAPI.getArtifactContainerName() != null) {
            api.setIsEdited(true);
            api.setArtifactContainerName(oldAPI.getArtifactContainerName());
            apiData.setIsEdited(true);
        } else {
            if (!Boolean.parseBoolean(System.getProperty("NonRegistryMode")) && saveRuntimeArtifacts) {
                MediationPersistenceManager pm = getMediationPersistenceManager();
                String fileName = api.getFileName();
                pm.deleteItem(apiName, fileName, ServiceBusConstants.ITEM_TYPE_REST_API);
                pm.saveItem(apiName, ServiceBusConstants.ITEM_TYPE_REST_API);
            }
        }
        return true;
    } 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 15 with ApiException

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

the class RestApiAdmin method generateUpdatedAPIFromSwaggerByFormat.

/**
 * Function to generate updated existing API by referring to swagger definition.
 *
 * @param swaggerJsonString swagger definition.
 * @param isJSON            input in YAML / JSON format.
 * @param existingApi       existing synapse API.
 * @return generated synapse API.
 * @throws APIException error occurred while retrieving host details.
 */
public String generateUpdatedAPIFromSwaggerByFormat(String swaggerJsonString, boolean isJSON, API existingApi) throws APIException {
    if (swaggerJsonString == null || swaggerJsonString.isEmpty()) {
        handleException(log, "Provided swagger definition is empty, hence unable to generate API", null);
    }
    if (existingApi == null) {
        handleException(log, "Provided existing API name is empty, hence unable to generate API", null);
    }
    if (!isJSON) {
        try {
            swaggerJsonString = GenericApiObjectDefinition.convertYamlToJson(swaggerJsonString);
        } catch (JsonProcessingException e) {
            handleException(log, "Error occurred while converting the provided YAML to JSON", null);
        }
    }
    JsonParser jsonParser = new JsonParser();
    JsonElement swaggerJson = jsonParser.parse(swaggerJsonString);
    if (swaggerJson.isJsonObject()) {
        APIGenerator apiGenerator = new APIGenerator(swaggerJson.getAsJsonObject());
        try {
            API api = apiGenerator.generateUpdatedSynapseAPI(existingApi);
            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)

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