Search in sources :

Example 1 with ErrorTO

use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.

the class RestServiceExceptionMapper method toResponse.

@Override
public Response toResponse(final Exception ex) {
    LOG.error("Exception thrown", ex);
    ResponseBuilder builder;
    if (ex instanceof AccessDeniedException) {
        // leaves the default exception processing to Spring Security
        builder = null;
    } else if (ex instanceof SyncopeClientException) {
        SyncopeClientException sce = (SyncopeClientException) ex;
        builder = sce.isComposite() ? getSyncopeClientCompositeExceptionResponse(sce.asComposite()) : getSyncopeClientExceptionResponse(sce);
    } else if (ex instanceof DelegatedAdministrationException || ExceptionUtils.getRootCause(ex) instanceof DelegatedAdministrationException) {
        builder = builder(ClientExceptionType.DelegatedAdministration, ExceptionUtils.getRootCauseMessage(ex));
    } else if (ex instanceof EntityExistsException || ex instanceof DuplicateException || ex instanceof PersistenceException && ex.getCause() instanceof EntityExistsException) {
        builder = builder(ClientExceptionType.EntityExists, getJPAMessage(ex instanceof PersistenceException ? ex.getCause() : ex));
    } else if (ex instanceof DataIntegrityViolationException || ex instanceof JpaSystemException) {
        builder = builder(ClientExceptionType.DataIntegrityViolation, getJPAMessage(ex));
    } else if (ex instanceof ConnectorException) {
        builder = builder(ClientExceptionType.ConnectorException, ExceptionUtils.getRootCauseMessage(ex));
    } else if (ex instanceof NotFoundException) {
        builder = builder(ClientExceptionType.NotFound, ExceptionUtils.getRootCauseMessage(ex));
    } else {
        builder = processInvalidEntityExceptions(ex);
        if (builder == null) {
            builder = processBadRequestExceptions(ex);
        }
        // process JAX-RS validation errors
        if (builder == null && ex instanceof ValidationException) {
            builder = builder(validationEM.toResponse((ValidationException) ex)).header(RESTHeaders.ERROR_CODE, ClientExceptionType.RESTValidation.name()).header(RESTHeaders.ERROR_INFO, ClientExceptionType.RESTValidation.getInfoHeaderValue(ExceptionUtils.getRootCauseMessage(ex)));
            ErrorTO error = new ErrorTO();
            error.setStatus(ClientExceptionType.RESTValidation.getResponseStatus().getStatusCode());
            error.setType(ClientExceptionType.RESTValidation);
            error.getElements().add(ExceptionUtils.getRootCauseMessage(ex));
            builder.entity(error);
        }
        // ...or just report as InternalServerError
        if (builder == null) {
            builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR).header(RESTHeaders.ERROR_INFO, ClientExceptionType.Unknown.getInfoHeaderValue(ExceptionUtils.getRootCauseMessage(ex)));
            ErrorTO error = new ErrorTO();
            error.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            error.setType(ClientExceptionType.Unknown);
            error.getElements().add(ExceptionUtils.getRootCauseMessage(ex));
            builder.entity(error);
        }
    }
    return builder == null ? null : builder.build();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(javax.validation.ValidationException) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) EntityExistsException(javax.persistence.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) JpaSystemException(org.springframework.orm.jpa.JpaSystemException) ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) DuplicateException(org.apache.syncope.core.persistence.api.dao.DuplicateException) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException) PersistenceException(javax.persistence.PersistenceException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 2 with ErrorTO

use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.

the class RestServiceExceptionMapper method getSyncopeClientExceptionResponse.

private ResponseBuilder getSyncopeClientExceptionResponse(final SyncopeClientException ex) {
    ResponseBuilder builder = Response.status(ex.getType().getResponseStatus());
    builder.header(RESTHeaders.ERROR_CODE, ex.getType().name());
    ErrorTO error = new ErrorTO();
    error.setStatus(ex.getType().getResponseStatus().getStatusCode());
    error.setType(ex.getType());
    for (String element : ex.getElements()) {
        builder.header(RESTHeaders.ERROR_INFO, ex.getType().getInfoHeaderValue(element));
        error.getElements().add(element);
    }
    return builder.entity(error);
}
Also used : ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 3 with ErrorTO

use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.

the class RestServiceExceptionMapper method getSyncopeClientCompositeExceptionResponse.

private ResponseBuilder getSyncopeClientCompositeExceptionResponse(final SyncopeClientCompositeException ex) {
    if (ex.getExceptions().size() == 1) {
        return getSyncopeClientExceptionResponse(ex.getExceptions().iterator().next());
    }
    ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
    List<ErrorTO> errors = new ArrayList<>();
    for (SyncopeClientException sce : ex.getExceptions()) {
        builder.header(RESTHeaders.ERROR_CODE, sce.getType().name());
        ErrorTO error = new ErrorTO();
        error.setStatus(sce.getType().getResponseStatus().getStatusCode());
        error.setType(sce.getType());
        for (String element : sce.getElements()) {
            builder.header(RESTHeaders.ERROR_INFO, sce.getType().getInfoHeaderValue(element));
            error.getElements().add(element);
        }
        errors.add(error);
    }
    return builder.entity(errors);
}
Also used : ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) ArrayList(java.util.ArrayList) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 4 with ErrorTO

