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