Search in sources :

Example 51 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class CertificateRestApiUtils method preValidateClientCertificate.

/**
 * To pre validate client certificate given for an alias
 *
 * @param alias Alias of the certificate.
 * @param apiIdentifier Identifier of the API.
 * @param organization Identifier of the organization.
 * @return Client certificate
 * @throws APIManagementException API Management Exception.
 */
public static ClientCertificateDTO preValidateClientCertificate(String alias, APIIdentifier apiIdentifier, String organization) throws APIManagementException {
    int tenantId = APIUtil.getInternalOrganizationId(organization);
    if (StringUtils.isEmpty(alias)) {
        throw new APIManagementException("The alias cannot be empty", ExceptionCodes.ALIAS_CANNOT_BE_EMPTY);
    }
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    ClientCertificateDTO clientCertificate = apiProvider.getClientCertificate(tenantId, alias, apiIdentifier, organization);
    if (clientCertificate == null) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Could not find a client certificate in truststore which belongs to " + "tenant : %d and with alias : %s. Hence the operation is terminated.", tenantId, alias));
        }
        String message = "Certificate for alias '" + alias + "' is not found.";
        throw new APIMgtResourceNotFoundException(message);
    }
    return clientCertificate;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 52 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method updateAPIProductDeployment.

@Override
public Response updateAPIProductDeployment(String apiProductId, String deploymentId, APIRevisionDeploymentDTO apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String revisionId = apIRevisionDeploymentDTO.getRevisionUuid();
    String decodedDeploymentName;
    if (deploymentId != null) {
        try {
            decodedDeploymentName = new String(Base64.getUrlDecoder().decode(deploymentId), StandardCharsets.UTF_8);
        } catch (IllegalArgumentException e) {
            throw new APIMgtResourceNotFoundException("deployment with " + deploymentId + " not found", ExceptionCodes.from(ExceptionCodes.EXISTING_DEPLOYMENT_NOT_FOUND, deploymentId));
        }
    } else {
        throw new APIMgtResourceNotFoundException("deployment id not found", ExceptionCodes.from(ExceptionCodes.DEPLOYMENT_ID_NOT_FOUND));
    }
    APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
    apiRevisionDeployment.setRevisionUUID(revisionId);
    apiRevisionDeployment.setDeployment(decodedDeploymentName);
    apiRevisionDeployment.setVhost(apIRevisionDeploymentDTO.getVhost());
    apiRevisionDeployment.setDisplayOnDevportal(apIRevisionDeploymentDTO.isDisplayOnDevportal());
    apiProvider.updateAPIProductDisplayOnDevportal(apiProductId, revisionId, apiRevisionDeployment);
    APIRevisionDeployment apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeployment(decodedDeploymentName, revisionId);
    APIRevisionDeploymentDTO apiRevisionDeploymentDTO = APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeploymentsResponse);
    Response.Status status = Response.Status.OK;
    return Response.status(status).entity(apiRevisionDeploymentDTO).build();
}
Also used : Response(javax.ws.rs.core.Response) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIRevisionDeploymentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDeploymentDTO) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 53 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method createNewAPIVersion.

@Override
public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException {
    URI newVersionedApiUri;
    APIDTO newVersionedApi = new APIDTO();
    ServiceEntry service = new ServiceEntry();
    try {
        APIIdentifier apiIdentifierFromTable = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
        if (apiIdentifierFromTable == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        int tenantId = APIUtil.getInternalOrganizationId(organization);
        API existingAPI = apiProvider.getAPIbyUUID(apiId, organization);
        if (existingAPI == null) {
            throw new APIMgtResourceNotFoundException("API not found for id " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        if (newVersion.equals(existingAPI.getId().getVersion())) {
            throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName(), ExceptionCodes.from(API_VERSION_ALREADY_EXISTS, newVersion, existingAPI.getId().getApiName()));
        }
        if (StringUtils.isNotEmpty(serviceVersion)) {
            String serviceName = existingAPI.getServiceInfo("name");
            ServiceCatalogImpl serviceCatalog = new ServiceCatalogImpl();
            service = serviceCatalog.getServiceByNameAndVersion(serviceName, serviceVersion, tenantId);
            if (service == null) {
                throw new APIManagementException("No matching service version found", ExceptionCodes.SERVICE_VERSION_NOT_FOUND);
            }
        }
        if (StringUtils.isNotEmpty(serviceVersion) && !serviceVersion.equals(existingAPI.getServiceInfo("version"))) {
            APIDTO apidto = createAPIDTO(existingAPI, newVersion);
            if (ServiceEntry.DefinitionType.OAS2.equals(service.getDefinitionType()) || ServiceEntry.DefinitionType.OAS3.equals(service.getDefinitionType())) {
                newVersionedApi = importOpenAPIDefinition(service.getEndpointDef(), null, null, apidto, null, service, organization);
            } else if (ServiceEntry.DefinitionType.ASYNC_API.equals(service.getDefinitionType())) {
                newVersionedApi = importAsyncAPISpecification(service.getEndpointDef(), null, apidto, null, service, organization);
            }
        } else {
            API versionedAPI = apiProvider.createNewAPIVersion(apiId, newVersion, defaultVersion, organization);
            newVersionedApi = APIMappingUtil.fromAPItoDTO(versionedAPI);
        }
        // This URI used to set the location header of the POST response
        newVersionedApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + newVersionedApi.getId());
        return Response.created(newVersionedApiUri).entity(newVersionedApi).build();
    } catch (APIManagementException e) {
        if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while copying API : " + apiId, e, log);
        } else {
            throw e;
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving API location of " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) URISyntaxException(java.net.URISyntaxException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) URI(java.net.URI) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ServiceCatalogImpl(org.wso2.carbon.apimgt.impl.ServiceCatalogImpl)

Example 54 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getOperationPolicyForAPIByPolicyId.

/**
 * Get the API specific operation policy specification by providing the policy ID
 *
 * @param apiId             API UUID
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return Operation policy DTO as response
 */
@Override
public Response getOperationPolicyForAPIByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate whether api exists or not
        validateAPIExistence(apiId);
        OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyId(operationPolicyId, apiId, organization, false);
        if (existingPolicy != null) {
            OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
            return Response.ok().entity(policyDataDTO).build();
        } else {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing operation policy with ID: " + operationPolicyId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
        } else {
            String errorMessage = "Error while getting an API specific operation policy with ID :" + operationPolicyId + " for API " + apiId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An Error has occurred while getting the operation policy with ID " + operationPolicyId + "operation policy for API " + apiId, e, log);
    }
    return null;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) JSONException(org.json.JSONException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) CryptoException(org.wso2.carbon.core.util.CryptoException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException)

Example 55 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method validateDocument.

@Override
public Response validateDocument(String apiId, String name, String ifMatch, MessageContext messageContext) {
    if (StringUtils.isEmpty(name) || StringUtils.isEmpty(apiId)) {
        RestApiUtil.handleBadRequest("API Id and/ or document name should not be empty", log);
    }
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
        if (apiIdentifier == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        return apiProvider.isDocumentationExist(apiId, name, organization) ? Response.status(Response.Status.OK).build() : Response.status(Response.Status.NOT_FOUND).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while checking the api existence", e, log);
    }
    return Response.status(Response.Status.NOT_FOUND).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)48 API (org.wso2.carbon.apimgt.api.model.API)22 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)22 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)22 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)21 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)21 HashMap (java.util.HashMap)19 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)19 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)17 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)12 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)11 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)11 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)10 ParseException (org.json.simple.parser.ParseException)9 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)9 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)8