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: 781237 $
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         *
041         * @param beanRef  reference to bean to lookup in the Registry
042         * @return the builder
043         */
044        public static ValueBuilder bean(String beanRef) {
045            Expression expression = ExpressionBuilder.beanExpression(beanRef);
046            return new ValueBuilder(expression);
047        }
048    
049        /**
050         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
051         * value builder
052         *
053         * @param beanRef  reference to bean to lookup in the Registry
054         * @param method   name of method to invoke
055         * @return the builder
056         */
057        public static ValueBuilder bean(String beanRef, String method) {
058            Expression expression = ExpressionBuilder.beanExpression(beanRef, method);
059            return new ValueBuilder(expression);
060        }
061        
062        /**
063         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
064         * value builder
065         *
066         * @param beanType the bean class which will be invoked
067         * @param method   name of method to invoke
068         * @return the builder
069         */
070        public static ValueBuilder bean(Class beanType, String method) {
071            Expression expression = ExpressionBuilder.beanExpression(beanType, method);
072            return new ValueBuilder(expression);
073        }
074    
075        /**
076         * Returns a constant expression
077         */
078        public static ValueBuilder constant(Object value) {
079            Expression expression = ExpressionBuilder.constantExpression(value);
080            return new ValueBuilder(expression);
081        }
082    
083        /**
084         * Returns a predicate and value builder for headers on an exchange
085         */
086        public static ValueBuilder header(String name) {
087            Expression expression = ExpressionBuilder.headerExpression(name);
088            return new ValueBuilder(expression);
089        }
090    
091        /**
092         * Returns a predicate and value builder for properties on an exchange
093         */
094        public static ValueBuilder property(String name) {
095            Expression expression = ExpressionBuilder.propertyExpression(name);
096            return new ValueBuilder(expression);
097        }    
098        
099        /**
100         * Returns a predicate and value builder for the inbound body on an exchange
101         */
102        public static ValueBuilder body() {
103            Expression expression = ExpressionBuilder.bodyExpression();
104            return new ValueBuilder(expression);
105        }
106    
107        /**
108         * Returns a predicate and value builder for the inbound message body as a
109         * specific type
110         */
111        public static <T> ValueBuilder bodyAs(Class<T> type) {
112            Expression expression = ExpressionBuilder.bodyExpression(type);
113            return new ValueBuilder(expression);
114        }
115    
116        /**
117         * Returns a predicate and value builder for the outbound body on an
118         * exchange
119         */
120        public static ValueBuilder outBody() {
121            Expression expression = ExpressionBuilder.outBodyExpression();
122            return new ValueBuilder(expression);
123        }
124    
125        /**
126         * Returns a predicate and value builder for the outbound message body as a
127         * specific type
128         */
129        public static <T> ValueBuilder outBodyAs(Class<T> type) {
130            Expression expression = ExpressionBuilder.outBodyExpression(type);
131            return new ValueBuilder(expression);
132        }
133    
134        /**
135         * Returns a predicate and value builder for the fault body on an
136         * exchange
137         */
138        public static ValueBuilder faultBody() {
139            Expression expression = ExpressionBuilder.faultBodyExpression();
140            return new ValueBuilder(expression);
141        }
142    
143        /**
144         * Returns a predicate and value builder for the fault message body as a
145         * specific type
146         */
147        public static <T> ValueBuilder faultBodyAs(Class<T> type) {
148            Expression expression = ExpressionBuilder.faultBodyExpression(type);
149            return new ValueBuilder(expression);
150        }
151    
152        /**
153         * Returns an expression for the given system property
154         */
155        public static ValueBuilder systemProperty(final String name) {
156            return systemProperty(name, null);
157        }
158    
159        /**
160         * Returns an expression for the given system property
161         */
162        public static ValueBuilder systemProperty(final String name, final String defaultValue) {
163            return new ValueBuilder(ExpressionBuilder.systemPropertyExpression(name, defaultValue));
164        }
165    
166        /**
167         * Returns a predicate and value builder for the exception message on an exchange
168         */
169        public static ValueBuilder exceptionMessage() {
170            Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
171            return new ValueBuilder(expression);
172        }
173        
174        /**
175         * Returns an expression that replaces all occurrences of the regular 
176         * expression with the given replacement
177         */
178        public static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
179            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
180            return new ValueBuilder(newExp);
181        }
182    
183        /**
184         * Returns an expression that replaces all occurrences of the regular 
185         * expression with the given replacement
186         */
187        public static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
188            Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
189            return new ValueBuilder(newExp);
190        }
191    
192        /**
193         * Returns an expression processing the exchange to the given endpoint uri.
194         *
195         * @param uri   endpoint uri
196         * @return the builder
197         */
198        public static ValueBuilder sendTo(String uri) {
199            Expression expression = ExpressionBuilder.toExpression(uri);
200            return new ValueBuilder(expression);
201        }
202    
203    }