Search in sources :

Example 6 with ApiException

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

the class APIServiceComponent method createdConfigurationContext.

public void createdConfigurationContext(ConfigurationContext configContext) {
    AxisConfiguration axisConfig = configContext.getAxisConfiguration();
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    if (axisConfig != null) {
        SynapseEnvironmentService synEnvService = ConfigHolder.getInstance().getSynapseEnvironmentService(tenantId);
        if (synEnvService != null) {
            try {
                registerDeployer(axisConfig, synEnvService.getSynapseEnvironment());
            } catch (APIException e) {
                log.error("Error while initializing API admin", e);
            }
        }
    }
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) SynapseEnvironmentService(org.wso2.carbon.mediation.initializer.services.SynapseEnvironmentService)

Example 7 with ApiException

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

the class RestApiAdminService method deleteApi.

/**
 * Deletes an API that carries the name that is passed in.
 *
 * @param apiName       Name of the API to be deleted
 * @return              Status of the execution of deleting the given API
 * @throws APIException Is thrown if an anomaly is encountered while deleting the given API
 */
public boolean deleteApi(String apiName) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        assertNameNotEmpty(log, apiName);
        apiName = apiName.trim();
        SynapseConfiguration synapseConfiguration = RestApiAdminUtils.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"))) {
                MediationPersistenceManager pm = RestApiAdminUtils.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) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 8 with ApiException

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

the class RestApiAdmin method generateUpdatedAPIFromSwagger.

/**
 * Function to generate updated existing API by referring to swagger definition (from JSON representation)
 *
 * @param swaggerJsonString swagger definition
 * @param existingApiName   name of the existing API
 * @return generated synapse API
 * @throws APIException
 */
public String generateUpdatedAPIFromSwagger(String swaggerJsonString, String existingApiName) throws APIException {
    if (swaggerJsonString == null || swaggerJsonString.isEmpty()) {
        handleException(log, "Provided swagger definition is empty, hence unable to generate API", null);
    }
    if (existingApiName == null || existingApiName.isEmpty()) {
        handleException(log, "Provided existing API name is empty, hence unable to generate API", null);
    }
    JsonParser jsonParser = new JsonParser();
    JsonElement swaggerJson = jsonParser.parse(swaggerJsonString);
    if (swaggerJson.isJsonObject()) {
        APIGenerator apiGenerator = new APIGenerator(swaggerJson.getAsJsonObject());
        try {
            API api = apiGenerator.generateUpdatedSynapseAPI(getSynapseAPIByName(existingApiName));
            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)

Example 9 with ApiException

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

the class RestApiAdmin method getApiForTenant.

/**
 * Set the tenant domain when a publisher tries to retrieve API his API in MT mode. When
 * publisher gets
 * the API, we login the gateway as supretenant. But we need to get the
 * API,which is in the particular tenant domain.
 *
 * @param apiName
 * @param tenantDomain
 * @return
 * @throws APIException
 */
public APIData getApiForTenant(String apiName, String tenantDomain) throws APIException {
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        APIData data = getApiByName(apiName);
        return data;
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : APIData(org.wso2.carbon.rest.api.APIData)

Example 10 with ApiException

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

the class RestApiAdmin method addSwaggerDocument.

/**
 * Replace the swagger document in the registry with the given swagger json string
 *
 * @param apiName
 * @param swaggerJsonString
 * @param tenantId
 * @throws APIException
 */
public void addSwaggerDocument(String apiName, String swaggerJsonString, int tenantId) throws APIException {
    String resourcePath = SwaggerConstants.DEFAULT_SWAGGER_REGISTRY_PATH + apiName + SwaggerConstants.SWAGGER_JSON_FILE_PATH;
    RegistryService registryService = RegistryServiceHolder.getInstance().getRegistryService();
    try {
        Registry registry = registryService.getConfigSystemRegistry(tenantId);
        org.wso2.carbon.registry.core.Resource resource;
        if (swaggerJsonString != null) {
            resource = registry.newResource();
            resource.setContent(swaggerJsonString);
            resource.setMediaType(APPLICATION_JSON_TYPE);
            registry.put(resourcePath, resource);
        }
    } catch (RegistryException e) {
        handleException(log, "Could not add swagger document", e);
    }
}
Also used : 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