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.model;
018    
019    import org.apache.camel.Processor;
020    import org.apache.camel.Route;
021    import org.apache.camel.Predicate;
022    import org.apache.camel.builder.PredicateBuilder;
023    import org.apache.camel.impl.RouteContext;
024    import org.apache.camel.processor.Interceptor;
025    
026    import javax.xml.bind.annotation.XmlAccessType;
027    import javax.xml.bind.annotation.XmlAccessorType;
028    import javax.xml.bind.annotation.XmlRootElement;
029    import java.util.Collection;
030    
031    /**
032     * @version $Revision: 1.1 $
033     */
034    @XmlRootElement(name = "intercept")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class InterceptType extends OutputType {
037        @Override
038        public String toString() {
039            return "Intercept[" + getOutputs() + "]";
040        }
041    
042        public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
043            Interceptor interceptor = new Interceptor();
044            routeContext.intercept(interceptor);
045    
046            final Processor interceptRoute = routeContext.createProcessor(this);
047            interceptor.setInterceptorLogic(interceptRoute);
048        }
049    
050        /**
051         * Applies this interceptor only if the given predicate is true
052         */
053        public OtherwiseType when(Predicate predicate) {
054            return choice().when(PredicateBuilder.not(predicate)).proceed().otherwise();
055    
056        }
057    }