Search in sources :

Example 46 with APIMgtResourceNotFoundException

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

the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyContentByPolicyId.

/**
 * Download the operation policy specification and definition for a given common operation policy
 *
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return A zip file containing both (if exists) operation policy specification and policy definition
 */
@Override
public Response getCommonOperationPolicyContentByPolicyId(String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        OperationPolicyData policyData = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, true);
        if (policyData != null) {
            File file = RestApiPublisherUtils.exportOperationPolicyData(policyData);
            return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"").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 getting the content of common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while getting the content of the common operation " + " policy with ID " + 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) File(java.io.File) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 47 with APIMgtResourceNotFoundException

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

the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyByPolicyId.

/**
 * Get the common operation policy by providing the policy ID
 *
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return Operation policy DTO as response
 */
@Override
public Response getCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, 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 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 getting the common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while getting the common operation " + " policy with ID: " + 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) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 48 with APIMgtResourceNotFoundException

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

the class RestApiUtilTest method testisDueToResourceAlreadyExistsWithInvalidException.

@Test
public void testisDueToResourceAlreadyExistsWithInvalidException() throws Exception {
    APIMgtResourceNotFoundException apiMgtResourceNotFoundException = new APIMgtResourceNotFoundException("New Sample exception");
    Throwable testThrowable = new Throwable();
    PowerMockito.spy(RestApiUtil.class);
    PowerMockito.doReturn(apiMgtResourceNotFoundException).when(RestApiUtil.class, "getPossibleErrorCause", testThrowable);
    Assert.assertFalse("Invalid exception has been passed.", RestApiUtil.isDueToResourceAlreadyExists(testThrowable));
}
Also used : APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 49 with APIMgtResourceNotFoundException

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

the class RestAPIStoreUtils method isUserAccessAllowedForAPI.

/**
 * Check whether the specified API exists and the current logged in user has access to it.
 * <p>
 * When it tries to retrieve the resource from the registry, it will fail with AuthorizationFailedException if user
 * does not have enough privileges. If the API does not exist, this will throw a APIMgtResourceNotFoundException
 *
 * @param apiId API identifier
 * @throws APIManagementException
 */
public static boolean isUserAccessAllowedForAPI(APIIdentifier apiId) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    // this is just to check whether the user has access to the api or the api exists.
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        String organization = ApiMgtDAO.getInstance().getOrganizationByAPIUUID(apiId.getUUID());
        apiConsumer.getLightweightAPIByUUID(apiId.getUUID(), organization);
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            String message = "user " + username + " failed to access the API " + apiId + " due to an authorization failure";
            log.info(message);
            return false;
        } else {
            // This is an unexpected failure
            String message = "Failed to retrieve the API " + apiId + " to check user " + username + " has access to the API";
            throw new APIManagementException(message, e);
        }
    }
    return true;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 50 with APIMgtResourceNotFoundException

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

the class RestAPIStoreUtils method isUserAccessAllowedForAPIByUUID.

/**
 * Check whether the specified API exists and the current logged in user has access to it.
 * <p>
 * When it tries to retrieve the resource from the registry, it will fail with AuthorizationFailedException if user
 * does not have enough privileges. If the API does not exist, this will throw a APIMgtResourceNotFoundException
 *
 * @param apiId API UUID
 * @param organization Identifier of the organization
 * @throws APIManagementException
 */
public static boolean isUserAccessAllowedForAPIByUUID(String apiId, String organization) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    APIConsumer consumer = RestApiCommonUtil.getLoggedInUserConsumer();
    // this is just to check whether the user has access to the api or the api exists.
    try {
        consumer.getLightweightAPIByUUID(apiId, organization);
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            String message = "user " + username + " failed to access the API " + apiId + " due to an authorization failure";
            log.info(message);
            return false;
        } else {
            // This is an unexpected failure
            String message = "Failed to retrieve the API " + apiId + " to check user " + username + " has access to the API";
            throw new APIManagementException(message, e);
        }
    }
    return true;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

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