001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.bean.validator;
018
019 import java.util.Set;
020
021 import javax.validation.ConstraintValidatorFactory;
022 import javax.validation.ConstraintViolation;
023 import javax.validation.MessageInterpolator;
024 import javax.validation.TraversableResolver;
025 import javax.validation.Validator;
026 import javax.validation.ValidatorFactory;
027
028 import org.apache.camel.Exchange;
029 import org.apache.camel.Processor;
030
031 /**
032 * Bean validator that uses the JSR 303 reference implementation
033 * (Hibernate Validator).
034 *
035 * @version $Revision: 19061 $
036 */
037 public class BeanValidator implements Processor {
038
039 private ValidatorFactory validatorFactory;
040 private Validator validator;
041 private Class group;
042
043 public void process(Exchange exchange) throws Exception {
044 Object bean = exchange.getIn().getBody();
045 Set<ConstraintViolation<Object>> constraintViolations = null;
046
047 if (this.group != null) {
048 constraintViolations = validator.validate(bean, group);
049 } else {
050 constraintViolations = validator.validate(bean);
051 }
052
053 if (!constraintViolations.isEmpty()) {
054 throw new BeanValidationException(exchange, constraintViolations, exchange.getIn().getBody());
055 }
056 }
057
058 public ValidatorFactory getValidatorFactory() {
059 return validatorFactory;
060 }
061
062 public void setValidatorFactory(ValidatorFactory validatorFactory) {
063 this.validatorFactory = validatorFactory;
064 this.validator = this.validatorFactory.getValidator();
065 }
066
067 public Validator getValidator() {
068 return validator;
069 }
070
071 public Class getGroup() {
072 return group;
073 }
074
075 public void setGroup(Class group) {
076 this.group = group;
077 }
078
079 public MessageInterpolator getMessageInterpolator() {
080 return this.validatorFactory.getMessageInterpolator();
081 }
082
083 public TraversableResolver getTraversableResolver() {
084 return this.validatorFactory.getTraversableResolver();
085 }
086
087 public ConstraintValidatorFactory getConstraintValidatorFactory() {
088 return this.validatorFactory.getConstraintValidatorFactory();
089 }
090 }