Search in sources :

Example 6 with APIProductDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.

the class APIControllerUtil method handleMutualSslCertificates.

/**
 * This method will be used to generate ClientCertificates and meta information related to client certs.
 *
 * @param envParams             Env params object with required parameters
 * @param importedApiDto        Imported API DTO (this will be null for API Products)
 * @param importedApiProductDto Imported API Product DTO (this will be null for APIs)
 * @param identifier            API Identifier/API Product Identifier of the imported API/API Product
 * @param pathToArchive         String of the archive project
 * @throws APIManagementException If an error while generating client certificate information
 */
private static void handleMutualSslCertificates(JsonObject envParams, APIDTO importedApiDto, APIProductDTO importedApiProductDto, Identifier identifier, String pathToArchive) throws APIManagementException {
    JsonElement clientCertificates = envParams.get(ImportExportConstants.MUTUAL_SSL_CERTIFICATES_FIELD);
    if (clientCertificates != null) {
        try {
            List<String> apiSecurity = (importedApiDto != null) ? importedApiDto.getSecurityScheme() : importedApiProductDto.getSecurityScheme();
            if (!apiSecurity.isEmpty()) {
                if (!apiSecurity.contains(ImportExportConstants.MUTUAL_SSL_ENABLED)) {
                    // if the apiSecurity field does not have mutualssl type, append it
                    apiSecurity.add(ImportExportConstants.MUTUAL_SSL_ENABLED);
                }
            } else {
                // if the apiSecurity field is empty, assign the value as "mutualssl"
                apiSecurity.add(ImportExportConstants.MUTUAL_SSL_ENABLED);
            }
            if (importedApiDto != null) {
                importedApiDto.securityScheme(apiSecurity);
            } else {
                importedApiProductDto.securityScheme(apiSecurity);
            }
            String jsonString = clientCertificates.toString();
            handleClientCertificates(new JsonParser().parse(jsonString).getAsJsonArray(), identifier, pathToArchive);
        } catch (IOException e) {
            // Error is logged and when generating certificate details and certs in the archive
            String errorMessage = "Error while generating meta information of client certificates from path.";
            throw new APIManagementException(errorMessage, e, ExceptionCodes.ERROR_READING_PARAMS_FILE);
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 7 with APIProductDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO 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 8 with APIProductDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO 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 9 with APIProductDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO 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 10 with APIProductDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method restoreAPIProductRevision.

@Override
public Response restoreAPIProductRevision(String apiProductId, String revisionId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    apiProvider.restoreAPIProductRevision(apiProductId, revisionId, organization);
    APIProductDTO apiToReturn = getAPIProductByID(apiProductId, apiProvider);
    Response.Status status = Response.Status.CREATED;
    return Response.status(status).entity(apiToReturn).build();
}
Also used : Response(javax.ws.rs.core.Response) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)12 APIProductDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO)9 ArrayList (java.util.ArrayList)7 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 API (org.wso2.carbon.apimgt.api.model.API)7 ProductAPIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO)7 JsonElement (com.google.gson.JsonElement)5 File (java.io.File)5 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)5 Gson (com.google.gson.Gson)4 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)4 Tier (org.wso2.carbon.apimgt.api.model.Tier)4 IOException (java.io.IOException)3 List (java.util.List)3 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)3 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)3 APIOperationsDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)3 JsonObject (com.google.gson.JsonObject)2 LinkedHashSet (java.util.LinkedHashSet)2