use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method createAPIProduct.
@Override
public Response createAPIProduct(APIProductDTO body, MessageContext messageContext) throws APIManagementException {
String provider = body.getProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIProduct createdProduct = PublisherCommonUtils.addAPIProductWithGeneratedSwaggerDefinition(body, RestApiCommonUtil.getLoggedInUsername(), organization);
APIProductDTO createdApiProductDTO = APIMappingUtil.fromAPIProducttoDTO(createdProduct);
URI createdApiProductUri = new URI(RestApiConstants.RESOURCE_PATH_API_PRODUCTS + "/" + createdApiProductDTO.getId());
return Response.created(createdApiProductUri).entity(createdApiProductDTO).build();
} catch (APIManagementException | FaultGatewaysException e) {
String errorMessage = "Error while adding new API Product : " + provider + "-" + body.getName() + " - " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving API Product location : " + provider + "-" + body.getName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method updateAPIProduct.
@Override
public Response updateAPIProduct(String apiProductId, APIProductDTO body, String ifMatch, MessageContext messageContext) {
try {
String username = RestApiCommonUtil.getLoggedInUsername();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
APIProduct retrievedProduct = apiProvider.getAPIProductbyUUID(apiProductId, tenantDomain);
if (retrievedProduct == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, log);
}
APIProduct updatedProduct = PublisherCommonUtils.updateApiProduct(retrievedProduct, body, apiProvider, username, tenantDomain);
APIProductDTO updatedProductDTO = getAPIProductByID(apiProductId, apiProvider);
return Response.ok().entity(updatedProductDTO).build();
} catch (APIManagementException | FaultGatewaysException e) {
if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("User is not authorized to access the API", e, log);
} else {
String errorMessage = "Error while updating API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getAPIProduct.
@Override
public Response getAPIProduct(String apiProductId, String accept, String ifNoneMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(username));
if (log.isDebugEnabled()) {
log.debug("API Product request: Id " + apiProductId + " by " + username);
}
APIProduct apiProduct = apiProvider.getAPIProductbyUUID(apiProductId, tenantDomain);
if (apiProduct == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, log);
}
APIProductDTO createdApiProductDTO = getAPIProductByID(apiProductId, apiProvider);
return Response.ok().entity(createdApiProductDTO).build();
} catch (APIManagementException e) {
if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("User is not authorized to access the API", e, log);
} else {
String errorMessage = "Error while retrieving API Product from Id : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getAPIProductByID.
private APIProductDTO getAPIProductByID(String apiProductId, APIProvider apiProvider) {
try {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
APIProduct api = apiProvider.getAPIProductbyUUID(apiProductId, tenantDomain);
return APIMappingUtil.fromAPIProducttoDTO(api);
} catch (APIManagementException e) {
// 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("User is not authorized to access the API Product", e, log);
} else {
String errorMessage = "Error while retrieving API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPIProduct.
@Test
public void testResourceConfigContextForAPIProduct() throws Exception {
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier("admin", "TestAPIProduct", "1.0.0", UUID.randomUUID().toString());
APIProduct apiProduct = new APIProduct(apiProductIdentifier);
apiProduct.setProductResources(getAPIProductResources(apiProductIdentifier));
apiProduct.setType(APIConstants.API_PRODUCT);
ConfigContext configcontext = new APIConfigContext(apiProduct);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, apiProduct);
resourceConfigContext.validate();
VelocityContext context = resourceConfigContext.getContext();
Assert.assertNotNull(context.get("apiType"));
Assert.assertEquals(context.get("apiType"), APIConstants.API_PRODUCT);
Object aggregates = context.get("aggregates");
Assert.assertNotNull(aggregates);
Assert.assertTrue(aggregates instanceof List);
List<APIProductResource> apiProductResources = (List<APIProductResource>) aggregates;
Assert.assertTrue(assertAPIProductResourceList(apiProduct.getProductResources(), apiProductResources));
}
Aggregations