use of org.wso2.carbon.apimgt.api.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPIDocument.
/**
* Updates an existing document of an API
*
* @param apiId API identifier
* @param documentId document identifier
* @param body updated document DTO
* @param ifMatch If-match header value
* @return updated document DTO as response
*/
@Override
public Response updateAPIDocument(String apiId, String documentId, DocumentDTO body, String ifMatch, MessageContext messageContext) {
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());
String sourceUrl = body.getSourceUrl();
Documentation oldDocument = apiProvider.getDocumentation(apiId, documentId, organization);
// validation checks for existence of the document
if (body.getType() == null) {
throw new BadRequestException();
}
if (oldDocument == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
return null;
}
if (body.getType() == DocumentDTO.TypeEnum.OTHER && org.apache.commons.lang3.StringUtils.isBlank(body.getOtherTypeName())) {
// check otherTypeName for not null if doc type is OTHER
RestApiUtil.handleBadRequest("otherTypeName cannot be empty if type is OTHER.", log);
return null;
}
if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (org.apache.commons.lang3.StringUtils.isBlank(sourceUrl) || !RestApiCommonUtil.isURL(sourceUrl))) {
RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
return null;
}
// overriding some properties
body.setName(oldDocument.getName());
Documentation newDocumentation = DocumentationMappingUtil.fromDTOtoDocumentation(body);
newDocumentation.setFilePath(oldDocument.getFilePath());
newDocumentation.setId(documentId);
newDocumentation = apiProvider.updateDocumentation(apiId, newDocumentation, organization);
return Response.ok().entity(DocumentationMappingUtil.fromDocumentationToDTO(newDocumentation)).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 updating document : " + documentId + " of API " + apiId, e, log);
} else {
String errorMessage = "Error while updating the document " + documentId + " for 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 createAPIRevision.
/**
* Create a new API revision
*
* @param apiId UUID of the API
* @param apIRevisionDTO API object that needs to be added
* @param messageContext message context object
* @return response containing newly created APIRevision object
*/
@Override
public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate whether the API is advertise only
APIDTO apiDto = getAPIByID(apiId, apiProvider, organization);
if (apiDto != null && apiDto.getAdvertiseInfo() != null && apiDto.getAdvertiseInfo().isAdvertised()) {
throw new APIManagementException("Creating API Revisions is not supported for third party APIs: " + apiId);
}
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID(apiId);
apiRevision.setDescription(apIRevisionDTO.getDescription());
// adding the api revision
String revisionId = apiProvider.addAPIRevision(apiRevision, organization);
// Retrieve the newly added APIRevision to send in the response payload
APIRevision createdApiRevision = apiProvider.getAPIRevision(revisionId);
APIRevisionDTO createdApiRevisionDTO = APIMappingUtil.fromAPIRevisiontoDTO(createdApiRevision);
// This URI used to set the location header of the POST response
URI createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiRevisionDTO.getApiInfo().getId() + "/" + RestApiConstants.RESOURCE_PATH_REVISIONS + "/" + createdApiRevisionDTO.getId());
return Response.created(createdApiUri).entity(createdApiRevisionDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API Revision for API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving created revision API location for 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 undeployAPIRevision.
@Override
public Response undeployAPIRevision(String apiId, String revisionId, String revisionNum, Boolean allEnvironments, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTOList, 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 organization = RestApiUtil.getValidatedOrganization(messageContext);
if (revisionId == null && revisionNum != null) {
revisionId = apiProvider.getAPIRevisionUUID(revisionNum, apiId);
if (revisionId == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(null).build();
}
}
Map<String, Environment> environments = APIUtil.getEnvironments(organization);
List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
if (allEnvironments) {
apiRevisionDeployments = apiProvider.getAPIRevisionDeploymentList(revisionId);
} else {
for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTOList) {
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setRevisionUUID(revisionId);
String environment = apiRevisionDeploymentDTO.getName();
if (environments.get(environment) == null) {
RestApiUtil.handleBadRequest("Gateway environment not found: " + environment, log);
}
apiRevisionDeployment.setDeployment(environment);
apiRevisionDeployment.setVhost(apiRevisionDeploymentDTO.getVhost());
apiRevisionDeployment.setDisplayOnDevportal(apiRevisionDeploymentDTO.isDisplayOnDevportal());
apiRevisionDeployments.add(apiRevisionDeployment);
}
}
apiProvider.undeployAPIRevisionDeployment(apiId, revisionId, apiRevisionDeployments, organization);
List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeploymentList(revisionId);
List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsResponse) {
apiRevisionDeploymentDTOS.add(APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
}
Response.Status status = Response.Status.CREATED;
return Response.status(status).entity(apiRevisionDeploymentDTOS).build();
}
use of org.wso2.carbon.apimgt.api.model.APIInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateGraphQLPolicyComplexityOfAPI.
/**
* Update complexity details of a given API
*
* @param apiId apiId
* @param body GraphQLQueryComplexityInfo DTO as request body
* @param messageContext message context
* @return Response
*/
@Override
public Response updateGraphQLPolicyComplexityOfAPI(String apiId, GraphQLQueryComplexityInfoDTO body, MessageContext messageContext) throws APIManagementException {
try {
if (StringUtils.isBlank(apiId)) {
String errorMessage = "API ID cannot be empty or null.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
API existingAPI = apiProvider.getAPIbyUUID(apiId, organization);
String schema = apiProvider.getGraphqlSchema(apiInfo.toAPIIdentifier());
GraphqlComplexityInfo graphqlComplexityInfo = GraphqlQueryAnalysisMappingUtil.fromDTOtoValidatedGraphqlComplexityInfo(body, schema);
if (APIConstants.GRAPHQL_API.equals(existingAPI.getType())) {
apiProvider.addOrUpdateComplexityDetails(apiId, graphqlComplexityInfo);
return Response.ok().build();
} else {
throw new APIManagementException(ExceptionCodes.API_NOT_GRAPHQL);
}
} 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 complexity details of API : " + apiId, e, log);
} else {
String errorMessage = "Error while updating complexity details of 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 SubscriptionMappingUtil method fromSubscriptionToDTO.
/**
* Converts a SubscribedAPI object into SubscriptionDTO
*
* @param subscription SubscribedAPI object
* @param organization Identifier of the organization
* @return SubscriptionDTO corresponds to SubscribedAPI object
*/
public static SubscriptionDTO fromSubscriptionToDTO(SubscribedAPI subscription, String organization) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
subscriptionDTO.setSubscriptionId(subscription.getUUID());
APIInfoDTO apiInfo;
Identifier apiId = subscription.getIdentifier();
ApiTypeWrapper apiTypeWrapper;
try {
apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(subscription.getIdentifier().getUUID(), organization);
subscriptionDTO.setApiId(subscription.getIdentifier().getUUID());
Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
Map<String, Tier> tierMap = APIUtil.getTiers(organization);
if (apiTypeWrapper.isAPIProduct()) {
apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiTypeWrapper.getApiProduct(), organization);
APIMappingUtil.setThrottlePoliciesAndMonetization(apiTypeWrapper.getApiProduct(), apiInfo, deniedTiers, tierMap);
} else {
apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiTypeWrapper.getApi());
APIMappingUtil.setThrottlePoliciesAndMonetization(apiTypeWrapper.getApi(), apiInfo, deniedTiers, tierMap);
}
subscriptionDTO.setApiInfo(apiInfo);
} catch (APIManagementException e) {
if (log.isDebugEnabled()) {
log.debug("User :" + username + " does not have access to the API " + apiId);
}
apiInfo = new APIInfoDTO();
apiInfo.setName(apiId.getName());
apiInfo.setVersion(apiId.getVersion());
subscriptionDTO.setApiInfo(apiInfo);
}
Application application = subscription.getApplication();
application = apiConsumer.getLightweightApplicationByUUID(application.getUUID());
subscriptionDTO.setApplicationId(subscription.getApplication().getUUID());
subscriptionDTO.setStatus(SubscriptionDTO.StatusEnum.valueOf(subscription.getSubStatus()));
subscriptionDTO.setThrottlingPolicy(subscription.getTier().getName());
subscriptionDTO.setRequestedThrottlingPolicy(subscription.getRequestedTier().getName());
ApplicationInfoDTO applicationInfoDTO = ApplicationMappingUtil.fromApplicationToInfoDTO(application);
subscriptionDTO.setApplicationInfo(applicationInfoDTO);
return subscriptionDTO;
}
Aggregations