use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class PublisherCommonUtils method addAPIProductWithGeneratedSwaggerDefinition.
/**
* Add API Product with the generated swagger from the DTO.
*
* @param apiProductDTO API Product DTO
* @param username Username
* @param organization Identifier of the organization
* @return Created API Product object
* @throws APIManagementException Error while creating the API Product
* @throws FaultGatewaysException Error while adding the API Product to gateway
*/
public static APIProduct addAPIProductWithGeneratedSwaggerDefinition(APIProductDTO apiProductDTO, String username, String organization) throws APIManagementException, FaultGatewaysException {
username = StringUtils.isEmpty(username) ? RestApiCommonUtil.getLoggedInUsername() : username;
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
// if not add product
String provider = apiProductDTO.getProvider();
String context = apiProductDTO.getContext();
if (!StringUtils.isBlank(provider) && !provider.equals(username)) {
if (!APIUtil.hasPermission(username, APIConstants.Permissions.APIM_ADMIN)) {
if (log.isDebugEnabled()) {
log.debug("User " + username + " does not have admin permission (" + APIConstants.Permissions.APIM_ADMIN + ") hence provider (" + provider + ") overridden with current user (" + username + ")");
}
provider = username;
}
} else {
// Set username in case provider is null or empty
provider = username;
}
List<String> tiersFromDTO = apiProductDTO.getPolicies();
Set<Tier> definedTiers = apiProvider.getTiers();
List<String> invalidTiers = PublisherCommonUtils.getInvalidTierNames(definedTiers, tiersFromDTO);
if (!invalidTiers.isEmpty()) {
throw new APIManagementException("Specified tier(s) " + Arrays.toString(invalidTiers.toArray()) + " are invalid", ExceptionCodes.TIER_NAME_INVALID);
}
if (apiProductDTO.getAdditionalProperties() != null) {
String errorMessage = PublisherCommonUtils.validateAdditionalProperties(apiProductDTO.getAdditionalProperties());
if (!errorMessage.isEmpty()) {
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.INVALID_ADDITIONAL_PROPERTIES, apiProductDTO.getName()));
}
}
if (apiProductDTO.getVisibility() == null) {
// set the default visibility to PUBLIC
apiProductDTO.setVisibility(APIProductDTO.VisibilityEnum.PUBLIC);
}
if (apiProductDTO.getAuthorizationHeader() == null) {
apiProductDTO.setAuthorizationHeader(APIUtil.getOAuthConfigurationFromAPIMConfig(APIConstants.AUTHORIZATION_HEADER));
}
if (apiProductDTO.getAuthorizationHeader() == null) {
apiProductDTO.setAuthorizationHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT);
}
// Remove the /{version} from the context.
if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) {
context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, "");
}
// Make sure context starts with "/". ex: /pizzaProduct
context = context.startsWith("/") ? context : ("/" + context);
// Check whether the context already exists
if (apiProvider.isContextExist(context)) {
throw new APIManagementException("Error occurred while adding API Product. API Product with the context " + context + " already " + "exists.", ExceptionCodes.from(ExceptionCodes.API_PRODUCT_CONTEXT_ALREADY_EXISTS, context));
}
// Set default gatewayVendor
if (apiProductDTO.getGatewayVendor() == null) {
apiProductDTO.setGatewayVendor(APIConstants.WSO2_GATEWAY_ENVIRONMENT);
}
APIProduct productToBeAdded = APIMappingUtil.fromDTOtoAPIProduct(apiProductDTO, provider);
productToBeAdded.setOrganization(organization);
if (!APIConstants.PROTOTYPED.equals(productToBeAdded.getState())) {
productToBeAdded.setState(APIConstants.CREATED);
}
APIProductIdentifier createdAPIProductIdentifier = productToBeAdded.getId();
Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.addAPIProductWithoutPublishingToGateway(productToBeAdded);
APIProduct createdProduct = apiProvider.getAPIProduct(createdAPIProductIdentifier);
apiProvider.addAPIProductSwagger(createdProduct.getUuid(), apiToProductResourceMapping, createdProduct, organization);
createdProduct = apiProvider.getAPIProduct(createdAPIProductIdentifier);
return createdProduct;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class PublisherCommonUtils method getLifecycleStateInformation.
/**
* Get lifecycle state information of API or API Product
*
* @param identifier Unique identifier of API or API Product
* @param organization Organization of logged-in user
* @return LifecycleStateDTO object
* @throws APIManagementException if there is en error while retrieving the lifecycle state information
*/
public static LifecycleStateDTO getLifecycleStateInformation(Identifier identifier, String organization) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
Map<String, Object> apiLCData = apiProvider.getAPILifeCycleData(identifier.getUUID(), organization);
if (apiLCData == null) {
String type;
if (identifier instanceof APIProductIdentifier) {
type = APIConstants.API_PRODUCT;
} else {
type = APIConstants.API_IDENTIFIER_TYPE;
}
throw new APIManagementException("Error while getting lifecycle state for " + type + " with ID " + identifier, ExceptionCodes.from(ExceptionCodes.LIFECYCLE_STATE_INFORMATION_NOT_FOUND, type, identifier.getUUID()));
} else {
boolean apiOlderVersionExist = false;
// check whether other versions of the current API exists
APIVersionStringComparator comparator = new APIVersionStringComparator();
Set<String> versions = apiProvider.getAPIVersions(APIUtil.replaceEmailDomain(identifier.getProviderName()), identifier.getName(), organization);
for (String tempVersion : versions) {
if (comparator.compare(tempVersion, identifier.getVersion()) < 0) {
apiOlderVersionExist = true;
break;
}
}
return APIMappingUtil.fromLifecycleModelToDTO(apiLCData, apiOlderVersionExist);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getAPIProductThumbnail.
@Override
public Response getAPIProductThumbnail(String apiProductId, String accept, String ifNoneMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
// this will fail if user does not have access to the API or the API does not exist
APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, tenantDomain);
ResourceFile thumbnailResource = apiProvider.getIcon(apiProductId, tenantDomain);
if (thumbnailResource != null) {
return Response.ok(thumbnailResource.getContent(), MediaType.valueOf(thumbnailResource.getContentType())).build();
} else {
return Response.noContent().build();
}
} catch (APIManagementException e) {
// existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving thumbnail of API Product : " + apiProductId, e, log);
} else {
String errorMessage = "Error while retrieving thumbnail of API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method deleteAPIProductLifecycleStatePendingTasks.
@Override
public Response deleteAPIProductLifecycleStatePendingTasks(String apiProductId, MessageContext messageContext) throws APIManagementException {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
APIProductIdentifier productIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiProductId);
if (productIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Product with UUID: " + apiProductId, ExceptionCodes.from(ExceptionCodes.API_PRODUCT_NOT_FOUND, apiProductId));
}
apiProvider.deleteWorkflowTask(productIdentifier);
return Response.ok().build();
} catch (APIManagementException e) {
String errorMessage = "Error while deleting task ";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method addAPIProductDocument.
@Override
public Response addAPIProductDocument(String apiProductId, DocumentDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
if (body.getType() == null) {
throw new BadRequestException();
}
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);
}
String sourceUrl = body.getSourceUrl();
if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (org.apache.commons.lang3.StringUtils.isBlank(sourceUrl) || !RestApiCommonUtil.isURL(sourceUrl))) {
RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
}
Documentation documentation = DocumentationMappingUtil.fromDTOtoDocumentation(body);
String documentName = body.getName();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// this will fail if user does not have access to the API Product or the API Product does not exist
APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
if (apiProvider.isDocumentationExist(apiProductId, documentName, organization)) {
String errorMessage = "Requested document '" + documentName + "' already exists";
RestApiUtil.handleResourceAlreadyExistsError(errorMessage, log);
}
documentation = apiProvider.addDocumentation(apiProductId, documentation, organization);
DocumentDTO newDocumentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
String uriString = RestApiConstants.RESOURCE_PATH_PRODUCT_DOCUMENTS_DOCUMENT_ID.replace(RestApiConstants.APIPRODUCTID_PARAM, apiProductId).replace(RestApiConstants.DOCUMENTID_PARAM, documentation.getId());
URI uri = new URI(uriString);
return Response.created(uri).entity(newDocumentDTO).build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing API Products. 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_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while adding documents of API : " + apiProductId, e, log);
} else {
String errorMessage = "Error while adding the document for API : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving location for document " + body.getName() + " of API " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
Aggregations