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 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                if (exchange.hasOut()) {
087                    exchange.getOut().setFault(true);
088                    exchange.getOut().setBody(newBody);
089                } else {
090                    exchange.getIn().setFault(true);
091                    exchange.getIn().setBody(newBody);
092                }
093            }
094
095            @Override
096            public String toString() {
097                return "setFaultBody(" + expression + ")";
098            }
099        };
100    }
101
102    /**
103     * Sets the header on the message.
104     */
105    public static Processor setHeader(final String name, final Expression expression) {
106        return new Processor() {
107            public void process(Exchange exchange) {
108                Object value = expression.evaluate(exchange, Object.class);
109                if (exchange.hasOut()) {
110                    exchange.getOut().setHeader(name, value);
111                } else {
112                    exchange.getIn().setHeader(name, value);
113                }
114            }
115
116            @Override
117            public String toString() {
118                return "setHeader(" + name + ", " + expression + ")";
119            }
120        };
121    }
122
123    /**
124     * Sets the header on the OUT message
125     *
126     * @deprecated use {@link #setHeader(String, org.apache.camel.Expression)}
127     */
128    @Deprecated
129    public static Processor setOutHeader(final String name, final Expression expression) {
130        return new Processor() {
131            public void process(Exchange exchange) {
132                Object value = expression.evaluate(exchange, Object.class);
133                exchange.getOut().setHeader(name, value);
134            }
135
136            @Override
137            public String toString() {
138                return "setOutHeader(" + name + ", " + expression + ")";
139            }
140        };
141    }
142
143    /**
144     * Sets the header on the FAULT message
145     */
146    public static Processor setFaultHeader(final String name, final Expression expression) {
147        return new Processor() {
148            public void process(Exchange exchange) {
149                Object value = expression.evaluate(exchange, Object.class);
150                if (exchange.hasOut()) {
151                    exchange.getOut().setFault(true);
152                    exchange.getOut().setHeader(name, value);
153                } else {
154                    exchange.getIn().setFault(true);
155                    exchange.getIn().setHeader(name, value);
156                }
157            }
158
159            @Override
160            public String toString() {
161                return "setFaultHeader(" + name + ", " + expression + ")";
162            }
163        };
164    }
165
166    /**
167     * Sets the property on the exchange
168     */
169    public static Processor setProperty(final String name, final Expression expression) {
170        return new Processor() {
171            public void process(Exchange exchange) {
172                Object value = expression.evaluate(exchange, Object.class);
173                exchange.setProperty(name, value);
174            }
175
176            @Override
177            public String toString() {
178                return "setProperty(" + name + ", " + expression + ")";
179            }
180        };
181    }
182
183    /**
184     * Removes the header on the message.
185     */
186    public static Processor removeHeader(final String name) {
187        return new Processor() {
188            public void process(Exchange exchange) {
189                if (exchange.hasOut()) {
190                    exchange.getOut().removeHeader(name);
191                } else {
192                    exchange.getIn().removeHeader(name);
193                }
194            }
195
196            @Override
197            public String toString() {
198                return "removeHeader(" + name +  ")";
199            }
200        };
201    }
202
203    /**
204     * Removes the headers on the message
205     */
206    public static Processor removeHeaders(final String pattern) {
207        return new Processor() {
208            public void process(Exchange exchange) {
209                if (exchange.hasOut()) {
210                    exchange.getOut().removeHeaders(pattern);
211                } else {
212                    exchange.getIn().removeHeaders(pattern);
213                }
214            }
215
216            @Override
217            public String toString() {
218                return "removeHeaders(" + pattern +  ")";
219            }
220        };
221    }
222    
223    /**
224     * Removes all headers on the message, except for the ones provided in the <tt>names</tt> parameter
225     */
226    public static Processor removeHeaders(final String pattern, final String... exceptionPatterns) {
227        return new Processor() {
228            public void process(Exchange exchange) {
229                if (exchange.hasOut()) {
230                    exchange.getOut().removeHeaders(pattern, exceptionPatterns);
231                } else {
232                    exchange.getIn().removeHeaders(pattern, exceptionPatterns);
233                }
234            }
235
236            @Override
237            public String toString() {
238                return "removeHeaders(" + pattern + ", " + Arrays.toString(exceptionPatterns) + ")";
239            }
240        };
241    }
242
243    /**
244     * Removes the header on the FAULT message (FAULT must be OUT)
245     * @deprecated will be removed in the near future. Instead use {@link #removeHeader(String)}
246     */
247    @Deprecated
248    public static Processor removeFaultHeader(final String name) {
249        return new Processor() {
250            public void process(Exchange exchange) {
251                exchange.getOut().setFault(true);
252                exchange.getOut().removeHeader(name);
253            }
254
255            @Override
256            public String toString() {
257                return "removeFaultHeader(" + name +  ")";
258            }
259        };
260    }
261
262    /**
263     * Removes the property on the exchange
264     */
265    public static Processor removeProperty(final String name) {
266        return new Processor() {
267            public void process(Exchange exchange) {
268                exchange.removeProperty(name);
269            }
270
271            @Override
272            public String toString() {
273                return "removeProperty(" + name +  ")";
274            }
275        };
276    }
277    
278    /**
279     * Removes the properties on the exchange
280     */
281    public static Processor removeProperties(final String pattern) {
282        return new Processor() {
283            public void process(Exchange exchange) {
284                exchange.removeProperties(pattern);
285            }
286
287            @Override
288            public String toString() {
289                return "removeProperties(" + pattern +  ")";
290            }
291        };
292    }
293    
294    /**
295     * Removes all properties on the exchange, except for the ones provided in the <tt>names</tt> parameter
296     */
297    public static Processor removeProperties(final String pattern, final String... exceptionPatterns) {
298        return new Processor() {
299            public void process(Exchange exchange) {
300                exchange.removeProperties(pattern, exceptionPatterns);
301            }
302
303            @Override
304            public String toString() {
305                return "removeProperties(" + pattern + ", " + Arrays.toString(exceptionPatterns) + ")";
306            }
307        };
308    }
309
310    /**
311     * Throws an exception
312     */
313    public static Processor throwException(final Exception ex) {
314        return new Processor() {
315            public void process(Exchange exchange) throws Exception {
316                throw ex;
317            }
318
319            @Override
320            public String toString() {
321                return "throwException(" + ex.toString() +  ")";
322            }
323        };
324    }
325}