コード例 #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);
      }
    }
  }
コード例 #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);
    }
  }
コード例 #3
0
  public ValidationExtension() {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    BootstrapConfiguration bootstrap = config.getBootstrapConfiguration();
    globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes();
    isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled();
    validatorFactory = config.buildValidatorFactory();
    validator = validatorFactory.getValidator();

    executableHelper = new ExecutableHelper(new TypeResolutionHelper());
  }