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