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 simple expression  
093         */
094        public static ValueBuilder simple(String value) {
095            Expression expression = ExpressionBuilder.simpleExpression(value);
096            return new ValueBuilder(expression);
097        }
098        
099        /**
100         * Returns a simple expression
101         */
102        public static ValueBuilder simple(String value, Class<?> resultType) {
103            Expression expression = ExpressionBuilder.simpleExpression(value);
104            expression = ExpressionBuilder.convertToExpression(expression, resultType);
105            return new ValueBuilder(expression);
106        }
107    
108        /**
109         * Returns a predicate and value builder for headers on an exchange
110         */
111        public static ValueBuilder header(String name) {
112            Expression expression = ExpressionBuilder.headerExpression(name);
113            return new ValueBuilder(expression);
114        }
115    
116        /**
117         * Returns a predicate and value builder for properties on an exchange
118         */
119        public static ValueBuilder property(String name) {
120            Expression expression = ExpressionBuilder.propertyExpression(name);
121            return new ValueBuilder(expression);
122        }    
123        
124        /**
125         * Returns a predicate and value builder for the inbound body on an exchange
126         */
127        public static ValueBuilder body() {
128            Expression expression = ExpressionBuilder.bodyExpression();
129            return new ValueBuilder(expression);
130        }
131    
132        /**
133         * Returns a predicate and value builder for the inbound message body as a
134         * specific type
135         */
136        public static <T> ValueBuilder bodyAs(Class<T> type) {
137            Expression expression = ExpressionBuilder.bodyExpression(type);
138            return new ValueBuilder(expression);
139        }
140    
141        /**
142         * Returns a predicate and value builder for the outbound body on an
143         * exchange
144         */
145        public static ValueBuilder outBody() {
146            Expression expression = ExpressionBuilder.outBodyExpression();
147            return new ValueBuilder(expression);
148        }
149    
150        /**
151         * Returns a predicate and value builder for the outbound message body as a
152         * specific type
153         */
154        public static <T> ValueBuilder outBodyAs(Class<T> type) {
155            Expression expression = ExpressionBuilder.outBodyExpression(type);
156            return new ValueBuilder(expression);
157        }
158    
159        /**
160         * Returns a predicate and value builder for the fault body on an
161         * exchange
162         */
163        public static ValueBuilder faultBody() {
164            Expression expression = ExpressionBuilder.faultBodyExpression();
165            return new ValueBuilder(expression);
166        }
167    
168        /**
169         * Returns a predicate and value builder for the fault message body as a
170         * specific type
171         */
172        public static <T> ValueBuilder faultBodyAs(Class<T> type) {
173            Expression expression = ExpressionBuilder.faultBodyExpression(type);
174            return new ValueBuilder(expression);
175        }
176    
177        /**
178         * Returns an expression for the given system property
179         */
180        public static ValueBuilder systemProperty(final String name) {
181            return systemProperty(name, null);
182        }
183    
184        /**
185         * Returns an expression for the given system property
186         */
187        public static ValueBuilder systemProperty(final String name, final String defaultValue) {
188            return new ValueBuilder(ExpressionBuilder.systemPropertyExpression(name, defaultValue));
189        }
190    
191        /**
192         * Returns a predicate and value builder for the exception message on an exchange
193         */
194        public static ValueBuilder exceptionMessage() {
195            Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
196            return new ValueBuilder(expression);
197        }
198        
199        /**
200         * Returns a predicate and value builder for the exception stacktrace on an exchange
201         */
202        public static ValueBuilder exceptionStackTrace() {
203            Expression expression = ExpressionBuilder.exchangeExceptionStackTraceExpression();
204            return new ValueBuilder(expression);
205        }
206    
207        /**
208         * Returns an expression that replaces all occurrences of the regular 
209         * expression with the given replacement
210         */
211        public static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
212            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
213            return new ValueBuilder(newExp);
214        }
215    
216        /**
217         * Returns an expression that replaces all occurrences of the regular 
218         * expression with the given replacement
219         */
220        public static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
221            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
222            return new ValueBuilder(newExp);
223        }
224    
225        /**
226         * Returns an expression processing the exchange to the given endpoint uri.
227         *
228         * @param uri   endpoint uri
229         * @return the builder
230         */
231        public static ValueBuilder sendTo(String uri) {
232            Expression expression = ExpressionBuilder.toExpression(uri);
233            return new ValueBuilder(expression);
234        }
235    
236    }