You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanValidator.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.validator;
  17. import java.io.Serializable;
  18. import java.util.Locale;
  19. import java.util.Set;
  20. import javax.validation.ConstraintViolation;
  21. import javax.validation.MessageInterpolator.Context;
  22. import javax.validation.Validation;
  23. import javax.validation.ValidationException;
  24. import javax.validation.ValidatorFactory;
  25. import javax.validation.metadata.ConstraintDescriptor;
  26. import com.vaadin.data.BeanValidationBinder;
  27. import com.vaadin.v7.data.Validator;
  28. /**
  29. * Vaadin {@link Validator} using the JSR-303 (javax.validation)
  30. * annotation-based bean validation.
  31. *
  32. * The annotations of the fields of the beans are used to determine the
  33. * validation to perform.
  34. *
  35. * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean
  36. * Validation - formerly agimatec validation) must be present on the project
  37. * classpath when using bean validation.
  38. *
  39. * @since 7.0
  40. *
  41. * @author Petri Hakala
  42. * @author Henri Sara
  43. *
  44. * @deprecated See {@link BeanValidationBinder} and
  45. * {@link com.vaadin.data.validator.BeanValidator}
  46. */
  47. @Deprecated
  48. public class BeanValidator implements Validator {
  49. private static final long serialVersionUID = 1L;
  50. private static ValidatorFactory factory;
  51. private transient javax.validation.Validator javaxBeanValidator;
  52. private String propertyName;
  53. private Class<?> beanClass;
  54. private Locale locale;
  55. /**
  56. * Simple implementation of a message interpolator context that returns
  57. * fixed values.
  58. */
  59. @Deprecated
  60. protected static class SimpleContext implements Context, Serializable {
  61. private final Object value;
  62. private final ConstraintViolation<?> violation;
  63. private final ConstraintDescriptor<?> descriptor;
  64. /**
  65. * Create a simple immutable message interpolator context.
  66. *
  67. * @param value
  68. * value being validated
  69. * @param violation
  70. * ConstraintViolation corresponding to the constraint being
  71. * validated
  72. */
  73. public SimpleContext(Object value, ConstraintViolation<?> violation) {
  74. this.value = value;
  75. this.violation = violation;
  76. this.descriptor = violation.getConstraintDescriptor();
  77. }
  78. public SimpleContext(Object value, ConstraintDescriptor<?> descriptor) {
  79. this.value = value;
  80. this.descriptor = descriptor;
  81. this.violation = null;
  82. }
  83. @Override
  84. public ConstraintDescriptor<?> getConstraintDescriptor() {
  85. return descriptor;
  86. }
  87. @Override
  88. public Object getValidatedValue() {
  89. return value;
  90. }
  91. @Override
  92. public <T> T unwrap(Class<T> type) {
  93. if (violation != null) {
  94. return violation.unwrap(type);
  95. } else {
  96. try {
  97. return type.newInstance();
  98. } catch (InstantiationException e) {
  99. throw new ValidationException();
  100. } catch (IllegalAccessException e) {
  101. throw new ValidationException();
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Creates a Vaadin {@link Validator} utilizing JSR-303 bean validation.
  108. *
  109. * @param beanClass
  110. * bean class based on which the validation should be performed
  111. * @param propertyName
  112. * property to validate
  113. */
  114. public BeanValidator(Class<?> beanClass, String propertyName) {
  115. this.beanClass = beanClass;
  116. this.propertyName = propertyName;
  117. locale = Locale.getDefault();
  118. }
  119. /*
  120. * (non-Javadoc)
  121. *
  122. * @see com.vaadin.data.Validator#validate(java.lang.Object)
  123. */
  124. @Override
  125. public void validate(final Object value) throws InvalidValueException {
  126. Set<?> violations = getJavaxBeanValidator().validateValue(beanClass,
  127. propertyName, value);
  128. if (!violations.isEmpty()) {
  129. InvalidValueException[] causes = new InvalidValueException[violations
  130. .size()];
  131. int i = 0;
  132. for (Object v : violations) {
  133. final ConstraintViolation<?> violation = (ConstraintViolation<?>) v;
  134. String msg = getJavaxBeanValidatorFactory()
  135. .getMessageInterpolator()
  136. .interpolate(violation.getMessageTemplate(),
  137. new SimpleContext(value,
  138. violation),
  139. locale);
  140. causes[i] = new InvalidValueException(msg);
  141. ++i;
  142. }
  143. throw new InvalidValueException(null, causes);
  144. }
  145. }
  146. /**
  147. * Sets the locale used for validation error messages.
  148. *
  149. * Revalidation is not automatically triggered by setting the locale.
  150. *
  151. * @param locale
  152. */
  153. public void setLocale(Locale locale) {
  154. this.locale = locale;
  155. }
  156. /**
  157. * Gets the locale used for validation error messages.
  158. *
  159. * @return locale used for validation
  160. */
  161. public Locale getLocale() {
  162. return locale;
  163. }
  164. /**
  165. * Returns the underlying JSR-303 bean validator factory used. A factory is
  166. * created using {@link Validation} if necessary.
  167. *
  168. * @return {@link ValidatorFactory} to use
  169. */
  170. protected static ValidatorFactory getJavaxBeanValidatorFactory() {
  171. if (factory == null) {
  172. factory = Validation.buildDefaultValidatorFactory();
  173. }
  174. return factory;
  175. }
  176. /**
  177. * Returns a shared Validator instance to use. An instance is created using
  178. * the validator factory if necessary and thereafter reused by the
  179. * {@link BeanValidator} instance.
  180. *
  181. * @return the JSR-303 {@link javax.validation.Validator} to use
  182. */
  183. protected javax.validation.Validator getJavaxBeanValidator() {
  184. if (javaxBeanValidator == null) {
  185. javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator();
  186. }
  187. return javaxBeanValidator;
  188. }
  189. }