Search in sources :

Example 76 with APIMgtResourceNotFoundException

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

the class ApisApiServiceImpl method updateAPIDeployment.

@Override
public Response updateAPIDeployment(String apiId, String deploymentId, APIRevisionDeploymentDTO apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    // validate if api exists
    APIInfo apiInfo = validateAPIExistence(apiId);
    // validate API update operation permitted based on the LC state
    validateAPIOperationsPerLC(apiInfo.getStatus().toString());
    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.updateAPIDisplayOnDevportal(apiId, 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 : HttpResponse(org.apache.http.HttpResponse) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) Response(javax.ws.rs.core.Response) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) 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 77 with APIMgtResourceNotFoundException

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

the class ApisApiServiceImpl method updateAPIResourcePoliciesByPolicyId.

/**
 * Update the resource policies(inflow/outflow) given the resource id.
 *
 * @param apiId  API ID
 * @param resourcePolicyId resource policy id
 * @param body resource policy content
 * @param ifMatch If-Match header value
 * @return json response of the updated sequence content
 */
@Override
public Response updateAPIResourcePoliciesByPolicyId(String apiId, String resourcePolicyId, ResourcePolicyInfoDTO body, String ifMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider provider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = provider.getAPIbyUUID(apiId, organization);
        if (api == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(api.getStatus());
        if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
            if (StringUtils.isEmpty(resourcePolicyId)) {
                String errorMessage = "Resource id should not be empty to update a resource policy.";
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            boolean isValidSchema = RestApiPublisherUtils.validateXMLSchema(body.getContent());
            if (isValidSchema) {
                List<SOAPToRestSequence> sequence = api.getSoapToRestSequences();
                for (SOAPToRestSequence soapToRestSequence : sequence) {
                    if (soapToRestSequence.getUuid().equals(resourcePolicyId)) {
                        soapToRestSequence.setContent(body.getContent());
                        break;
                    }
                }
                API originalAPI = provider.getAPIbyUUID(apiId, organization);
                provider.updateAPI(api, originalAPI);
                SequenceUtils.updateResourcePolicyFromRegistryResourceId(api.getId(), resourcePolicyId, body.getContent());
                String updatedPolicyContent = SequenceUtils.getResourcePolicyFromRegistryResourceId(api, resourcePolicyId);
                ResourcePolicyInfoDTO resourcePolicyInfoDTO = APIMappingUtil.fromResourcePolicyStrToInfoDTO(updatedPolicyContent);
                return Response.ok().entity(resourcePolicyInfoDTO).build();
            } else {
                String errorMessage = "Error while validating the resource policy xml content for the API : " + apiId;
                RestApiUtil.handleInternalServerError(errorMessage, log);
            }
        } else {
            String errorMessage = "The provided api with id: " + apiId + " is not a soap to rest converted api.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
    } catch (APIManagementException | FaultGatewaysException e) {
        String errorMessage = "Error while retrieving the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 78 with APIMgtResourceNotFoundException

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

the class OperationPoliciesApiServiceImpl method deleteCommonOperationPolicyByPolicyId.

/**
 * Delete a common operation policy by providing the policy ID
 *
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return ok if deleted successfully
 */
@Override
public Response deleteCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
        if (existingPolicy != null) {
            apiProvider.deleteOperationPolicyById(operationPolicyId, organization);
            if (log.isDebugEnabled()) {
                log.debug("The common operation policy " + operationPolicyId + " has been deleted");
            }
            return Response.ok().build();
        } else {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
        } else {
            String errorMessage = "Error while deleting the common operation policy with ID: " + operationPolicyId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An Error has occurred while deleting the common operation policy + " + operationPolicyId, 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 79 with APIMgtResourceNotFoundException

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

the class ApiMgtDAO method getSharedScopeUsage.

/**
 * Get the API and URI usages of the given shared scope
 *
 * @param uuid Id of the shared scope
 * @param tenantId tenant Id
 * @return usgaes ofr the shaerd scope
 * @throws APIManagementException If an error occurs while getting the usage details
 */
public SharedScopeUsage getSharedScopeUsage(String uuid, int tenantId) throws APIManagementException {
    SharedScopeUsage sharedScopeUsage;
    List<API> usedApiList = new ArrayList<>();
    String sharedScopeName = getSharedScopeKeyByUUID(uuid);
    if (sharedScopeName != null) {
        sharedScopeUsage = new SharedScopeUsage();
        sharedScopeUsage.setId(uuid);
        sharedScopeUsage.setName(sharedScopeName);
    } else {
        throw new APIMgtResourceNotFoundException("Shared Scope not found for scope ID: " + uuid, ExceptionCodes.from(ExceptionCodes.SHARED_SCOPE_NOT_FOUND, uuid));
    }
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement psForApiUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_API_USAGE_BY_TENANT)) {
        psForApiUsage.setString(1, uuid);
        psForApiUsage.setInt(2, tenantId);
        try (ResultSet apiUsageResultSet = psForApiUsage.executeQuery()) {
            while (apiUsageResultSet.next()) {
                String provider = apiUsageResultSet.getString("API_PROVIDER");
                String apiName = apiUsageResultSet.getString("API_NAME");
                String version = apiUsageResultSet.getString("API_VERSION");
                APIIdentifier apiIdentifier = new APIIdentifier(provider, apiName, version);
                API usedApi = new API(apiIdentifier);
                usedApi.setContext(apiUsageResultSet.getString("CONTEXT"));
                try (PreparedStatement psForUriUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_URI_USAGE_BY_TENANT)) {
                    int apiId = apiUsageResultSet.getInt("API_ID");
                    Set<URITemplate> usedUriTemplates = new LinkedHashSet<>();
                    psForUriUsage.setString(1, uuid);
                    psForUriUsage.setInt(2, tenantId);
                    psForUriUsage.setInt(3, apiId);
                    try (ResultSet uriUsageResultSet = psForUriUsage.executeQuery()) {
                        while (uriUsageResultSet.next()) {
                            URITemplate usedUriTemplate = new URITemplate();
                            usedUriTemplate.setUriTemplate(uriUsageResultSet.getString("URL_PATTERN"));
                            usedUriTemplate.setHTTPVerb(uriUsageResultSet.getString("HTTP_METHOD"));
                            usedUriTemplates.add(usedUriTemplate);
                        }
                    }
                    usedApi.setUriTemplates(usedUriTemplates);
                    usedApiList.add(usedApi);
                } catch (SQLException e) {
                    handleException("Failed to retrieve Resource usages of shared scope with scope ID " + uuid, e);
                }
            }
        }
        if (sharedScopeUsage != null) {
            sharedScopeUsage.setApis(usedApiList);
        }
        return sharedScopeUsage;
    } catch (SQLException e) {
        handleException("Failed to retrieve API usages of shared scope with scope ID" + uuid, e);
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) SharedScopeUsage(org.wso2.carbon.apimgt.api.model.SharedScopeUsage) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 80 with APIMgtResourceNotFoundException

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

the class ImportUtils method retrieveApiToOverwrite.

/**
 * This method retrieves an API to overwrite in the current tenant domain.
 *
 * @param apiName             API Name
 * @param apiVersion          API Version
 * @param currentTenantDomain Current tenant domain
 * @param apiProvider         API Provider
 * @param ignoreAndImport     This should be true if the exception should be ignored
 * @param organization        Organization
 * @throws APIManagementException If an error occurs when retrieving the API to overwrite
 */
private static API retrieveApiToOverwrite(String apiName, String apiVersion, String currentTenantDomain, APIProvider apiProvider, Boolean ignoreAndImport, String organization) throws APIManagementException {
    String provider = APIUtil.getAPIProviderFromAPINameVersionTenant(apiName, apiVersion, currentTenantDomain);
    APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(provider), apiName, apiVersion);
    // Checking whether the API exists
    if (!apiProvider.isAPIAvailable(apiIdentifier, organization)) {
        if (ignoreAndImport) {
            return null;
        }
        throw new APIMgtResourceNotFoundException("Error occurred while retrieving the API. API: " + apiName + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiVersion + " not found", ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion()));
    }
    String uuid = APIUtil.getUUIDFromIdentifier(apiIdentifier, organization);
    return apiProvider.getAPIbyUUID(uuid, currentTenantDomain);
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

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