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