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 org.apache.camel.Expression;
020
021/**
022 * A helper class for including portions of the <a
023 * href="http://camel.apache.org/expression.html">expression</a> and
024 * <a href="http://camel.apache.org/predicate.html">predicate</a> <a
025 * href="http://camel.apache.org/dsl.html">Java DSL</a>
026 *
027 * @version 
028 */
029public final class Builder {
030
031    /**
032     * Utility classes should not have a public constructor.
033     */
034    private Builder() {
035    }
036
037    /**
038     * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
039     * value builder.
040     * <p/>
041     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
042     *
043     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
044     * @return the builder
045     */
046    public static ValueBuilder bean(final Object beanOrBeanRef) {
047        return bean(beanOrBeanRef, null);
048    }
049
050    /**
051     * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
052     * value builder.
053     * <p/>
054     * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
055     *
056     * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
057     * @param method the method name
058     * @return the builder
059     */
060    public static ValueBuilder bean(Object beanOrBeanRef, String method) {
061        Expression expression;
062        if (beanOrBeanRef instanceof String) {
063            expression = ExpressionBuilder.beanExpression((String) beanOrBeanRef, method);
064        } else {
065            expression = ExpressionBuilder.beanExpression(beanOrBeanRef, method);
066        }
067        return new ValueBuilder(expression);
068    }
069    
070    /**
071     * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
072     * value builder
073     *
074     * @param beanType the bean class which will be invoked
075     * @param method   name of method to invoke
076     * @return the builder
077     */
078    public static ValueBuilder bean(Class<?> beanType, String method) {
079        Expression expression = ExpressionBuilder.beanExpression(beanType, method);
080        return new ValueBuilder(expression);
081    }
082
083    /**
084     * Returns a constant expression
085     */
086    public static ValueBuilder constant(Object value) {
087        Expression expression = ExpressionBuilder.constantExpression(value);
088        return new ValueBuilder(expression);
089    }
090    
091    /**
092     * Returns a constant expression
093     */
094    public static ValueBuilder language(String language, String expression) {
095        Expression exp = ExpressionBuilder.languageExpression(language, expression);
096        return new ValueBuilder(exp);
097    }
098
099    /**
100     * Returns a simple expression  
101     */
102    public static ValueBuilder simple(String value) {
103        Expression expression = ExpressionBuilder.simpleExpression(value);
104        return new ValueBuilder(expression);
105    }
106    
107    /**
108     * Returns a simple expression
109     */
110    public static ValueBuilder simple(String value, Class<?> resultType) {
111        Expression expression = ExpressionBuilder.simpleExpression(value);
112        expression = ExpressionBuilder.convertToExpression(expression, resultType);
113        return new ValueBuilder(expression);
114    }
115
116    /**
117     * Returns a predicate and value builder for headers on an exchange
118     */
119    public static ValueBuilder header(String name) {
120        Expression expression = ExpressionBuilder.headerExpression(name);
121        return new ValueBuilder(expression);
122    }
123
124    /**
125     * Returns a predicate and value builder for properties on an exchange
126     *
127     * @deprecated use {@link #exchangeProperty(String)} instead
128     */
129    @Deprecated
130    public static ValueBuilder property(String name) {
131        return exchangeProperty(name);
132    }
133    
134    /**
135     * Returns a predicate and value builder for properties on an exchange
136     */
137    public static ValueBuilder exchangeProperty(String name) {
138        Expression expression = ExpressionBuilder.exchangePropertyExpression(name);
139        return new ValueBuilder(expression);
140    }
141
142    /**
143     * Returns a predicate and value builder for the inbound body on an exchange
144     */
145    public static ValueBuilder body() {
146        Expression expression = ExpressionBuilder.bodyExpression();
147        return new ValueBuilder(expression);
148    }
149
150    /**
151     * Returns a predicate and value builder for the inbound message body as a
152     * specific type
153     */
154    public static <T> ValueBuilder bodyAs(Class<T> type) {
155        Expression expression = ExpressionBuilder.bodyExpression(type);
156        return new ValueBuilder(expression);
157    }
158
159    /**
160     * Returns a predicate and value builder for the outbound body on an
161     * exchange
162     */
163    public static ValueBuilder outBody() {
164        Expression expression = ExpressionBuilder.outBodyExpression();
165        return new ValueBuilder(expression);
166    }
167
168    /**
169     * Returns a predicate and value builder for the outbound message body as a
170     * specific type
171     */
172    public static <T> ValueBuilder outBodyAs(Class<T> type) {
173        Expression expression = ExpressionBuilder.outBodyExpression(type);
174        return new ValueBuilder(expression);
175    }
176
177    /**
178     * Returns a predicate and value builder for the fault body on an
179     * exchange
180     */
181    public static ValueBuilder faultBody() {
182        Expression expression = ExpressionBuilder.faultBodyExpression();
183        return new ValueBuilder(expression);
184    }
185
186    /**
187     * Returns a predicate and value builder for the fault message body as a
188     * specific type
189     */
190    public static <T> ValueBuilder faultBodyAs(Class<T> type) {
191        Expression expression = ExpressionBuilder.faultBodyExpression(type);
192        return new ValueBuilder(expression);
193    }
194
195    /**
196     * Returns an expression for the given system property
197     */
198    public static ValueBuilder systemProperty(final String name) {
199        return systemProperty(name, null);
200    }
201
202    /**
203     * Returns an expression for the given system property
204     */
205    public static ValueBuilder systemProperty(final String name, final String defaultValue) {
206        return new ValueBuilder(ExpressionBuilder.systemPropertyExpression(name, defaultValue));
207    }
208
209    /**
210     * Returns a predicate and value builder for the exception message on an exchange
211     */
212    public static ValueBuilder exceptionMessage() {
213        Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
214        return new ValueBuilder(expression);
215    }
216    
217    /**
218     * Returns a predicate and value builder for the exception stacktrace on an exchange
219     */
220    public static ValueBuilder exceptionStackTrace() {
221        Expression expression = ExpressionBuilder.exchangeExceptionStackTraceExpression();
222        return new ValueBuilder(expression);
223    }
224
225    /**
226     * Returns an expression that replaces all occurrences of the regular 
227     * expression with the given replacement
228     */
229    public static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
230        Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
231        return new ValueBuilder(newExp);
232    }
233
234    /**
235     * Returns an expression that replaces all occurrences of the regular 
236     * expression with the given replacement
237     */
238    public static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
239        Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
240        return new ValueBuilder(newExp);
241    }
242
243    /**
244     * Returns an expression processing the exchange to the given endpoint uri.
245     *
246     * @param uri   endpoint uri
247     * @return the builder
248     */
249    public static ValueBuilder sendTo(String uri) {
250        Expression expression = ExpressionBuilder.toExpression(uri);
251        return new ValueBuilder(expression);
252    }
253
254}