Ejemplo n.º 1
0
  @Override
  public void validateResult(
      final Object resource, final Invocable resourceMethod, final Object result) {
    if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
      final Set<ConstraintViolation<Object>> constraintViolations =
          new HashSet<ConstraintViolation<Object>>();
      final Method handlingMethod = resourceMethod.getHandlingMethod();

      final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
      final MethodDescriptor methodDescriptor =
          beanDescriptor.getConstraintsForMethod(
              handlingMethod.getName(), handlingMethod.getParameterTypes());

      final Method definitionMethod = resourceMethod.getDefinitionMethod();

      if (methodDescriptor != null
          && methodDescriptor.hasConstrainedReturnValue()
          && validateOnExecutionHandler.validateMethod(
              resource.getClass(), definitionMethod, handlingMethod)) {
        constraintViolations.addAll(
            forExecutables().validateReturnValue(resource, handlingMethod, result));

        if (result instanceof Response) {
          constraintViolations.addAll(
              forExecutables()
                  .validateReturnValue(resource, handlingMethod, ((Response) result).getEntity()));
        }
      }

      if (!constraintViolations.isEmpty()) {
        throw new ConstraintViolationException(constraintViolations);
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public void validateResourceAndInputParams(
      final Object resource, final Invocable resourceMethod, final Object[] args) {
    final Set<ConstraintViolation<Object>> constraintViolations =
        new HashSet<ConstraintViolation<Object>>();
    final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());

    // Resource validation.
    if (beanDescriptor.isBeanConstrained()) {
      constraintViolations.addAll(validate(resource));
    }

    if (resourceMethod != null
        && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
      final Method handlingMethod = resourceMethod.getHandlingMethod();

      // Resource method validation - input parameters.
      final MethodDescriptor methodDescriptor =
          beanDescriptor.getConstraintsForMethod(
              handlingMethod.getName(), handlingMethod.getParameterTypes());

      if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters()) {
        constraintViolations.addAll(
            forExecutables().validateParameters(resource, handlingMethod, args));
      }
    }

    if (!constraintViolations.isEmpty()) {
      throw new ConstraintViolationException(constraintViolations);
    }
  }