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