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;
021import java.util.concurrent.ExecutorService;
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlElementRef;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlTransient;
029
030import org.apache.camel.ExchangePattern;
031import org.apache.camel.Expression;
032import org.apache.camel.Processor;
033import org.apache.camel.builder.ExpressionBuilder;
034import org.apache.camel.processor.CamelInternalProcessor;
035import org.apache.camel.processor.SendDynamicProcessor;
036import org.apache.camel.processor.WireTapProcessor;
037import org.apache.camel.spi.Metadata;
038import org.apache.camel.spi.RouteContext;
039import org.apache.camel.util.CamelContextHelper;
040
041/**
042 * Routes a copy of a message (or creates a new message) to a secondary destination while continue routing the original message.
043 */
044@Metadata(label = "eip,endpoint,routing")
045@XmlRootElement(name = "wireTap")
046@XmlAccessorType(XmlAccessType.FIELD)
047public class WireTapDefinition<Type extends ProcessorDefinition<Type>> extends ToDynamicDefinition implements ExecutorServiceAwareDefinition<WireTapDefinition<Type>> {
048    @XmlTransient
049    private Processor newExchangeProcessor;
050    @XmlAttribute(name = "processorRef")
051    private String newExchangeProcessorRef;
052    @XmlElement(name = "body")
053    private ExpressionSubElementDefinition newExchangeExpression;
054    @XmlElementRef
055    private List<SetHeaderDefinition> headers = new ArrayList<>();
056    @XmlTransient
057    private ExecutorService executorService;
058    @XmlAttribute
059    private String executorServiceRef;
060    @XmlAttribute @Metadata(defaultValue = "true")
061    private Boolean copy;
062    @XmlAttribute @Metadata(defaultValue = "true")
063    private Boolean dynamicUri;
064    @XmlAttribute
065    private String onPrepareRef;
066    @XmlTransient
067    private Processor onPrepare;
068
069    public WireTapDefinition() {
070    }
071
072    @Override
073    public Processor createProcessor(RouteContext routeContext) throws Exception {
074        // executor service is mandatory for wire tap
075        boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, true);
076        ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "WireTap", this, true);
077
078        // must use InOnly for WireTap
079        setPattern(ExchangePattern.InOnly);
080
081        // create the send dynamic producer to send to the wire tapped endpoint
082        SendDynamicProcessor dynamicTo = (SendDynamicProcessor) super.createProcessor(routeContext);
083
084        // create error handler we need to use for processing the wire tapped
085        Processor target = wrapInErrorHandler(routeContext, dynamicTo);
086
087        // and wrap in unit of work
088        CamelInternalProcessor internal = new CamelInternalProcessor(target);
089        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
090
091        // is true bt default
092        boolean isCopy = getCopy() == null || getCopy();
093
094        WireTapProcessor answer = new WireTapProcessor(dynamicTo, internal, getPattern(), threadPool, shutdownThreadPool, isDynamic());
095        answer.setCopy(isCopy);
096        if (newExchangeProcessorRef != null) {
097            newExchangeProcessor = routeContext.mandatoryLookup(newExchangeProcessorRef, Processor.class);
098        }
099        if (newExchangeProcessor != null) {
100            answer.addNewExchangeProcessor(newExchangeProcessor);
101        }
102        if (newExchangeExpression != null) {
103            answer.setNewExchangeExpression(newExchangeExpression.createExpression(routeContext));
104        }
105        if (headers != null && !headers.isEmpty()) {
106            for (SetHeaderDefinition header : headers) {
107                Processor processor = createProcessor(routeContext, header);
108                answer.addNewExchangeProcessor(processor);
109            }
110        }
111        if (onPrepareRef != null) {
112            onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
113        }
114        if (onPrepare != null) {
115            answer.setOnPrepare(onPrepare);
116        }
117
118        return answer;
119    }
120
121    @Override
122    protected Expression createExpression(RouteContext routeContext) {
123        // whether to use dynamic or static uri
124        if (isDynamic()) {
125            return super.createExpression(routeContext);
126        } else {
127            return ExpressionBuilder.constantExpression(getUri());
128        }
129    }
130
131    private boolean isDynamic() {
132        // its dynamic by default
133        return dynamicUri == null || dynamicUri;
134    }
135
136    public ExchangePattern getPattern() {
137        return ExchangePattern.InOnly;
138    }
139
140    @Override
141    public String toString() {
142        return "WireTap[" + getUri() + "]";
143    }
144    
145    @Override
146    public String getLabel() {
147        return "wireTap[" + getUri() + "]";
148    }
149
150    @Override
151    @SuppressWarnings("unchecked")
152    public Type end() {
153        // allow end() to return to previous type so you can continue in the DSL
154        return (Type) super.end();
155    }
156
157    @Override
158    public void addOutput(ProcessorDefinition<?> output) {
159        // add outputs on parent as this wiretap does not support outputs
160        getParent().addOutput(output);
161    }
162
163    // Fluent API
164    // -------------------------------------------------------------------------
165
166    /**
167     * Uses a custom thread pool
168     *
169     * @param executorService a custom {@link ExecutorService} to use as thread pool
170     *                        for sending tapped exchanges
171     * @return the builder
172     */
173    public WireTapDefinition<Type> executorService(ExecutorService executorService) {
174        setExecutorService(executorService);
175        return this;
176    }
177
178    /**
179     * Uses a custom thread pool
180     *
181     * @param executorServiceRef reference to lookup a custom {@link ExecutorService}
182     *                           to use as thread pool for sending tapped exchanges
183     * @return the builder
184     */
185    public WireTapDefinition<Type> executorServiceRef(String executorServiceRef) {
186        setExecutorServiceRef(executorServiceRef);
187        return this;
188    }
189
190    /**
191     * Uses a copy of the original exchange
192     *
193     * @return the builder
194     */
195    public WireTapDefinition<Type> copy() {
196        setCopy(true);
197        return this;
198    }
199    
200    /**
201     * Uses a copy of the original exchange
202     *
203     * @param copy if it is true camel will copy the original exchange,
204     *             if it is false camel will not copy the original exchange 
205     * @return the builder
206     */
207    public WireTapDefinition<Type> copy(boolean copy) {
208        setCopy(copy);
209        return this;
210    }
211
212    /**
213     * Whether the uri is dynamic or static.
214     * If the uri is dynamic then the simple language is used to evaluate a dynamic uri to use as the wire-tap destination,
215     * for each incoming message. This works similar to how the <tt>toD</tt> EIP pattern works.
216     * If static then the uri is used as-is as the wire-tap destination.
217     *
218     * @param dynamicUri  whether to use dynamic or static uris
219     * @return the builder
220     */
221    public WireTapDefinition<Type> dynamicUri(boolean dynamicUri) {
222        setDynamicUri(dynamicUri);
223        return this;
224    }
225
226    /**
227     * @deprecated will be removed in Camel 3.0 Instead use {@link #newExchangeBody(org.apache.camel.Expression)}
228     */
229    @Deprecated
230    public WireTapDefinition<Type> newExchange(Expression expression) {
231        return newExchangeBody(expression);
232    }
233
234    /**
235     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
236     *
237     * @param expression expression that creates the new body to send
238     * @return the builder
239     * @see #newExchangeHeader(String, org.apache.camel.Expression)
240     */
241    public WireTapDefinition<Type> newExchangeBody(Expression expression) {
242        setNewExchangeExpression(new ExpressionSubElementDefinition(expression));
243        return this;
244    }
245
246    /**
247     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
248     *
249     * @param ref reference to the {@link Processor} to lookup in the {@link org.apache.camel.spi.Registry} to
250     *            be used for preparing the new exchange to send
251     * @return the builder
252     */
253    public WireTapDefinition<Type> newExchangeRef(String ref) {
254        setNewExchangeProcessorRef(ref);
255        return this;
256    }
257
258    /**
259     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
260     *
261     * @param processor  processor preparing the new exchange to send
262     * @return the builder
263     * @see #newExchangeHeader(String, org.apache.camel.Expression)
264     */
265    public WireTapDefinition<Type> newExchange(Processor processor) {
266        setNewExchangeProcessor(processor);
267        return this;
268    }
269
270    /**
271     * Sets a header on the <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}.
272     * <p/>
273     * Use this together with the {@link #newExchange(org.apache.camel.Expression)} or {@link #newExchange(org.apache.camel.Processor)}
274     * methods.
275     *
276     * @param headerName  the header name
277     * @param expression  the expression setting the header value
278     * @return the builder
279     */
280    public WireTapDefinition<Type> newExchangeHeader(String headerName, Expression expression) {
281        headers.add(new SetHeaderDefinition(headerName, expression));
282        return this;
283    }
284
285    /**
286     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
287     * This can be used to deep-clone messages that should be send, or any custom logic needed before
288     * the exchange is send.
289     *
290     * @param onPrepare the processor
291     * @return the builder
292     */
293    public WireTapDefinition<Type> onPrepare(Processor onPrepare) {
294        setOnPrepare(onPrepare);
295        return this;
296    }
297
298    /**
299     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
300     * This can be used to deep-clone messages that should be send, or any custom logic needed before
301     * the exchange is send.
302     *
303     * @param onPrepareRef reference to the processor to lookup in the {@link org.apache.camel.spi.Registry}
304     * @return the builder
305     */
306    public WireTapDefinition<Type> onPrepareRef(String onPrepareRef) {
307        setOnPrepareRef(onPrepareRef);
308        return this;
309    }
310
311    /**
312     * Sets the maximum size used by the {@link org.apache.camel.impl.ProducerCache} which is used
313     * to cache and reuse producers, when uris are reused.
314     *
315     * @param cacheSize  the cache size, use <tt>0</tt> for default cache size, or <tt>-1</tt> to turn cache off.
316     * @return the builder
317     */
318    @Override
319    public WireTapDefinition<Type> cacheSize(int cacheSize) {
320        setCacheSize(cacheSize);
321        return this;
322    }
323
324    /**
325     * Ignore the invalidate endpoint exception when try to create a producer with that endpoint
326     *
327     * @return the builder
328     */
329    @Override
330    public WireTapDefinition<Type> ignoreInvalidEndpoint() {
331        setIgnoreInvalidEndpoint(true);
332        return this;
333    }
334
335    // Properties
336    //-------------------------------------------------------------------------
337
338    @Override
339    public String getUri() {
340        return super.getUri();
341    }
342
343    /**
344     * The uri of the endpoint to wiretap to. The uri can be dynamic computed using the {@link org.apache.camel.language.simple.SimpleLanguage} expression.
345     */
346    @Override
347    public void setUri(String uri) {
348        super.setUri(uri);
349    }
350
351    public Processor getNewExchangeProcessor() {
352        return newExchangeProcessor;
353    }
354
355    /**
356     * To use a Processor for creating a new body as the message to use for wire tapping
357     */
358    public void setNewExchangeProcessor(Processor processor) {
359        this.newExchangeProcessor = processor;
360    }
361
362    public String getNewExchangeProcessorRef() {
363        return newExchangeProcessorRef;
364    }
365
366    /**
367     * Reference to a Processor to use for creating a new body as the message to use for wire tapping
368     */
369    public void setNewExchangeProcessorRef(String ref) {
370        this.newExchangeProcessorRef = ref;
371    }
372
373    public ExpressionSubElementDefinition getNewExchangeExpression() {
374        return newExchangeExpression;
375    }
376
377    /**
378     * Uses the expression for creating a new body as the message to use for wire tapping
379     */
380    public void setNewExchangeExpression(ExpressionSubElementDefinition newExchangeExpression) {
381        this.newExchangeExpression = newExchangeExpression;
382    }
383
384    public ExecutorService getExecutorService() {
385        return executorService;
386    }
387
388    public void setExecutorService(ExecutorService executorService) {
389        this.executorService = executorService;
390    }
391
392    public String getExecutorServiceRef() {
393        return executorServiceRef;
394    }
395
396    public void setExecutorServiceRef(String executorServiceRef) {
397        this.executorServiceRef = executorServiceRef;
398    }
399
400    public Boolean getCopy() {
401        return copy;
402    }
403
404    public void setCopy(Boolean copy) {
405        this.copy = copy;
406    }
407
408    public Boolean getDynamicUri() {
409        return dynamicUri;
410    }
411
412    public void setDynamicUri(Boolean dynamicUri) {
413        this.dynamicUri = dynamicUri;
414    }
415
416    public String getOnPrepareRef() {
417        return onPrepareRef;
418    }
419
420    public void setOnPrepareRef(String onPrepareRef) {
421        this.onPrepareRef = onPrepareRef;
422    }
423
424    public Processor getOnPrepare() {
425        return onPrepare;
426    }
427
428    public void setOnPrepare(Processor onPrepare) {
429        this.onPrepare = onPrepare;
430    }
431
432    public List<SetHeaderDefinition> getHeaders() {
433        return headers;
434    }
435
436    public void setHeaders(List<SetHeaderDefinition> headers) {
437        this.headers = headers;
438    }
439
440}