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.builder;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.Endpoint;
025import org.apache.camel.Expression;
026import org.apache.camel.LoggingLevel;
027import org.apache.camel.NoSuchEndpointException;
028import org.apache.camel.builder.xml.XPathBuilder;
029import org.apache.camel.model.ModelCamelContext;
030import org.apache.camel.model.language.ExchangePropertyExpression;
031import org.apache.camel.model.language.HeaderExpression;
032import org.apache.camel.model.language.JsonPathExpression;
033import org.apache.camel.util.ObjectHelper;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Base class for implementation inheritance for different clauses in the <a
039 * href="http://camel.apache.org/dsl.html">Java DSL</a>
040 *
041 * @version
042 */
043public abstract class BuilderSupport {
044    private ModelCamelContext context;
045    private ErrorHandlerBuilder errorHandlerBuilder;
046
047    protected BuilderSupport() {
048    }
049
050    protected BuilderSupport(CamelContext context) {
051        this.context = context != null ? context.adapt(ModelCamelContext.class) : null;
052    }
053
054    // Builder methods
055    // -------------------------------------------------------------------------
056
057    /**
058     * Returns a value builder for the given header
059     */
060    public ValueBuilder header(String name) {
061        Expression exp = new HeaderExpression(name);
062        return new ValueBuilder(exp);
063    }
064
065    /**
066     *
067     * Returns a value builder for the given exchange property
068     * @deprecated use {@link #exchangeProperty(String)} instead
069     */
070    @Deprecated
071    public ValueBuilder property(String name) {
072        Expression exp = new ExchangePropertyExpression(name);
073        return new ValueBuilder(exp);
074    }
075
076    /**
077     * Returns a value builder for the given exchange property
078     */
079    public ValueBuilder exchangeProperty(String name) {
080        Expression exp = new ExchangePropertyExpression(name);
081        return new ValueBuilder(exp);
082    }
083
084    /**
085     * Returns a predicate and value builder for the inbound body on an exchange
086     */
087    public ValueBuilder body() {
088        return Builder.body();
089    }
090
091    /**
092     * Returns a predicate and value builder for the inbound message body as a
093     * specific type
094     *
095     * @deprecated use {@link #bodyAs(Class)}
096     */
097    @Deprecated
098    public <T> ValueBuilder body(Class<T> type) {
099        return bodyAs(type);
100    }
101
102    /**
103     * Returns a predicate and value builder for the inbound message body as a
104     * specific type
105     */
106    public <T> ValueBuilder bodyAs(Class<T> type) {
107        return Builder.bodyAs(type);
108    }
109
110    /**
111     * Returns a predicate and value builder for the outbound body on an
112     * exchange
113     *
114     * @deprecated use {@link #body()}
115     */
116    @Deprecated
117    public ValueBuilder outBody() {
118        return Builder.outBody();
119    }
120
121    /**
122     * Returns a predicate and value builder for the outbound message body as a
123     * specific type
124     *
125     * @deprecated use {@link #bodyAs(Class)}
126     */
127    @Deprecated
128    public <T> ValueBuilder outBody(Class<T> type) {
129        return Builder.outBodyAs(type);
130    }
131
132    /**
133     * Returns a predicate and value builder for the fault body on an
134     * exchange
135     */
136    public ValueBuilder faultBody() {
137        return Builder.faultBody();
138    }
139
140    /**
141     * Returns a predicate and value builder for the fault message body as a
142     * specific type
143     *
144     * @deprecated use {@link #bodyAs(Class)}
145     */
146    @Deprecated
147    public <T> ValueBuilder faultBodyAs(Class<T> type) {
148        return Builder.faultBodyAs(type);
149    }
150
151    /**
152     * Returns a value builder for the given system property
153     */
154    public ValueBuilder systemProperty(String name) {
155        return Builder.systemProperty(name);
156    }
157
158    /**
159     * Returns a value builder for the given system property
160     */
161    public ValueBuilder systemProperty(String name, String defaultValue) {
162        return Builder.systemProperty(name, defaultValue);
163    }
164
165    /**
166     * Returns a constant expression value builder
167     */
168    public ValueBuilder constant(Object value) {
169        return Builder.constant(value);
170    }
171
172    /**
173     * Returns a JSonPath expression value builder
174     */
175    public ValueBuilder jsonpath(String value) {
176        JsonPathExpression exp = new JsonPathExpression(value);
177        return new ValueBuilder(exp);
178    }
179
180    /**
181     * Returns a JSonPath expression value builder
182     *
183     * @param value      The JSonPath expression
184     * @param resultType The result type that the JSonPath expression will return.
185     */
186    public ValueBuilder jsonpath(String value, Class<?> resultType) {
187        JsonPathExpression exp = new JsonPathExpression(value);
188        exp.setResultType(resultType);
189        return new ValueBuilder(exp);
190    }
191
192    /**
193     * Returns a language expression value builder
194     */
195    public ValueBuilder language(String language, String expression) {
196        return Builder.language(language, expression);
197    }
198
199    /**
200     * Returns a simple expression value builder
201     */
202    public SimpleBuilder simple(String value) {
203        return SimpleBuilder.simple(value);
204    }
205
206    /**
207     * Returns a simple expression value builder
208     */
209    public SimpleBuilder simple(String value, Class<?> resultType) {
210        return SimpleBuilder.simple(value, resultType);
211    }
212
213    /**
214     * Returns a simple expression value builder, using String.format style
215     */
216    public SimpleBuilder simpleF(String format, Object...values) {
217        return SimpleBuilder.simpleF(format, values);
218    }
219
220    /**
221     * Returns a simple expression value builder, using String.format style
222     */
223    public SimpleBuilder simpleF(String format, Class<?> resultType, Object...values) {
224        return SimpleBuilder.simpleF(format, resultType, values);
225    }
226
227    /**
228     * Returns a xpath expression value builder
229     *
230     * @param value the XPath expression
231     * @return the builder
232     */
233    public XPathBuilder xpath(String value) {
234        return XPathBuilder.xpath(value);
235    }
236
237    /**
238     * Returns a xpath expression value builder
239     *
240     * @param value      the XPath expression
241     * @param resultType the result type that the XPath expression will return.
242     * @return the builder
243     */
244    public static XPathBuilder xpath(String value, Class<?> resultType) {
245        return XPathBuilder.xpath(value, resultType);
246    }
247
248    /**
249     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
250     * value builder
251     * <p/>
252     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
253     *
254     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
255     * @return the builder
256     * @deprecated use {@link #method(Object)} instead
257     */
258    @Deprecated
259    public ValueBuilder bean(Object beanOrBeanRef) {
260        return bean(beanOrBeanRef, null);
261    }
262
263    /**
264     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
265     * value builder
266     * <p/>
267     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
268     *
269     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
270     * @param method   name of method to invoke
271     * @return the builder
272     * @deprecated use {@link #method(Object, String)} instead
273     */
274    @Deprecated
275    public ValueBuilder bean(Object beanOrBeanRef, String method) {
276        return Builder.bean(beanOrBeanRef, method);
277    }
278
279    /**
280     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
281     * value builder
282     *
283     * @param beanType the Class of the bean which we want to invoke
284     * @return the builder
285     * @deprecated use {@link #method(Class)} instead
286     */
287    @Deprecated
288    public ValueBuilder bean(Class<?> beanType) {
289        return Builder.bean(beanType);
290    }
291
292    /**
293     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
294     * value builder
295     *
296     * @param beanType the Class of the bean which we want to invoke
297     * @param method   name of method to invoke
298     * @return the builder
299     * @deprecated use {@link #method(Class, String)} instead
300     */
301    @Deprecated
302    public ValueBuilder bean(Class<?> beanType, String method) {
303        return Builder.bean(beanType, method);
304    }
305
306    /**
307     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
308     * value builder
309     * <p/>
310     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
311     *
312     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
313     * @return the builder
314     */
315    public ValueBuilder method(Object beanOrBeanRef) {
316        return method(beanOrBeanRef, null);
317    }
318
319    /**
320     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
321     * value builder
322     * <p/>
323     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
324     *
325     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
326     * @param method   name of method to invoke
327     * @return the builder
328     */
329    public ValueBuilder method(Object beanOrBeanRef, String method) {
330        return Builder.bean(beanOrBeanRef, method);
331    }
332
333    /**
334     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
335     * value builder
336     *
337     * @param beanType the Class of the bean which we want to invoke
338     * @return the builder
339     */
340    public ValueBuilder method(Class<?> beanType) {
341        return Builder.bean(beanType);
342    }
343
344    /**
345     * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
346     * value builder
347     *
348     * @param beanType the Class of the bean which we want to invoke
349     * @param method   name of method to invoke
350     * @return the builder
351     */
352    public ValueBuilder method(Class<?> beanType, String method) {
353        return Builder.bean(beanType, method);
354    }
355
356    /**
357     * Returns an expression processing the exchange to the given endpoint uri
358     *
359     * @param uri endpoint uri to send the exchange to
360     * @return the builder
361     * @deprecated not in use, and not available in XML DSL
362     */
363    @Deprecated
364    public ValueBuilder sendTo(String uri) {
365        return Builder.sendTo(uri);
366    }
367
368    /**
369     * Returns an expression value builder that replaces all occurrences of the
370     * regular expression with the given replacement
371     */
372    public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
373        return Builder.regexReplaceAll(content, regex, replacement);
374    }
375
376    /**
377     * Returns an expression value builder that replaces all occurrences of the
378     * regular expression with the given replacement
379     */
380    public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
381        return Builder.regexReplaceAll(content, regex, replacement);
382    }
383
384    /**
385     * Returns a exception expression value builder
386     */
387    public ValueBuilder exceptionMessage() {
388        return Builder.exceptionMessage();
389    }
390
391    /**
392     * Resolves the given URI to an endpoint
393     *
394     * @param uri  the uri to resolve
395     * @throws NoSuchEndpointException if the endpoint URI could not be resolved
396     * @return the endpoint
397     */
398    public Endpoint endpoint(String uri) throws NoSuchEndpointException {
399        ObjectHelper.notNull(uri, "uri");
400        Endpoint endpoint = getContext().getEndpoint(uri);
401        if (endpoint == null) {
402            throw new NoSuchEndpointException(uri);
403        }
404        return endpoint;
405    }
406
407    /**
408     * Resolves the given URI to an endpoint of the specified type
409     *
410     * @param uri  the uri to resolve
411     * @param type the excepted type of the endpoint
412     * @throws NoSuchEndpointException if the endpoint URI could not be resolved
413     * @return the endpoint
414     */
415    public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
416        ObjectHelper.notNull(uri, "uri");
417        T endpoint = getContext().getEndpoint(uri, type);
418        if (endpoint == null) {
419            throw new NoSuchEndpointException(uri);
420        }
421        return endpoint;
422    }
423
424    /**
425     * Resolves the list of URIs into a list of {@link Endpoint} instances
426     *
427     * @param uris  list of endpoints to resolve
428     * @throws NoSuchEndpointException if an endpoint URI could not be resolved
429     * @return list of endpoints
430     */
431    public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
432        List<Endpoint> endpoints = new ArrayList<Endpoint>();
433        for (String uri : uris) {
434            endpoints.add(endpoint(uri));
435        }
436        return endpoints;
437    }
438
439    /**
440     * Helper method to create a list of {@link Endpoint} instances
441     *
442     * @param endpoints  endpoints
443     * @return list of the given endpoints
444     */
445    public List<Endpoint> endpoints(Endpoint... endpoints) {
446        List<Endpoint> answer = new ArrayList<Endpoint>();
447        answer.addAll(Arrays.asList(endpoints));
448        return answer;
449    }
450
451    /**
452     * Creates a default <a href="http://camel.apache.org/error-handler.html">error handler</a>.
453     *
454     * @return the builder
455     */
456    public DefaultErrorHandlerBuilder defaultErrorHandler() {
457        return new DefaultErrorHandlerBuilder();
458    }
459
460    /**
461     * Creates a disabled <a href="http://camel.apache.org/error-handler.html">error handler</a>
462     * for removing the default error handler
463     *
464     * @return the builder
465     */
466    public NoErrorHandlerBuilder noErrorHandler() {
467        return new NoErrorHandlerBuilder();
468    }
469
470    /**
471     * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
472     * which just logs errors
473     *
474     * @return the builder
475     * @deprecated use dead letter channel with a log endpoint
476     */
477    @Deprecated
478    public LoggingErrorHandlerBuilder loggingErrorHandler() {
479        return new LoggingErrorHandlerBuilder();
480    }
481
482    /**
483     * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
484     * which just logs errors
485     *
486     * @return the builder
487     * @deprecated use dead letter channel with a log endpoint
488     */
489    @Deprecated
490    public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
491        return loggingErrorHandler(LoggerFactory.getLogger(log));
492    }
493
494    /**
495     * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
496     * which just logs errors
497     *
498     * @return the builder
499     * @deprecated use dead letter channel with a log endpoint
500     */
501    @Deprecated
502    public LoggingErrorHandlerBuilder loggingErrorHandler(Logger log) {
503        return new LoggingErrorHandlerBuilder(log);
504    }
505
506    /**
507     * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
508     * which just logs errors
509     *
510     * @return the builder
511     * @deprecated use dead letter channel with a log endpoint
512     */
513    @Deprecated
514    public LoggingErrorHandlerBuilder loggingErrorHandler(Logger log, LoggingLevel level) {
515        return new LoggingErrorHandlerBuilder(log, level);
516    }
517
518    /**
519     * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
520     * is a error handler for handling messages that could not be delivered to it's intended destination.
521     *
522     * @param deadLetterUri  uri to the dead letter endpoint storing dead messages
523     * @return the builder
524     */
525    public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
526        return deadLetterChannel(endpoint(deadLetterUri));
527    }
528
529    /**
530     * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
531     * is a error handler for handling messages that could not be delivered to it's intended destination.
532     *
533     * @param deadLetterEndpoint  dead letter endpoint storing dead messages
534     * @return the builder
535     */
536    public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
537        return new DeadLetterChannelBuilder(deadLetterEndpoint);
538    }
539
540    // Properties
541    // -------------------------------------------------------------------------
542
543    public ModelCamelContext getContext() {
544        return context;
545    }
546
547    public void setContext(CamelContext context) {
548        ObjectHelper.notNull(context, "CamelContext", this);
549        this.context = context.adapt(ModelCamelContext.class);
550    }
551
552    public void setContext(ModelCamelContext context) {
553        ObjectHelper.notNull(context, "CamelContext", this);
554        this.context = context;
555    }
556
557    public ErrorHandlerBuilder getErrorHandlerBuilder() {
558        if (errorHandlerBuilder == null) {
559            errorHandlerBuilder = createErrorHandlerBuilder();
560        }
561        return errorHandlerBuilder;
562    }
563
564    protected ErrorHandlerBuilder createErrorHandlerBuilder() {
565        return new DefaultErrorHandlerBuilder();
566    }
567
568    /**
569     * Sets the error handler to use with processors created by this builder
570     */
571    public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
572        this.errorHandlerBuilder = errorHandlerBuilder;
573    }
574
575}