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.ConstraintViolation;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.ValidationException;
025    
026    /**
027     * A bean validation exception occurred
028     * 
029     * @version $Revision: 18758 $
030     */
031    public class BeanValidationException extends ValidationException {
032    
033        private static final long serialVersionUID = 5767438583860347105L;
034    
035        private final Set<ConstraintViolation<Object>> constraintViolations;
036    
037        public BeanValidationException(Exchange exchange, Set<ConstraintViolation<Object>> constraintViolations, Object bean) {
038            super(exchange, buildMessage(constraintViolations, bean));
039            this.constraintViolations = constraintViolations;
040        }
041    
042        protected static String buildMessage(Set<ConstraintViolation<Object>> constraintViolations, Object bean) {
043            StringBuilder buffer = new StringBuilder("Validation failed for: ");
044            buffer.append(bean);
045    
046            buffer.append(" errors: [");
047            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
048                buffer.append("property: " + constraintViolation.getPropertyPath() + "; value: " + constraintViolation.getInvalidValue() + "; constraint: " + constraintViolation.getMessage() + "; ");
049            }
050            buffer.append("]");
051    
052            return buffer.toString();
053        }
054        
055        public Set<ConstraintViolation<Object>> getConstraintViolations() {
056            return constraintViolations;
057        }
058    }