Search in sources :

Example 1 with Default

use of javax.validation.groups.Default in project dropwizard by dropwizard.

the class DropwizardConfiguredValidator method getGroup.

/**
     * If the request entity is annotated with {@link Validated} then run
     * validations in the specified constraint group else validate with the
     * {@link Default} group
     */
private Class<?>[] getGroup(Invocable invocable) {
    final ImmutableList.Builder<Class<?>[]> builder = ImmutableList.builder();
    for (Parameter parameter : invocable.getParameters()) {
        if (parameter.isAnnotationPresent(Validated.class)) {
            builder.add(parameter.getAnnotation(Validated.class).value());
        }
    }
    final ImmutableList<Class<?>[]> groups = builder.build();
    switch(groups.size()) {
        // No parameters were annotated with Validated, so validate under the default group
        case 0:
            return new Class<?>[] { Default.class };
        // A single parameter was annotated with Validated, so use their group
        case 1:
            return groups.get(0);
        // group.
        default:
            for (int i = 0; i < groups.size(); i++) {
                for (int j = i; j < groups.size(); j++) {
                    if (!Arrays.deepEquals(groups.get(i), groups.get(j))) {
                        throw new WebApplicationException("Parameters must have the same validation groups in " + invocable.getHandlingMethod().getName(), 500);
                    }
                }
            }
            return groups.get(0);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ImmutableList(com.google.common.collect.ImmutableList) Parameter(org.glassfish.jersey.server.model.Parameter) Default(javax.validation.groups.Default)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Default (javax.validation.groups.Default)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Parameter (org.glassfish.jersey.server.model.Parameter)1