use of org.glassfish.jersey.server.validation.ValidationError in project jersey by jersey.
the class CombinedFeedResource method notFoundException.
private NotFoundException notFoundException(String feedId) {
String message = "No Combined Feed was found with ID: " + feedId;
Response response = Response.status(NOT_FOUND).entity(new ValidationError(message, null, null, feedId)).build();
return new NotFoundException(message, response);
}
use of org.glassfish.jersey.server.validation.ValidationError in project jersey by jersey.
the class ValidationErrorMessageBodyWriter method writeTo.
@Override
public void writeTo(final Object entity, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
final Collection<ValidationError> errors;
if (entity instanceof ValidationError) {
errors = Collections.singleton((ValidationError) entity);
} else {
//noinspection unchecked
errors = (Collection<ValidationError>) entity;
}
final boolean isPlain = MediaType.TEXT_PLAIN_TYPE.getSubtype().equals(mediaType.getSubtype());
final StringBuilder builder = new StringBuilder();
// Root <div>
if (!isPlain) {
builder.append("<div class=\"validation-errors\">");
}
for (final ValidationError error : errors) {
if (!isPlain) {
builder.append("<div class=\"validation-error\">");
}
// Message.
builder.append(isPlain ? error.getMessage() : "<span class=\"message\">" + error.getMessage() + "</span>");
builder.append(' ');
builder.append('(');
// Path.
builder.append(isPlain ? "path = " : ("<span class=\"path\"><strong>path</strong> = "));
builder.append(isPlain ? error.getPath() : (error.getPath() + "</span>"));
builder.append(',');
builder.append(' ');
// Invalid value.
builder.append(isPlain ? "invalidValue = " : ("<span class=\"invalid-value\"><strong>invalidValue</strong> = "));
builder.append(isPlain ? error.getInvalidValue() : (error.getInvalidValue() + "</span>"));
builder.append(')');
if (!isPlain) {
builder.append("</div>");
} else {
builder.append('\n');
}
}
// Root <div>
if (!isPlain) {
builder.append("</div>");
}
entityStream.write(builder.toString().getBytes(MessageUtils.getCharset(mediaType)));
entityStream.flush();
}
use of org.glassfish.jersey.server.validation.ValidationError in project jersey by jersey.
the class ContactCardTest method testAddInvalidContact.
@Test
public void testAddInvalidContact() throws Exception {
final ContactCard entity = new ContactCard();
entity.setPhone("Crrrn");
final Response response = target().path("contact").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));
assertEquals(400, response.getStatus());
final List<ValidationError> validationErrorList = getValidationErrorList(response);
for (final ValidationError validationError : validationErrorList) {
assertTrue(validationError.getPath().contains("ContactCardResource.addContact.contact."));
}
final Set<String> messageTemplates = getValidationMessageTemplates(validationErrorList);
assertEquals(2, messageTemplates.size());
assertTrue(messageTemplates.contains("{contact.wrong.name}"));
assertTrue(messageTemplates.contains("{contact.wrong.phone}"));
}
Aggregations