use of org.wso2.carbon.apimgt.api.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPISwagger.
/**
* Updates the swagger definition of an existing API
*
* @param apiId API identifier
* @param apiDefinition Swagger definition
* @param url Swagger definition URL
* @param fileInputStream Swagger definition input file content
* @param fileDetail file meta information as Attachment
* @param ifMatch If-match header value
* @return updated swagger document of the API
*/
@Override
public Response updateAPISwagger(String apiId, String ifMatch, String apiDefinition, String url, InputStream fileInputStream, Attachment fileDetail, MessageContext messageContext) {
try {
String updatedSwagger;
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().getStatus());
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// Handle URL and file based definition imports
if (url != null || fileInputStream != null) {
// Validate and retrieve the OpenAPI definition
Map validationResponseMap = validateOpenAPIDefinition(url, fileInputStream, fileDetail, null, true, false);
APIDefinitionValidationResponse validationResponse = (APIDefinitionValidationResponse) validationResponseMap.get(RestApiConstants.RETURN_MODEL);
if (!validationResponse.isValid()) {
RestApiUtil.handleBadRequest(validationResponse.getErrorItems(), log);
}
updatedSwagger = PublisherCommonUtils.updateSwagger(apiId, validationResponse, false, organization);
} else {
updatedSwagger = updateSwagger(apiId, apiDefinition, organization);
}
return Response.ok().entity(updatedSwagger).build();
} catch (APIManagementException e) {
// to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while updating swagger definition of API: " + apiId, e, log);
} else {
String errorMessage = "Error while updating the swagger definition of the API: " + apiId + " - " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (FaultGatewaysException e) {
String errorMessage = "Error while updating API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method validateAPIExistence.
/**
* Validate whether the given API with UUID exists in the DB
*
* @param apiId API UUID
* @return API details
* @throws APIManagementException if the API doesn't exists in the DB
*/
private APIInfo validateAPIExistence(String apiId) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
APIInfo apiInfo = apiProvider.getAPIInfoByUUID(apiId);
if (apiInfo == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
return apiInfo;
}
use of org.wso2.carbon.apimgt.api.model.APIInfo 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.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteAPIDocument.
/**
* Deletes an existing document of an API
*
* @param apiId API identifier
* @param documentId document identifier
* @param ifMatch If-match header value
* @return 200 response if deleted successfully
*/
@Override
public Response deleteAPIDocument(String apiId, String documentId, String ifMatch, MessageContext messageContext) {
Documentation documentation;
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
// this will fail if user does not have access to the API or the API does not exist
// APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
documentation = apiProvider.getDocumentation(apiId, documentId, organization);
if (documentation == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
}
apiProvider.removeDocumentation(apiId, documentId, organization);
return Response.ok().build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while deleting : " + documentId + " of API " + apiId, e, log);
} else {
String errorMessage = "Error while retrieving API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSubscriptionPoliciesGet.
@Override
public Response apisApiIdSubscriptionPoliciesGet(String apiId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIDTO apiInfo = getAPIByAPIId(apiId, organization);
List<Tier> availableThrottlingPolicyList = new ThrottlingPoliciesApiServiceImpl().getThrottlingPolicyList(ThrottlingPolicyDTO.PolicyLevelEnum.SUBSCRIPTION.toString(), organization);
if (apiInfo != null) {
List<APITiersDTO> apiTiers = apiInfo.getTiers();
if (apiTiers != null && !apiTiers.isEmpty()) {
List<Tier> apiThrottlingPolicies = new ArrayList<>();
for (Tier policy : availableThrottlingPolicyList) {
for (APITiersDTO apiTier : apiTiers) {
if (apiTier.getTierName().equalsIgnoreCase(policy.getName())) {
apiThrottlingPolicies.add(policy);
}
}
}
return Response.ok().entity(apiThrottlingPolicies).build();
}
}
return null;
}
Aggregations