use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPIDeployment.
@Override
public Response updateAPIDeployment(String apiId, String deploymentId, APIRevisionDeploymentDTO apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
String revisionId = apIRevisionDeploymentDTO.getRevisionUuid();
String decodedDeploymentName;
if (deploymentId != null) {
try {
decodedDeploymentName = new String(Base64.getUrlDecoder().decode(deploymentId), StandardCharsets.UTF_8);
} catch (IllegalArgumentException e) {
throw new APIMgtResourceNotFoundException("deployment with " + deploymentId + " not found", ExceptionCodes.from(ExceptionCodes.EXISTING_DEPLOYMENT_NOT_FOUND, deploymentId));
}
} else {
throw new APIMgtResourceNotFoundException("deployment id not found", ExceptionCodes.from(ExceptionCodes.DEPLOYMENT_ID_NOT_FOUND));
}
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setRevisionUUID(revisionId);
apiRevisionDeployment.setDeployment(decodedDeploymentName);
apiRevisionDeployment.setVhost(apIRevisionDeploymentDTO.getVhost());
apiRevisionDeployment.setDisplayOnDevportal(apIRevisionDeploymentDTO.isDisplayOnDevportal());
apiProvider.updateAPIDisplayOnDevportal(apiId, revisionId, apiRevisionDeployment);
APIRevisionDeployment apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeployment(decodedDeploymentName, revisionId);
APIRevisionDeploymentDTO apiRevisionDeploymentDTO = APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeploymentsResponse);
Response.Status status = Response.Status.OK;
return Response.status(status).entity(apiRevisionDeploymentDTO).build();
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPIResourcePoliciesByPolicyId.
/**
* Update the resource policies(inflow/outflow) given the resource id.
*
* @param apiId API ID
* @param resourcePolicyId resource policy id
* @param body resource policy content
* @param ifMatch If-Match header value
* @return json response of the updated sequence content
*/
@Override
public Response updateAPIResourcePoliciesByPolicyId(String apiId, String resourcePolicyId, ResourcePolicyInfoDTO body, String ifMatch, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider provider = RestApiCommonUtil.getLoggedInUserProvider();
API api = provider.getAPIbyUUID(apiId, organization);
if (api == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(api.getStatus());
if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
if (StringUtils.isEmpty(resourcePolicyId)) {
String errorMessage = "Resource id should not be empty to update a resource policy.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
boolean isValidSchema = RestApiPublisherUtils.validateXMLSchema(body.getContent());
if (isValidSchema) {
List<SOAPToRestSequence> sequence = api.getSoapToRestSequences();
for (SOAPToRestSequence soapToRestSequence : sequence) {
if (soapToRestSequence.getUuid().equals(resourcePolicyId)) {
soapToRestSequence.setContent(body.getContent());
break;
}
}
API originalAPI = provider.getAPIbyUUID(apiId, organization);
provider.updateAPI(api, originalAPI);
SequenceUtils.updateResourcePolicyFromRegistryResourceId(api.getId(), resourcePolicyId, body.getContent());
String updatedPolicyContent = SequenceUtils.getResourcePolicyFromRegistryResourceId(api, resourcePolicyId);
ResourcePolicyInfoDTO resourcePolicyInfoDTO = APIMappingUtil.fromResourcePolicyStrToInfoDTO(updatedPolicyContent);
return Response.ok().entity(resourcePolicyInfoDTO).build();
} else {
String errorMessage = "Error while validating the resource policy xml content for the API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, log);
}
} else {
String errorMessage = "The provided api with id: " + apiId + " is not a soap to rest converted api.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
} catch (APIManagementException | FaultGatewaysException e) {
String errorMessage = "Error while retrieving the API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method deleteCommonOperationPolicyByPolicyId.
/**
* Delete a common operation policy by providing the policy ID
*
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return ok if deleted successfully
*/
@Override
public Response deleteCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
if (existingPolicy != null) {
apiProvider.deleteOperationPolicyById(operationPolicyId, organization);
if (log.isDebugEnabled()) {
log.debug("The common operation policy " + operationPolicyId + " has been deleted");
}
return Response.ok().build();
} else {
throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
} else {
String errorMessage = "Error while deleting the common operation policy with ID: " + operationPolicyId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An Error has occurred while deleting the common operation policy + " + operationPolicyId, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ApiMgtDAO method getSharedScopeUsage.
/**
* Get the API and URI usages of the given shared scope
*
* @param uuid Id of the shared scope
* @param tenantId tenant Id
* @return usgaes ofr the shaerd scope
* @throws APIManagementException If an error occurs while getting the usage details
*/
public SharedScopeUsage getSharedScopeUsage(String uuid, int tenantId) throws APIManagementException {
SharedScopeUsage sharedScopeUsage;
List<API> usedApiList = new ArrayList<>();
String sharedScopeName = getSharedScopeKeyByUUID(uuid);
if (sharedScopeName != null) {
sharedScopeUsage = new SharedScopeUsage();
sharedScopeUsage.setId(uuid);
sharedScopeUsage.setName(sharedScopeName);
} else {
throw new APIMgtResourceNotFoundException("Shared Scope not found for scope ID: " + uuid, ExceptionCodes.from(ExceptionCodes.SHARED_SCOPE_NOT_FOUND, uuid));
}
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement psForApiUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_API_USAGE_BY_TENANT)) {
psForApiUsage.setString(1, uuid);
psForApiUsage.setInt(2, tenantId);
try (ResultSet apiUsageResultSet = psForApiUsage.executeQuery()) {
while (apiUsageResultSet.next()) {
String provider = apiUsageResultSet.getString("API_PROVIDER");
String apiName = apiUsageResultSet.getString("API_NAME");
String version = apiUsageResultSet.getString("API_VERSION");
APIIdentifier apiIdentifier = new APIIdentifier(provider, apiName, version);
API usedApi = new API(apiIdentifier);
usedApi.setContext(apiUsageResultSet.getString("CONTEXT"));
try (PreparedStatement psForUriUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_URI_USAGE_BY_TENANT)) {
int apiId = apiUsageResultSet.getInt("API_ID");
Set<URITemplate> usedUriTemplates = new LinkedHashSet<>();
psForUriUsage.setString(1, uuid);
psForUriUsage.setInt(2, tenantId);
psForUriUsage.setInt(3, apiId);
try (ResultSet uriUsageResultSet = psForUriUsage.executeQuery()) {
while (uriUsageResultSet.next()) {
URITemplate usedUriTemplate = new URITemplate();
usedUriTemplate.setUriTemplate(uriUsageResultSet.getString("URL_PATTERN"));
usedUriTemplate.setHTTPVerb(uriUsageResultSet.getString("HTTP_METHOD"));
usedUriTemplates.add(usedUriTemplate);
}
}
usedApi.setUriTemplates(usedUriTemplates);
usedApiList.add(usedApi);
} catch (SQLException e) {
handleException("Failed to retrieve Resource usages of shared scope with scope ID " + uuid, e);
}
}
}
if (sharedScopeUsage != null) {
sharedScopeUsage.setApis(usedApiList);
}
return sharedScopeUsage;
} catch (SQLException e) {
handleException("Failed to retrieve API usages of shared scope with scope ID" + uuid, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class ImportUtils method retrieveApiToOverwrite.
/**
* This method retrieves an API to overwrite in the current tenant domain.
*
* @param apiName API Name
* @param apiVersion API Version
* @param currentTenantDomain Current tenant domain
* @param apiProvider API Provider
* @param ignoreAndImport This should be true if the exception should be ignored
* @param organization Organization
* @throws APIManagementException If an error occurs when retrieving the API to overwrite
*/
private static API retrieveApiToOverwrite(String apiName, String apiVersion, String currentTenantDomain, APIProvider apiProvider, Boolean ignoreAndImport, String organization) throws APIManagementException {
String provider = APIUtil.getAPIProviderFromAPINameVersionTenant(apiName, apiVersion, currentTenantDomain);
APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(provider), apiName, apiVersion);
// Checking whether the API exists
if (!apiProvider.isAPIAvailable(apiIdentifier, organization)) {
if (ignoreAndImport) {
return null;
}
throw new APIMgtResourceNotFoundException("Error occurred while retrieving the API. API: " + apiName + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiVersion + " not found", ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion()));
}
String uuid = APIUtil.getUUIDFromIdentifier(apiIdentifier, organization);
return apiProvider.getAPIbyUUID(uuid, currentTenantDomain);
}
Aggregations