use of org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException in project carbon-apimgt by wso2.
the class GlobalThrowableMapper method toResponse.
@Override
public Response toResponse(Throwable e) {
if (e instanceof ClientErrorException) {
log.error("Client error", e);
return ((ClientErrorException) e).getResponse();
}
if (e instanceof NotFoundException) {
log.error("Resource not found", e);
return ((NotFoundException) e).getResponse();
}
if (e instanceof PreconditionFailedException) {
log.error("Precondition failed", e);
return ((PreconditionFailedException) e).getResponse();
}
if (e instanceof BadRequestException) {
log.error("Bad request", e);
return ((BadRequestException) e).getResponse();
}
if (e instanceof ConstraintViolationException) {
log.error("Constraint violation", e);
return ((ConstraintViolationException) e).getResponse();
}
if (e instanceof ForbiddenException) {
log.error("Resource forbidden", e);
return ((ForbiddenException) e).getResponse();
}
if (e instanceof ConflictException) {
log.error("Conflict", e);
return ((ConflictException) e).getResponse();
}
if (e instanceof MethodNotAllowedException) {
log.error("Method not allowed", e);
return ((MethodNotAllowedException) e).getResponse();
}
if (e instanceof InternalServerErrorException) {
String errorMessage = "The server encountered an internal error : " + e.getMessage();
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
}
if (e instanceof JsonParseException) {
String errorMessage = "Malformed request body.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
if (e instanceof JsonMappingException) {
if (e instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException unrecognizedPropertyException = (UnrecognizedPropertyException) e;
String unrecognizedProperty = unrecognizedPropertyException.getPropertyName();
String errorMessage = "Unrecognized property '" + unrecognizedProperty + "'";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
} else {
String errorMessage = "One or more request body parameters contain disallowed values.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
}
if (e instanceof AuthenticationException) {
ErrorDTO errorDetail = new ErrorDTO();
errorDetail.setCode((long) 401);
errorDetail.setMoreInfo("");
errorDetail.setMessage("");
errorDetail.setDescription(e.getMessage());
return Response.status(Response.Status.UNAUTHORIZED).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDetail).build();
}
// This occurs when received an empty body in an occasion where the body is mandatory
if (e instanceof EOFException) {
String errorMessage = "Request payload cannot be empty.";
log.error(errorMessage, e);
// noinspection ThrowableResultOfMethodCallIgnored
return RestApiUtil.buildBadRequestException(errorMessage).getResponse();
}
if (e instanceof APIManagementException) {
ErrorHandler selectedErrorHandler = null;
List<Throwable> throwableList = ExceptionUtils.getThrowableList(e);
for (Throwable t : throwableList) {
if (t instanceof APIManagementException) {
APIManagementException apimException = (APIManagementException) t;
ErrorHandler errorHandler = apimException.getErrorHandler();
if (errorHandler != null) {
if (selectedErrorHandler == null) {
selectedErrorHandler = errorHandler;
} else {
selectedErrorHandler = errorHandler.getHttpStatusCode() < selectedErrorHandler.getHttpStatusCode() && errorHandler.getHttpStatusCode() > 0 ? errorHandler : selectedErrorHandler;
}
}
}
}
if (selectedErrorHandler != null) {
// logs the error as the error may be not logged by the origin
if (selectedErrorHandler.printStackTrace()) {
log.error("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
} else {
// Not to log the stack trace due to error code was mark as not print stacktrace.
log.error(e.getMessage());
if (log.isDebugEnabled()) {
log.debug("A defined exception has been captured and mapped to an HTTP response " + "by the global exception mapper ", e);
}
}
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(selectedErrorHandler);
return Response.status(Response.Status.fromStatusCode(selectedErrorHandler.getHttpStatusCode())).type(MediaType.APPLICATION_JSON_TYPE).entity(errorDTO).build();
}
}
// unknown exception log and return
log.error("An unknown exception has been captured by the global exception mapper.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(e500).build();
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException in project carbon-apimgt by wso2.
the class RestApiUtil method handleAuthorizationFailure.
/**
* Logs the error, builds a ForbiddenException with specified details and throws it
*
* @param description description of the error
* @param log Log instance
* @throws ForbiddenException
*/
public static void handleAuthorizationFailure(String description, Log log) throws ForbiddenException {
ForbiddenException forbiddenException = buildForbiddenException(description);
log.error(description);
throw forbiddenException;
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException in project carbon-apimgt by wso2.
the class RestApiUtilTest method testHandleAuthorizationFailureArg.
@Test
public void testHandleAuthorizationFailureArg() {
String apiId = "testapiid_4567ui456789";
String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API + " " + "with Id " + apiId;
APIManagementException apiManagementException = new APIManagementException("API management exception test");
Log log = Mockito.mock(Log.class);
PowerMockito.mockStatic(LogFactory.class);
PowerMockito.when(LogFactory.getLog(Mockito.any(Class.class))).thenReturn(log);
try {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, apiManagementException, log);
} catch (ForbiddenException exception) {
Assert.assertEquals(expectedErrormessage, exception.getMessage());
Mockito.verify(log).error(expectedErrormessage, apiManagementException);
}
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException in project carbon-apimgt by wso2.
the class RestApiUtilTest method testbuildForbiddenException.
@Test
public void testbuildForbiddenException() {
String apiId = "TesT_API_ID_45678";
String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API + " " + "with Id " + apiId;
ForbiddenException forbiddenException = RestApiUtil.buildForbiddenException(RestApiConstants.RESOURCE_API, apiId);
Assert.assertEquals(expectedErrormessage, forbiddenException.getMessage());
}
use of org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException in project carbon-apimgt by wso2.
the class RestApiUtilTest method testbuildForbiddenExceptionWithEmptyID.
@Test
public void testbuildForbiddenExceptionWithEmptyID() {
String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API;
ForbiddenException forbiddenException = RestApiUtil.buildForbiddenException(RestApiConstants.RESOURCE_API, "");
Assert.assertEquals(expectedErrormessage, forbiddenException.getMessage());
}
Aggregations