001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.builder;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Expression;
022    import org.apache.camel.Processor;
023    
024    /**
025     * A builder of a number of different {@link Processor} implementations
026     *
027     * @version $Revision: 1.1 $
028     */
029    public class ProcessorBuilder {
030    
031        /**
032         * Creates a processor which sets the body of the IN message to the value of the expression
033         */
034        public static Processor setBody(final Expression expression) {
035            return new Processor() {
036                public void process(Exchange exchange) {
037                    Object newBody = expression.evaluate(exchange);
038                    exchange.getIn().setBody(newBody);
039                }
040    
041                @Override
042                public String toString() {
043                    return "setBody(" + expression + ")";
044                }
045            };
046        }
047    
048        /**
049         * Creates a processor which sets the body of the IN message to the value of the expression
050         */
051        public static Processor setOutBody(final Expression expression) {
052            return new Processor() {
053                public void process(Exchange exchange) {
054                    Object newBody = expression.evaluate(exchange);
055                    exchange.getOut().setBody(newBody);
056                }
057    
058                @Override
059                public String toString() {
060                    return "setOutBody(" + expression + ")";
061                }
062            };
063        }
064    
065        /**
066         * Sets the header on the IN message
067         */
068        public static Processor setHeader(final String name, final Expression expression) {
069            return new Processor() {
070                public void process(Exchange exchange) {
071                    Object value = expression.evaluate(exchange);
072                    exchange.getIn().setHeader(name, value);
073                }
074    
075                @Override
076                public String toString() {
077                    return "setHeader(" + name + ", " + expression + ")";
078                }
079            };
080        }
081    
082        /**
083         * Sets the header on the OUT message
084         */
085        public static Processor setOutHeader(final String name, final Expression expression) {
086            return new Processor() {
087                public void process(Exchange exchange) {
088                    Object value = expression.evaluate(exchange);
089                    exchange.getOut().setHeader(name, value);
090                }
091    
092                @Override
093                public String toString() {
094                    return "setOutHeader(" + name + ", " + expression + ")";
095                }
096            };
097        }
098    
099        /**
100         * Sets the property on the exchange
101         */
102        public static Processor setProperty(final String name, final Expression expression) {
103            return new Processor() {
104                public void process(Exchange exchange) {
105                    Object value = expression.evaluate(exchange);
106                    exchange.setProperty(name, value);
107                }
108    
109                @Override
110                public String toString() {
111                    return "setProperty(" + name + ", " + expression + ")";
112                }
113            };
114        }
115    }