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