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