Java Code Examples for javax.validation.ConstraintViolation#getRootBean()

The following examples show how to use javax.validation.ConstraintViolation#getRootBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstraintViolationExceptionMapper.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
	LOGGER.warn("a ConstraintViolationException occured", cve);

	List<ErrorData> errors = new ArrayList<>();
	for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {

		ErrorDataBuilder builder = ErrorData.builder();
		builder = builder.addMetaField(META_TYPE_KEY, META_TYPE_VALUE);
		builder = builder.setStatus(String.valueOf(HttpStatus.UNPROCESSABLE_ENTITY_422));
		builder = builder.setDetail(violation.getMessage());

		builder = builder.setCode(toCode(violation));
		if (violation.getMessageTemplate() != null) {
			builder = builder.addMetaField(META_MESSAGE_TEMPLATE, violation.getMessageTemplate());
		}

		// for now we just provide root resource validation information
		// depending on bulk update spec, we might also provide the leaf information in the future
		if (violation.getRootBean() != null) {
			ResourceRef resourceRef = resolvePath(violation);
			builder = builder.addMetaField(META_RESOURCE_ID, resourceRef.getRootResourceId());
			builder = builder.addMetaField(META_RESOURCE_TYPE, resourceRef.getRootResourceType());
			builder = builder.setSourcePointer(resourceRef.getRootSourcePointer());
		}

		ErrorData error = builder.build();
		errors.add(error);
	}

	return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setErrorData(errors).build();
}
 
Example 2
Source File: ConstraintViolationExceptionMapper.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Translate validated bean and root path into validated resource and
 * resource path. For example, embeddables belonging to an entity document
 * are mapped back to an entity violation and a proper path to the
 * embeddable attribute.
 *
 * @param violation to compute the reference
 * @return computaed reference
 */
private ResourceRef resolvePath(ConstraintViolation<?> violation) {
	Object resource = violation.getRootBean();

	Object nodeObject = resource;
	ResourceRef ref = new ResourceRef(resource);

	Iterator<Node> iterator = violation.getPropertyPath().iterator();
	while (iterator.hasNext()) {
		Node node = iterator.next();

		// ignore methods/parameters
		if (node.getKind() == ElementKind.METHOD) {
			continue;
		}
		if (node.getKind() == ElementKind.PARAMETER) {
			resource = getParameterValue(node);
			nodeObject = resource;
			ref = new ResourceRef(resource);
			assertResource(resource);
			continue;
		}

		// visit list, set, map references
		nodeObject = ref.getNodeReference(nodeObject, node);
		ref.visitNode(nodeObject);

		// visit property
		nodeObject = ref.visitProperty(nodeObject, node);
	}

	return ref;
}