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 */
017package org.apache.camel.builder;
018
019import java.util.Arrays;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.Expression;
023import org.apache.camel.Processor;
024
025/**
026 * A builder of a number of different {@link Processor} implementations
027 *
028 * @version 
029 */
030public final class ProcessorBuilder {
031
032    /**
033     * Utility classes should not have a public constructor.
034     */
035    private ProcessorBuilder() {
036    }
037
038    /**
039     * Creates a processor which sets the body of the message to the value of the expression
040     */
041    public static Processor setBody(final Expression expression) {
042        return new Processor() {
043            public void process(Exchange exchange) {
044                Object newBody = expression.evaluate(exchange, Object.class);
045                if (exchange.hasOut()) {
046                    exchange.getOut().setBody(newBody);
047                } else {
048                    exchange.getIn().setBody(newBody);
049                }
050            }
051
052            @Override
053            public String toString() {
054                return "setBody(" + expression + ")";
055            }
056        };
057    }
058
059    /**
060     * Creates a processor which sets the body of the OUT message to the value of the expression
061     *
062     * @deprecated use {@link #setBody(org.apache.camel.Expression)}
063     */
064    @Deprecated
065    public static Processor setOutBody(final Expression expression) {
066        return new Processor() {
067            public void process(Exchange exchange) {
068                Object newBody = expression.evaluate(exchange, Object.class);
069                exchange.getOut().setBody(newBody);
070            }
071
072            @Override
073            public String toString() {
074                return "setOutBody(" + expression + ")";
075            }
076        };
077    }
078
079    /**
080     * Creates a processor which sets the body of the FAULT message (FAULT must be OUT) to the value of the expression
081     */
082    public static Processor setFaultBody(final Expression expression) {
083        return new Processor() {
084            public void process(Exchange exchange) {
085                Object newBody = expression.evaluate(exchange, Object.class);
086                exchange.getOut().setFault(true);
087                exchange.getOut().setBody(newBody);
088            }
089
090            @Override
091            public String toString() {
092                return "setFaultBody(" + expression + ")";
093            }
094        };
095    }
096
097    /**
098     * Sets the header on the message.
099     */
100    public static Processor setHeader(final String name, final Expression expression) {
101        return new Processor() {
102            public void process(Exchange exchange) {
103                Object value = expression.evaluate(exchange, Object.class);
104                if (exchange.hasOut()) {
105                    exchange.getOut().setHeader(name, value);
106                } else {
107                    exchange.getIn().setHeader(name, value);
108                }
109            }
110
111            @Override
112            public String toString() {
113                return "setHeader(" + name + ", " + expression + ")";
114            }
115        };
116    }
117
118    /**
119     * Sets the header on the OUT message
120     *
121     * @deprecated use {@link #setHeader(String, org.apache.camel.Expression)}
122     */
123    @Deprecated
124    public static Processor setOutHeader(final String name, final Expression expression) {
125        return new Processor() {
126            public void process(Exchange exchange) {
127                Object value = expression.evaluate(exchange, Object.class);
128                exchange.getOut().setHeader(name, value);
129            }
130
131            @Override
132            public String toString() {
133                return "setOutHeader(" + name + ", " + expression + ")";
134            }
135        };
136    }
137
138    /**
139     * Sets the header on the FAULT message (FAULT must be OUT)
140     */
141    public static Processor setFaultHeader(final String name, final Expression expression) {
142        return new Processor() {
143            public void process(Exchange exchange) {
144                Object value = expression.evaluate(exchange, Object.class);
145                exchange.getOut().setFault(true);
146                exchange.getOut().setHeader(name, value);
147            }
148
149            @Override
150            public String toString() {
151                return "setFaultHeader(" + name + ", " + expression + ")";
152            }
153        };
154    }
155
156    /**
157     * Sets the property on the exchange
158     */
159    public static Processor setProperty(final String name, final Expression expression) {
160        return new Processor() {
161            public void process(Exchange exchange) {
162                Object value = expression.evaluate(exchange, Object.class);
163                exchange.setProperty(name, value);
164            }
165
166            @Override
167            public String toString() {
168                return "setProperty(" + name + ", " + expression + ")";
169            }
170        };
171    }
172
173    /**
174     * Removes the header on the message.
175     */
176    public static Processor removeHeader(final String name) {
177        return new Processor() {
178            public void process(Exchange exchange) {
179                if (exchange.hasOut()) {
180                    exchange.getOut().removeHeader(name);
181                } else {
182                    exchange.getIn().removeHeader(name);
183                }
184            }
185
186            @Override
187            public String toString() {
188                return "removeHeader(" + name +  ")";
189            }
190        };
191    }
192
193    /**
194     * Removes the headers on the message
195     */
196    public static Processor removeHeaders(final String pattern) {
197        return new Processor() {
198            public void process(Exchange exchange) {
199                if (exchange.hasOut()) {
200                    exchange.getOut().removeHeaders(pattern);
201                } else {
202                    exchange.getIn().removeHeaders(pattern);
203                }
204            }
205
206            @Override
207            public String toString() {
208                return "removeHeaders(" + pattern +  ")";
209            }
210        };
211    }
212    
213    /**
214     * Removes all headers on the message, except for the ones provided in the <tt>names</tt> parameter
215     */
216    public static Processor removeHeaders(final String pattern, final String... exceptionPatterns) {
217        return new Processor() {
218            public void process(Exchange exchange) {
219                if (exchange.hasOut()) {
220                    exchange.getOut().removeHeaders(pattern, exceptionPatterns);
221                } else {
222                    exchange.getIn().removeHeaders(pattern, exceptionPatterns);
223                }
224            }
225
226            @Override
227            public String toString() {
228                return "removeHeaders(" + pattern + ", " + Arrays.toString(exceptionPatterns) + ")";
229            }
230        };
231    }
232
233    /**
234     * Removes the header on the FAULT message (FAULT must be OUT)
235     * @deprecated will be removed in the near future. Instead use {@link #removeHeader(String)}
236     */
237    @Deprecated
238    public static Processor removeFaultHeader(final String name) {
239        return new Processor() {
240            public void process(Exchange exchange) {
241                exchange.getOut().setFault(true);
242                exchange.getOut().removeHeader(name);
243            }
244
245            @Override
246            public String toString() {
247                return "removeFaultHeader(" + name +  ")";
248            }
249        };
250    }
251
252    /**
253     * Removes the property on the exchange
254     */
255    public static Processor removeProperty(final String name) {
256        return new Processor() {
257            public void process(Exchange exchange) {
258                exchange.removeProperty(name);
259            }
260
261            @Override
262            public String toString() {
263                return "removeProperty(" + name +  ")";
264            }
265        };
266    }
267
268    /**
269     * Throws an exception
270     */
271    public static Processor throwException(final Exception ex) {
272        return new Processor() {
273            public void process(Exchange exchange) throws Exception {
274                throw ex;
275            }
276
277            @Override
278            public String toString() {
279                return "throwException(" + ex.toString() +  ")";
280            }
281        };
282    }
283}