Search in sources :

Example 16 with SyncopeClientCompositeException

use of org.apache.syncope.common.lib.SyncopeClientCompositeException 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)

Example 17 with SyncopeClientCompositeException

use of org.apache.syncope.common.lib.SyncopeClientCompositeException in project syncope by apache.

the class RestClientExceptionMapper method fromResponse.

@Override
public Exception fromResponse(final Response response) {
    int statusCode = response.getStatus();
    String message = response.getHeaderString(RESTHeaders.ERROR_INFO);
    Exception ex;
    SyncopeClientCompositeException scce = checkSyncopeClientCompositeException(response);
    if (scce != null) {
        // 1. Check for client (possibly composite) exception in HTTP header
        ex = scce.getExceptions().size() == 1 ? scce.getExceptions().iterator().next() : scce;
    } else if (statusCode == Response.Status.UNAUTHORIZED.getStatusCode()) {
        // 2. Map SC_UNAUTHORIZED
        ex = new AccessControlException(StringUtils.isBlank(message) ? "Remote unauthorized exception" : message);
    } else if (statusCode == Response.Status.FORBIDDEN.getStatusCode()) {
        // 3. Map SC_FORBIDDEN
        ex = new ForbiddenException(StringUtils.isBlank(message) ? "Remote forbidden exception" : message);
    } else if (statusCode == Response.Status.BAD_REQUEST.getStatusCode()) {
        // 4. Map SC_BAD_REQUEST
        ex = StringUtils.isBlank(message) ? new BadRequestException() : new BadRequestException(message);
    } else {
        // 5. All other codes are mapped to runtime exception with HTTP code information
        ex = new WebServiceException(String.format("Remote exception with status code: %s", Response.Status.fromStatusCode(statusCode).name()));
    }
    LOG.error("Exception thrown", ex);
    return ex;
}
Also used : SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ForbiddenException(javax.ws.rs.ForbiddenException) WebServiceException(javax.xml.ws.WebServiceException) AccessControlException(java.security.AccessControlException) BadRequestException(javax.ws.rs.BadRequestException) 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)

Aggregations

SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)17 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)16 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)9 Realm (org.apache.syncope.core.persistence.api.entity.Realm)9 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)8 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)8 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)8 Collections (java.util.Collections)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 StringUtils (org.apache.commons.lang3.StringUtils)6 ResourceOperation (org.apache.syncope.common.lib.types.ResourceOperation)6 AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)6 Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)6 ParseException (java.text.ParseException)5 Group (org.apache.syncope.core.persistence.api.entity.group.Group)5 Collection (java.util.Collection)4