Search in sources :

Example 1 with ValidationError

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);
}
Also used : Response(javax.ws.rs.core.Response) NotFoundException(javax.ws.rs.NotFoundException) ValidationError(org.glassfish.jersey.server.validation.ValidationError)

Example 2 with ValidationError

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();
}
Also used : ValidationError(org.glassfish.jersey.server.validation.ValidationError)

Example 3 with ValidationError

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}"));
}
Also used : Response(javax.ws.rs.core.Response) ContactCard(org.glassfish.jersey.examples.beanvalidation.webapp.domain.ContactCard) ValidationError(org.glassfish.jersey.server.validation.ValidationError) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

ValidationError (org.glassfish.jersey.server.validation.ValidationError)3 Response (javax.ws.rs.core.Response)2 NotFoundException (javax.ws.rs.NotFoundException)1 ContactCard (org.glassfish.jersey.examples.beanvalidation.webapp.domain.ContactCard)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 Test (org.junit.Test)1