Search in sources :

Example 51 with APIProduct

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;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 52 with APIProduct

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;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 53 with APIProduct

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;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 54 with APIProduct

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;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 55 with APIProduct

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));
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) List(java.util.List) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)71 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)52 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)51 API (org.wso2.carbon.apimgt.api.model.API)37 ArrayList (java.util.ArrayList)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)22 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)22 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 HashMap (java.util.HashMap)19 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)19 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)18 JSONObject (org.json.simple.JSONObject)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)16 HashSet (java.util.HashSet)15 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)15 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)14 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)14 ParseException (org.json.simple.parser.ParseException)12