Search in sources :

Example 1 with ClientExceptionType

use of org.apache.syncope.common.lib.types.ClientExceptionType in project syncope by apache.

the class SCIMExceptionMapper method processInvalidEntityExceptions.

private ResponseBuilder processInvalidEntityExceptions(final Exception ex) {
    InvalidEntityException iee = null;
    if (ex instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex;
    }
    if (ex instanceof TransactionSystemException && ROLLBACK_EXCLASS.isAssignableFrom(ex.getCause().getClass()) && 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;
            }
        }
        StringBuilder msg = new StringBuilder();
        for (Map.Entry<Class<?>, Set<EntityViolationType>> violation : iee.getViolations().entrySet()) {
            for (EntityViolationType violationType : violation.getValue()) {
                msg.append(violationType.name()).append(": ").append(violationType.getMessage()).append('\n');
            }
        }
        return builder(exType, msg.toString());
    }
    return null;
}
Also used : Set(java.util.Set) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) TransactionSystemException(org.springframework.transaction.TransactionSystemException) Map(java.util.Map) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) EntityViolationType(org.apache.syncope.common.lib.types.EntityViolationType)

Example 2 with ClientExceptionType

use of org.apache.syncope.common.lib.types.ClientExceptionType in project syncope by apache.

the class TemplateUtils method check.

public void check(final Map<String, AnyTO> templates, final ClientExceptionType clientExceptionType) {
    SyncopeClientException sce = SyncopeClientException.build(clientExceptionType);
    templates.values().forEach(value -> {
        value.getPlainAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty() && !JexlUtils.isExpressionValid(attrTO.getValues().get(0))).forEachOrdered(attrTO -> {
            sce.getElements().add("Invalid JEXL: " + attrTO.getValues().get(0));
        });
        value.getVirAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty() && !JexlUtils.isExpressionValid(attrTO.getValues().get(0))).forEachOrdered((attrTO) -> {
            sce.getElements().add("Invalid JEXL: " + attrTO.getValues().get(0));
        });
        if (value instanceof UserTO) {
            UserTO template = (UserTO) value;
            if (StringUtils.isNotBlank(template.getUsername()) && !JexlUtils.isExpressionValid(template.getUsername())) {
                sce.getElements().add("Invalid JEXL: " + template.getUsername());
            }
            if (StringUtils.isNotBlank(template.getPassword()) && !JexlUtils.isExpressionValid(template.getPassword())) {
                sce.getElements().add("Invalid JEXL: " + template.getPassword());
            }
        } else if (value instanceof GroupTO) {
            GroupTO template = (GroupTO) value;
            if (StringUtils.isNotBlank(template.getName()) && !JexlUtils.isExpressionValid(template.getName())) {
                sce.getElements().add("Invalid JEXL: " + template.getName());
            }
        }
    });
    if (!sce.isEmpty()) {
        throw sce;
    }
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) AnyTO(org.apache.syncope.common.lib.to.AnyTO) User(org.apache.syncope.core.persistence.api.entity.user.User) Autowired(org.springframework.beans.factory.annotation.Autowired) MapContext(org.apache.commons.jexl3.MapContext) GroupTO(org.apache.syncope.common.lib.to.GroupTO) StringUtils(org.apache.commons.lang3.StringUtils) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) Component(org.springframework.stereotype.Component) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Map(java.util.Map) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyTemplate(org.apache.syncope.core.persistence.api.entity.AnyTemplate) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) Transactional(org.springframework.transaction.annotation.Transactional) UserTO(org.apache.syncope.common.lib.to.UserTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) GroupTO(org.apache.syncope.common.lib.to.GroupTO)

Example 3 with ClientExceptionType

use of org.apache.syncope.common.lib.types.ClientExceptionType 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 4 with ClientExceptionType

use of org.apache.syncope.common.lib.types.ClientExceptionType in project syncope by apache.

the class RestClientExceptionMapper method checkSyncopeClientCompositeException.

private SyncopeClientCompositeException checkSyncopeClientCompositeException(final Response response) {
    SyncopeClientCompositeException compException = SyncopeClientException.buildComposite();
    // Attempts to read ErrorTO or List<ErrorTO> as entity...
    List<ErrorTO> errors = null;
    try {
        ErrorTO error = response.readEntity(ErrorTO.class);
        if (error != null) {
            errors = Collections.singletonList(error);
        }
    } catch (Exception e) {
        LOG.debug("Could not read {}, attempting to read composite...", ErrorTO.class.getName(), e);
    }
    if (errors == null) {
        try {
            errors = response.readEntity(new GenericType<List<ErrorTO>>() {
            });
        } catch (Exception e) {
            LOG.debug("Could not read {} list, attempting to read headers...", ErrorTO.class.getName(), e);
        }
    }
    // ...if not possible, attempts to parse response headers
    if (errors == null) {
        List<String> exTypesInHeaders = response.getStringHeaders().get(RESTHeaders.ERROR_CODE);
        if (exTypesInHeaders == null) {
            LOG.debug("No " + RESTHeaders.ERROR_CODE + " provided");
            return null;
        }
        List<String> exInfos = response.getStringHeaders().get(RESTHeaders.ERROR_INFO);
        Set<String> handledExceptions = new HashSet<>();
        exTypesInHeaders.forEach(exTypeAsString -> {
            ClientExceptionType exceptionType = null;
            try {
                exceptionType = ClientExceptionType.fromHeaderValue(exTypeAsString);
            } catch (IllegalArgumentException e) {
                LOG.error("Unexpected value of " + RESTHeaders.ERROR_CODE + ": " + exTypeAsString, e);
            }
            if (exceptionType != null) {
                handledExceptions.add(exTypeAsString);
                SyncopeClientException clientException = SyncopeClientException.build(exceptionType);
                if (exInfos != null && !exInfos.isEmpty()) {
                    for (String element : exInfos) {
                        if (element.startsWith(exceptionType.name())) {
                            clientException.getElements().add(StringUtils.substringAfter(element, ":"));
                        }
                    }
                }
                compException.addException(clientException);
            }
        });
        exTypesInHeaders.removeAll(handledExceptions);
        if (!exTypesInHeaders.isEmpty()) {
            LOG.error("Unmanaged exceptions: " + exTypesInHeaders);
        }
    } else {
        for (ErrorTO error : errors) {
            SyncopeClientException clientException = SyncopeClientException.build(error.getType());
            clientException.getElements().addAll(error.getElements());
            compException.addException(clientException);
        }
    }
    if (compException.hasExceptions()) {
        return compException;
    }
    return null;
}
Also used : SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) GenericType(javax.ws.rs.core.GenericType) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ForbiddenException(javax.ws.rs.ForbiddenException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) WebServiceException(javax.xml.ws.WebServiceException) AccessControlException(java.security.AccessControlException) BadRequestException(javax.ws.rs.BadRequestException) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) HashSet(java.util.HashSet)

Aggregations

ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)4 Map (java.util.Map)3 Set (java.util.Set)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 ErrorTO (org.apache.syncope.common.lib.to.ErrorTO)2 EntityViolationType (org.apache.syncope.common.lib.types.EntityViolationType)2 InvalidEntityException (org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 AccessControlException (java.security.AccessControlException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 RollbackException (javax.persistence.RollbackException)1 BadRequestException (javax.ws.rs.BadRequestException)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 GenericType (javax.ws.rs.core.GenericType)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 WebServiceException (javax.xml.ws.WebServiceException)1 MapContext (org.apache.commons.jexl3.MapContext)1 StringUtils (org.apache.commons.lang3.StringUtils)1