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.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    
023    /**
024     * A builder of a number of different {@link Processor} implementations
025     *
026     * @version $Revision: 799728 $
027     */
028    public final class ProcessorBuilder {
029    
030        /**
031         * Utility classes should not have a public constructor.
032         */
033        private ProcessorBuilder() {
034        }
035    
036        /**
037         * Creates a processor which sets the body of the IN message to the value of the expression
038         */
039        public static Processor setBody(final Expression expression) {
040            return new Processor() {
041                public void process(Exchange exchange) {
042                    Object newBody = expression.evaluate(exchange, Object.class);
043                    exchange.getIn().setBody(newBody);
044                }
045    
046                @Override
047                public String toString() {
048                    return "setBody(" + expression + ")";
049                }
050            };
051        }
052    
053        /**
054         * Creates a processor which sets the body of the OUT message to the value of the expression
055         */
056        public static Processor setOutBody(final Expression expression) {
057            return new Processor() {
058                public void process(Exchange exchange) {
059                    Object newBody = expression.evaluate(exchange, Object.class);
060                    exchange.getOut().setBody(newBody);
061                }
062    
063                @Override
064                public String toString() {
065                    return "setOutBody(" + expression + ")";
066                }
067            };
068        }
069    
070        /**
071         * Creates a processor which sets the body of the FAULT message to the value of the expression
072         */
073        public static Processor setFaultBody(final Expression expression) {
074            return new Processor() {
075                public void process(Exchange exchange) {
076                    Object newBody = expression.evaluate(exchange, Object.class);
077                    exchange.getOut().setFault(true);
078                    exchange.getOut().setBody(newBody);
079                }
080    
081                @Override
082                public String toString() {
083                    return "setFaultBody(" + expression + ")";
084                }
085            };
086        }
087    
088        /**
089         * Sets the header on the IN message
090         */
091        public static Processor setHeader(final String name, final Expression expression) {
092            return new Processor() {
093                public void process(Exchange exchange) {
094                    Object value = expression.evaluate(exchange, Object.class);
095                    exchange.getIn().setHeader(name, value);
096                }
097    
098                @Override
099                public String toString() {
100                    return "setHeader(" + name + ", " + expression + ")";
101                }
102            };
103        }
104    
105        /**
106         * Sets the header on the OUT message
107         */
108        public static Processor setOutHeader(final String name, final Expression expression) {
109            return new Processor() {
110                public void process(Exchange exchange) {
111                    Object value = expression.evaluate(exchange, Object.class);
112                    exchange.getOut().setHeader(name, value);
113                }
114    
115                @Override
116                public String toString() {
117                    return "setOutHeader(" + name + ", " + expression + ")";
118                }
119            };
120        }
121    
122        /**
123         * Sets the header on the FAULT message
124         */
125        public static Processor setFaultHeader(final String name, final Expression expression) {
126            return new Processor() {
127                public void process(Exchange exchange) {
128                    Object value = expression.evaluate(exchange, Object.class);
129                    exchange.getOut().setFault(true);
130                    exchange.getOut().setHeader(name, value);
131                }
132    
133                @Override
134                public String toString() {
135                    return "setFaultHeader(" + name + ", " + expression + ")";
136                }
137            };
138        }
139    
140        /**
141         * Sets the property on the exchange
142         */
143        public static Processor setProperty(final String name, final Expression expression) {
144            return new Processor() {
145                public void process(Exchange exchange) {
146                    Object value = expression.evaluate(exchange, Object.class);
147                    exchange.setProperty(name, value);
148                }
149    
150                @Override
151                public String toString() {
152                    return "setProperty(" + name + ", " + expression + ")";
153                }
154            };
155        }
156    
157        /**
158         * Removes the header on the IN message
159         */
160        public static Processor removeHeader(final String name) {
161            return new Processor() {
162                public void process(Exchange exchange) {
163                    exchange.getIn().removeHeader(name);
164                }
165    
166                @Override
167                public String toString() {
168                    return "removeHeader(" + name +  ")";
169                }
170            };
171        }
172    
173        /**
174         * Removes the header on the FAULT message
175         */
176        public static Processor removeFaultHeader(final String name) {
177            return new Processor() {
178                public void process(Exchange exchange) {
179                    exchange.getOut().setFault(true);
180                    exchange.getOut().removeHeader(name);
181                }
182    
183                @Override
184                public String toString() {
185                    return "removeFaultHeader(" + name +  ")";
186                }
187            };
188        }
189    
190        /**
191         * Removes the property on the exchange
192         */
193        public static Processor removeProperty(final String name) {
194            return new Processor() {
195                public void process(Exchange exchange) {
196                    exchange.removeProperty(name);
197                }
198    
199                @Override
200                public String toString() {
201                    return "removeProperty(" + name +  ")";
202                }
203            };
204        }
205    
206        /**
207         * Throws an exception
208         */
209        public static Processor throwException(final Exception ex) {
210            return new Processor() {
211                public void process(Exchange exchange) throws Exception {
212                    throw ex;
213                }
214    
215                @Override
216                public String toString() {
217                    return "throwException(" + ex.toString() +  ")";
218                }
219            };
220        }
221    }