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;
}
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;
}
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));
}
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;
}
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;
}
Aggregations