use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method createNewAPIVersion.
@Override
public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException {
URI newVersionedApiUri;
APIDTO newVersionedApi = new APIDTO();
ServiceEntry service = new ServiceEntry();
try {
APIIdentifier apiIdentifierFromTable = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifierFromTable == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
int tenantId = APIUtil.getInternalOrganizationId(organization);
API existingAPI = apiProvider.getAPIbyUUID(apiId, organization);
if (existingAPI == null) {
throw new APIMgtResourceNotFoundException("API not found for id " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
if (newVersion.equals(existingAPI.getId().getVersion())) {
throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName(), ExceptionCodes.from(API_VERSION_ALREADY_EXISTS, newVersion, existingAPI.getId().getApiName()));
}
if (StringUtils.isNotEmpty(serviceVersion)) {
String serviceName = existingAPI.getServiceInfo("name");
ServiceCatalogImpl serviceCatalog = new ServiceCatalogImpl();
service = serviceCatalog.getServiceByNameAndVersion(serviceName, serviceVersion, tenantId);
if (service == null) {
throw new APIManagementException("No matching service version found", ExceptionCodes.SERVICE_VERSION_NOT_FOUND);
}
}
if (StringUtils.isNotEmpty(serviceVersion) && !serviceVersion.equals(existingAPI.getServiceInfo("version"))) {
APIDTO apidto = createAPIDTO(existingAPI, newVersion);
if (ServiceEntry.DefinitionType.OAS2.equals(service.getDefinitionType()) || ServiceEntry.DefinitionType.OAS3.equals(service.getDefinitionType())) {
newVersionedApi = importOpenAPIDefinition(service.getEndpointDef(), null, null, apidto, null, service, organization);
} else if (ServiceEntry.DefinitionType.ASYNC_API.equals(service.getDefinitionType())) {
newVersionedApi = importAsyncAPISpecification(service.getEndpointDef(), null, apidto, null, service, organization);
}
} else {
API versionedAPI = apiProvider.createNewAPIVersion(apiId, newVersion, defaultVersion, organization);
newVersionedApi = APIMappingUtil.fromAPItoDTO(versionedAPI);
}
// This URI used to set the location header of the POST response
newVersionedApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + newVersionedApi.getId());
return Response.created(newVersionedApiUri).entity(newVersionedApi).build();
} catch (APIManagementException e) {
if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while copying API : " + apiId, e, log);
} else {
throw e;
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving API location of " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getOperationPolicyForAPIByPolicyId.
/**
* Get the API specific operation policy specification by providing the policy ID
*
* @param apiId API UUID
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return Operation policy DTO as response
*/
@Override
public Response getOperationPolicyForAPIByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate whether api exists or not
validateAPIExistence(apiId);
OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyId(operationPolicyId, apiId, organization, false);
if (existingPolicy != null) {
OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
return Response.ok().entity(policyDataDTO).build();
} else {
throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing operation policy with ID: " + operationPolicyId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
} else {
String errorMessage = "Error while getting an API specific operation policy with ID :" + operationPolicyId + " for API " + apiId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An Error has occurred while getting the operation policy with ID " + operationPolicyId + "operation policy for API " + apiId, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method addAPIMonetization.
/**
* Monetize (enable or disable) for a given API
*
* @param apiId API ID
* @param body request body
* @param messageContext message context
* @return monetizationDTO
*/
@Override
public Response addAPIMonetization(String apiId, APIMonetizationInfoDTO body, MessageContext messageContext) {
try {
if (StringUtils.isBlank(apiId)) {
String errorMessage = "API ID cannot be empty or null when configuring monetization.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
API api = apiProvider.getAPIbyUUID(apiId, organization);
if (!APIConstants.PUBLISHED.equalsIgnoreCase(api.getStatus())) {
String errorMessage = "API " + apiIdentifier.getApiName() + " should be in published state to configure monetization.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
// set the monetization status
boolean monetizationEnabled = body.isEnabled();
api.setMonetizationStatus(monetizationEnabled);
// clear the existing properties related to monetization
api.getMonetizationProperties().clear();
Map<String, String> monetizationProperties = body.getProperties();
if (MapUtils.isNotEmpty(monetizationProperties)) {
String errorMessage = RestApiPublisherUtils.validateMonetizationProperties(monetizationProperties);
if (!errorMessage.isEmpty()) {
RestApiUtil.handleBadRequest(errorMessage, log);
}
for (Map.Entry<String, String> currentEntry : monetizationProperties.entrySet()) {
api.addMonetizationProperty(currentEntry.getKey(), currentEntry.getValue());
}
}
Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
HashMap monetizationDataMap = new Gson().fromJson(api.getMonetizationProperties().toString(), HashMap.class);
boolean isMonetizationStateChangeSuccessful = false;
if (MapUtils.isEmpty(monetizationDataMap)) {
String errorMessage = "Monetization is not configured. Monetization data is empty for " + apiIdentifier.getApiName();
RestApiUtil.handleBadRequest(errorMessage, log);
}
try {
if (monetizationEnabled) {
isMonetizationStateChangeSuccessful = monetizationImplementation.enableMonetization(organization, api, monetizationDataMap);
} else {
isMonetizationStateChangeSuccessful = monetizationImplementation.disableMonetization(organization, api, monetizationDataMap);
}
} catch (MonetizationException e) {
String errorMessage = "Error while changing monetization status for API ID : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
if (isMonetizationStateChangeSuccessful) {
apiProvider.configureMonetizationInAPIArtifact(api);
APIMonetizationInfoDTO monetizationInfoDTO = APIMappingUtil.getMonetizationInfoDTO(apiId, organization);
return Response.ok().entity(monetizationInfoDTO).build();
} else {
String errorMessage = "Unable to change monetization status for API : " + apiId;
RestApiUtil.handleBadRequest(errorMessage, log);
}
} catch (APIManagementException e) {
String errorMessage = "Error while configuring monetization for API ID : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return Response.serverError().build();
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method validateDocument.
@Override
public Response validateDocument(String apiId, String name, String ifMatch, MessageContext messageContext) {
if (StringUtils.isEmpty(name) || StringUtils.isEmpty(apiId)) {
RestApiUtil.handleBadRequest("API Id and/ or document name should not be empty", log);
}
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
return apiProvider.isDocumentationExist(apiId, name, organization) ? Response.status(Response.Status.OK).build() : Response.status(Response.Status.NOT_FOUND).build();
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while checking the api existence", e, log);
}
return Response.status(Response.Status.NOT_FOUND).build();
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteAPILifecycleStatePendingTasks.
@Override
public Response deleteAPILifecycleStatePendingTasks(String apiId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
APIIdentifier apiIdentifierFromTable = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifierFromTable == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
apiProvider.deleteWorkflowTask(apiIdentifierFromTable);
return Response.ok().build();
} catch (APIManagementException e) {
String errorMessage = "Error while deleting task ";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
Aggregations