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 */ 017package org.apache.camel.model.validator; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlElementRef; 022import javax.xml.bind.annotation.XmlType; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.Expression; 026import org.apache.camel.Predicate; 027import org.apache.camel.impl.validator.ProcessorValidator; 028import org.apache.camel.model.ExpressionNodeHelper; 029import org.apache.camel.model.language.ExpressionDefinition; 030import org.apache.camel.processor.validation.PredicateValidatingProcessor; 031import org.apache.camel.spi.Metadata; 032import org.apache.camel.spi.Validator; 033 034/** 035 * Represents a predicate {@link Validator} which leverages expression or predicates to 036 * perform content validation. A {@link ProcessorValidator} will be created internally 037 * with a {@link PredicateValidatingProcessor} which validates the message according to specified expression/predicates. 038 * 039 * {@see ValidatorDefinition} 040 * {@see Validator} 041 */ 042@Metadata(label = "validation") 043@XmlType(name = "predicateValidator") 044@XmlAccessorType(XmlAccessType.FIELD) 045public class PredicateValidatorDefinition extends ValidatorDefinition { 046 047 @XmlElementRef 048 private ExpressionDefinition expression; 049 050 @Override 051 protected Validator doCreateValidator(CamelContext context) throws Exception { 052 Predicate pred = getExpression().createPredicate(context); 053 PredicateValidatingProcessor processor = new PredicateValidatingProcessor(pred); 054 return new ProcessorValidator(context) 055 .setProcessor(processor) 056 .setType(getType()); 057 } 058 059 public ExpressionDefinition getExpression() { 060 return expression; 061 } 062 063 public void setExpression(ExpressionDefinition expression) { 064 // favour using the helper to set the expression as it can unwrap some unwanted builders when using Java DSL 065 if (expression instanceof Expression) { 066 this.expression = ExpressionNodeHelper.toExpressionDefinition((Expression) expression); 067 } else if (expression instanceof Predicate) { 068 this.expression = ExpressionNodeHelper.toExpressionDefinition((Predicate) expression); 069 } else { 070 this.expression = expression; 071 } 072 } 073 074} 075