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