use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.

the class RestServiceExceptionMapper method processInvalidEntityExceptions.

private ResponseBuilder processInvalidEntityExceptions(final Exception ex) {
    InvalidEntityException iee = null;
    if (ex instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex;
    }
    if (ex instanceof TransactionSystemException && ex.getCause() instanceof RollbackException && ex.getCause().getCause() instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex.getCause().getCause();
    }
    if (iee != null) {
        ClientExceptionType exType;
        if (iee.getEntityClassSimpleName().endsWith("Policy")) {
            exType = ClientExceptionType.InvalidPolicy;
        } else if (iee.getEntityClassSimpleName().equals(PlainAttr.class.getSimpleName())) {
            exType = ClientExceptionType.InvalidValues;
        } else {
            try {
                exType = ClientExceptionType.valueOf("Invalid" + iee.getEntityClassSimpleName());
            } catch (IllegalArgumentException e) {
                // ignore
                exType = ClientExceptionType.InvalidEntity;
            }
        }
        ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
        builder.header(RESTHeaders.ERROR_CODE, exType.name());
        ErrorTO error = new ErrorTO();
        error.setStatus(exType.getResponseStatus().getStatusCode());
        error.setType(exType);
        for (Map.Entry<Class<?>, Set<EntityViolationType>> violation : iee.getViolations().entrySet()) {
            for (EntityViolationType violationType : violation.getValue()) {
                builder.header(RESTHeaders.ERROR_INFO, exType.getInfoHeaderValue(violationType.name() + ": " + violationType.getMessage()));
                error.getElements().add(violationType.name() + ": " + violationType.getMessage());
            }
        }
        return builder;
    }
    return null;
}
Also used : ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) Set(java.util.Set) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashMap(java.util.HashMap) Map(java.util.Map) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) EntityViolationType(org.apache.syncope.common.lib.types.EntityViolationType)

Example 5 with ErrorTO

use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.

the class RestServiceExceptionMapper method builder.

private ResponseBuilder builder(final ClientExceptionType hType, final String msg) {
    ResponseBuilder builder = Response.status(hType.getResponseStatus()).header(RESTHeaders.ERROR_CODE, hType.name()).header(RESTHeaders.ERROR_INFO, hType.getInfoHeaderValue(msg));
    ErrorTO error = new ErrorTO();
    error.setStatus(hType.getResponseStatus().getStatusCode());
    error.setType(hType);
    error.getElements().add(msg);
    return builder.entity(error);
}
Also used : ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

ErrorTO (org.apache.syncope.common.lib.to.ErrorTO)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)2 AccessControlException (java.security.AccessControlException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 EntityExistsException (javax.persistence.EntityExistsException)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1 ValidationException (javax.validation.ValidationException)1 BadRequestException (javax.ws.rs.BadRequestException)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 GenericType (javax.ws.rs.core.GenericType)1 WebServiceException (javax.xml.ws.WebServiceException)1 SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)1 EntityViolationType (org.apache.syncope.common.lib.types.EntityViolationType)1