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     */
017    package org.apache.camel.builder;
018    
019    import 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     */
029    public 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        public static ValueBuilder property(String name) {
128            Expression expression = ExpressionBuilder.propertyExpression(name);
129            return new ValueBuilder(expression);
130        }    
131        
132        /**
133         * Returns a predicate and value builder for the inbound body on an exchange
134         */
135        public static ValueBuilder body() {
136            Expression expression = ExpressionBuilder.bodyExpression();
137            return new ValueBuilder(expression);
138        }
139    
140        /**
141         * Returns a predicate and value builder for the inbound message body as a
142         * specific type
143         */
144        public static <T> ValueBuilder bodyAs(Class<T> type) {
145            Expression expression = ExpressionBuilder.bodyExpression(type);
146            return new ValueBuilder(expression);
147        }
148    
149        /**
150         * Returns a predicate and value builder for the outbound body on an
151         * exchange
152         */
153        public static ValueBuilder outBody() {
154            Expression expression = ExpressionBuilder.outBodyExpression();
155            return new ValueBuilder(expression);
156        }
157    
158        /**
159         * Returns a predicate and value builder for the outbound message body as a
160         * specific type
161         */
162        public static <T> ValueBuilder outBodyAs(Class<T> type) {
163            Expression expression = ExpressionBuilder.outBodyExpression(type);
164            return new ValueBuilder(expression);
165        }
166    
167        /**
168         * Returns a predicate and value builder for the fault body on an
169         * exchange
170         */
171        public static ValueBuilder faultBody() {
172            Expression expression = ExpressionBuilder.faultBodyExpression();
173            return new ValueBuilder(expression);
174        }
175    
176        /**
177         * Returns a predicate and value builder for the fault message body as a
178         * specific type
179         */
180        public static <T> ValueBuilder faultBodyAs(Class<T> type) {
181            Expression expression = ExpressionBuilder.faultBodyExpression(type);
182            return new ValueBuilder(expression);
183        }
184    
185        /**
186         * Returns an expression for the given system property
187         */
188        public static ValueBuilder systemProperty(final String name) {
189            return systemProperty(name, null);
190        }
191    
192        /**
193         * Returns an expression for the given system property
194         */
195        public static ValueBuilder systemProperty(final String name, final String defaultValue) {
196            return new ValueBuilder(ExpressionBuilder.systemPropertyExpression(name, defaultValue));
197        }
198    
199        /**
200         * Returns a predicate and value builder for the exception message on an exchange
201         */
202        public static ValueBuilder exceptionMessage() {
203            Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
204            return new ValueBuilder(expression);
205        }
206        
207        /**
208         * Returns a predicate and value builder for the exception stacktrace on an exchange
209         */
210        public static ValueBuilder exceptionStackTrace() {
211            Expression expression = ExpressionBuilder.exchangeExceptionStackTraceExpression();
212            return new ValueBuilder(expression);
213        }
214    
215        /**
216         * Returns an expression that replaces all occurrences of the regular 
217         * expression with the given replacement
218         */
219        public static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
220            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
221            return new ValueBuilder(newExp);
222        }
223    
224        /**
225         * Returns an expression that replaces all occurrences of the regular 
226         * expression with the given replacement
227         */
228        public static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
229            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
230            return new ValueBuilder(newExp);
231        }
232    
233        /**
234         * Returns an expression processing the exchange to the given endpoint uri.
235         *
236         * @param uri   endpoint uri
237         * @return the builder
238         */
239        public static ValueBuilder sendTo(String uri) {
240            Expression expression = ExpressionBuilder.toExpression(uri);
241            return new ValueBuilder(expression);
242        }
243    
244    }