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 java.util.ArrayList;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.Predicate;
029import org.apache.camel.Processor;
030import org.apache.camel.spi.AsPredicate;
031import org.apache.camel.spi.Metadata;
032
033/**
034 * Intercepts a message at each step in the route
035 */
036@Metadata(label = "configuration")
037@XmlRootElement(name = "intercept")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class InterceptDefinition extends OutputDefinition<InterceptDefinition> {
040    @XmlTransient
041    protected final List<Processor> intercepted = new ArrayList<>();
042
043    public InterceptDefinition() {
044    }
045
046    @Override
047    public String toString() {
048        return "Intercept[" + getOutputs() + "]";
049    }
050
051    @Override
052    public String getShortName() {
053        return "intercept";
054    }
055
056    @Override
057    public String getLabel() {
058        return "intercept";
059    }
060
061    @Override
062    public boolean isAbstract() {
063        return true;
064    }
065
066    @Override
067    public boolean isTopLevelOnly() {
068        return true;
069    }
070
071    @Override
072    public List<ProcessorDefinition<?>> getOutputs() {
073        return outputs;
074    }
075
076    @XmlElementRef
077    @Override
078    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
079        super.setOutputs(outputs);
080    }
081
082    /**
083     * Applies this interceptor only if the given predicate is true
084     *
085     * @param predicate the predicate
086     * @return the builder
087     */
088    public InterceptDefinition when(@AsPredicate Predicate predicate) {
089        WhenDefinition when = new WhenDefinition(predicate);
090        addOutput(when);
091        return this;
092    }
093
094    /**
095     * This method is <b>only</b> for handling some post configuration that is
096     * needed since this is an interceptor, and we have to do a bit of magic
097     * logic to fixup to handle predicates with or without proceed/stop set as
098     * well.
099     */
100    public void afterPropertiesSet() {
101        if (getOutputs().size() == 0) {
102            // no outputs
103            return;
104        }
105
106        ProcessorDefinition<?> first = getOutputs().get(0);
107        if (first instanceof WhenDefinition) {
108            WhenDefinition when = (WhenDefinition)first;
109            // move this outputs to the when, expect the first one
110            // as the first one is the interceptor itself
111            for (int i = 1; i < outputs.size(); i++) {
112                ProcessorDefinition<?> out = outputs.get(i);
113                when.addOutput(out);
114            }
115            // remove the moved from the original output, by just keeping the
116            // first one
117            ProcessorDefinition<?> keep = outputs.get(0);
118            clearOutput();
119            outputs.add(keep);
120        }
121    }
122
123    public List<Processor> getIntercepted() {
124        return intercepted;
125    }
126
127}