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.Predicate;
024import org.apache.camel.Processor;
025import org.apache.camel.model.language.ExpressionDefinition;
026import org.apache.camel.processor.FilterProcessor;
027import org.apache.camel.spi.AsPredicate;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030
031/**
032 * Filter out messages based using a predicate
033 *
034 * @version 
035 */
036@Metadata(label = "eip,routing") @AsPredicate
037@XmlRootElement(name = "filter")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class FilterDefinition extends ExpressionNode {
040
041    public FilterDefinition() {
042    }
043
044    public FilterDefinition(ExpressionDefinition expression) {
045        super(expression);
046    }
047
048    public FilterDefinition(Predicate predicate) {
049        super(predicate);
050    }
051
052    @Override
053    public String toString() {
054        return "Filter[" + getExpression() + " -> " + getOutputs() + "]";
055    }
056    
057    @Override
058    public String getLabel() {
059        return "filter[" + getExpression() + "]";
060    }
061    
062    @Override
063    public FilterProcessor createProcessor(RouteContext routeContext) throws Exception {
064        return createFilterProcessor(routeContext);
065    }
066    
067    @Override
068    protected FilterProcessor createFilterProcessor(RouteContext routeContext) throws Exception {
069        // filter EIP should have child outputs
070        Processor childProcessor = this.createChildProcessor(routeContext, true);
071        return new FilterProcessor(createPredicate(routeContext), childProcessor);
072    }
073
074    /**
075     * Expression to determine if the message should be filtered or not. If the expression returns an empty value or <tt>false</tt>
076     * then the message is filtered (dropped), otherwise the message is continued being routed.
077     */
078    @Override
079    public void setExpression(ExpressionDefinition expression) {
080        // override to include javadoc what the expression is used for
081        super.setExpression(expression);
082    }
083}