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.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlRootElement;
026
027import org.apache.camel.Predicate;
028import org.apache.camel.spi.AsPredicate;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * Intercepts messages being sent to an endpoint
033 */
034@Metadata(label = "configuration")
035@XmlRootElement(name = "interceptSendToEndpoint")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class InterceptSendToEndpointDefinition extends OutputDefinition<InterceptSendToEndpointDefinition> {
038
039    @XmlAttribute(required = true)
040    private String uri;
041    @XmlAttribute
042    private String skipSendToOriginalEndpoint;
043    @XmlAttribute
044    private String afterUri;
045
046    public InterceptSendToEndpointDefinition() {
047    }
048
049    public InterceptSendToEndpointDefinition(String uri) {
050        this.uri = uri;
051    }
052
053    @Override
054    public List<ProcessorDefinition<?>> getOutputs() {
055        return outputs;
056    }
057
058    @XmlElementRef
059    @Override
060    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
061        super.setOutputs(outputs);
062    }
063
064    @Override
065    public String toString() {
066        return "InterceptSendToEndpoint[" + uri + " -> " + getOutputs() + "]";
067    }
068
069    @Override
070    public String getShortName() {
071        return "interceptSendToEndpoint";
072    }
073
074    @Override
075    public String getLabel() {
076        return "interceptSendToEndpoint[" + uri + "]";
077    }
078
079    @Override
080    public boolean isAbstract() {
081        return true;
082    }
083
084    @Override
085    public boolean isTopLevelOnly() {
086        return true;
087    }
088
089    /**
090     * Applies this interceptor only if the given predicate is true
091     *
092     * @param predicate the predicate
093     * @return the builder
094     */
095    public InterceptSendToEndpointDefinition when(@AsPredicate Predicate predicate) {
096        WhenDefinition when = new WhenDefinition(predicate);
097        addOutput(when);
098        return this;
099    }
100
101    /**
102     * Skip sending the {@link org.apache.camel.Exchange} to the original
103     * intended endpoint
104     *
105     * @return the builder
106     */
107    public InterceptSendToEndpointDefinition skipSendToOriginalEndpoint() {
108        setSkipSendToOriginalEndpoint(Boolean.toString(true));
109        return this;
110    }
111
112    /**
113     * After sending to the endpoint then send the message to this url which
114     * allows to process its result.
115     *
116     * @return the builder
117     */
118    public InterceptSendToEndpointDefinition afterUrl(String url) {
119        setAfterUri(url);
120        return this;
121    }
122
123    /**
124     * This method is <b>only</b> for handling some post configuration that is
125     * needed since this is an interceptor, and we have to do a bit of magic
126     * logic to fixup to handle predicates with or without proceed/stop set as
127     * well.
128     */
129    public void afterPropertiesSet() {
130        // okay the intercept endpoint works a bit differently than the regular
131        // interceptors
132        // so we must fix the route definition yet again
133
134        if (getOutputs().size() == 0) {
135            // no outputs
136            return;
137        }
138
139        // if there is a when definition at first, then its a predicate for this
140        // interceptor
141        ProcessorDefinition<?> first = getOutputs().get(0);
142        if (first instanceof WhenDefinition && !(first instanceof WhenSkipSendToEndpointDefinition)) {
143            WhenDefinition when = (WhenDefinition)first;
144
145            // create a copy of when to use as replacement
146            WhenSkipSendToEndpointDefinition newWhen = new WhenSkipSendToEndpointDefinition();
147            newWhen.setExpression(when.getExpression());
148            newWhen.setId(when.getId());
149            newWhen.setInheritErrorHandler(when.isInheritErrorHandler());
150            newWhen.setParent(when.getParent());
151            newWhen.setDescription(when.getDescription());
152
153            // move this outputs to the when, expect the first one
154            // as the first one is the interceptor itself
155            for (int i = 1; i < outputs.size(); i++) {
156                ProcessorDefinition<?> out = outputs.get(i);
157                newWhen.addOutput(out);
158            }
159            // remove the moved from the original output, by just keeping the
160            // first one
161            clearOutput();
162            outputs.add(newWhen);
163        }
164    }
165
166    public String getSkipSendToOriginalEndpoint() {
167        return skipSendToOriginalEndpoint;
168    }
169
170    /**
171     * If set to true then the message is not sent to the original endpoint. By
172     * default (false) the message is both intercepted and then sent to the
173     * original endpoint.
174     */
175    public void setSkipSendToOriginalEndpoint(String skipSendToOriginalEndpoint) {
176        this.skipSendToOriginalEndpoint = skipSendToOriginalEndpoint;
177    }
178
179    public String getUri() {
180        return uri;
181    }
182
183    /**
184     * Intercept sending to the uri or uri pattern.
185     */
186    public void setUri(String uri) {
187        this.uri = uri;
188    }
189
190    public String getAfterUri() {
191        return afterUri;
192    }
193
194    /**
195     * After sending to the endpoint then send the message to this uri which
196     * allows to process its result.
197     */
198    public void setAfterUri(String afterProcessor) {
199        this.afterUri = afterProcessor;
200    }
201
202}