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();
}
}
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;
}
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 "";
}
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;
}
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);
}
}
Aggregations