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; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlRootElement; 022 023import org.apache.camel.Expression; 024import org.apache.camel.Predicate; 025import org.apache.camel.model.language.ExpressionDefinition; 026import org.apache.camel.processor.validation.PredicateValidatingProcessor; 027import org.apache.camel.spi.AsPredicate; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.spi.RouteContext; 030 031/** 032 * Validates a message based on an expression 033 * 034 * @version 035 */ 036@Metadata(label = "eip,transformation") @AsPredicate 037@XmlRootElement(name = "validate") 038@XmlAccessorType(XmlAccessType.FIELD) 039public class ValidateDefinition extends NoOutputExpressionNode { 040 041 public ValidateDefinition() { 042 } 043 044 public ValidateDefinition(Expression expression) { 045 super(expression); 046 } 047 048 public ValidateDefinition(Predicate predicate) { 049 super(predicate); 050 } 051 052 @Override 053 public String toString() { 054 return "Validate[" + getExpression() + " -> " + getOutputs() + "]"; 055 } 056 057 @Override 058 public String getShortName() { 059 return "validate"; 060 } 061 062 @Override 063 public String getLabel() { 064 return "validate[" + getExpression() + "]"; 065 } 066 067 @Override 068 public PredicateValidatingProcessor createProcessor(RouteContext routeContext) throws Exception { 069 Predicate pred = getExpression().createPredicate(routeContext); 070 return new PredicateValidatingProcessor(pred); 071 } 072 073 /** 074 * Expression to use for validation as a predicate. The expression should return either <tt>true</tt> or <tt>false</tt>. 075 * If returning <tt>false</tt> the message is invalid and an exception is thrown. 076 */ 077 @Override 078 public void setExpression(ExpressionDefinition expression) { 079 // override to include javadoc what the expression is used for 080 super.setExpression(expression); 081 } 082 083 084}