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: 751655 $
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);
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);
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);
077 exchange.getFault().setBody(newBody);
078 }
079
080 @Override
081 public String toString() {
082 return "setFaultBody(" + expression + ")";
083 }
084 };
085 }
086
087 /**
088 * Sets the header on the IN message
089 */
090 public static Processor setHeader(final String name, final Expression expression) {
091 return new Processor() {
092 public void process(Exchange exchange) {
093 Object value = expression.evaluate(exchange);
094 exchange.getIn().setHeader(name, value);
095 }
096
097 @Override
098 public String toString() {
099 return "setHeader(" + name + ", " + expression + ")";
100 }
101 };
102 }
103
104 /**
105 * Sets the header on the OUT message
106 */
107 public static Processor setOutHeader(final String name, final Expression expression) {
108 return new Processor() {
109 public void process(Exchange exchange) {
110 Object value = expression.evaluate(exchange);
111 exchange.getOut().setHeader(name, value);
112 }
113
114 @Override
115 public String toString() {
116 return "setOutHeader(" + name + ", " + expression + ")";
117 }
118 };
119 }
120
121 /**
122 * Sets the header on the FAULT message
123 */
124 public static Processor setFaultHeader(final String name, final Expression expression) {
125 return new Processor() {
126 public void process(Exchange exchange) {
127 Object value = expression.evaluate(exchange);
128 exchange.getFault().setHeader(name, value);
129 }
130
131 @Override
132 public String toString() {
133 return "setFaultHeader(" + name + ", " + expression + ")";
134 }
135 };
136 }
137
138 /**
139 * Sets the property on the exchange
140 */
141 public static Processor setProperty(final String name, final Expression expression) {
142 return new Processor() {
143 public void process(Exchange exchange) {
144 Object value = expression.evaluate(exchange);
145 exchange.setProperty(name, value);
146 }
147
148 @Override
149 public String toString() {
150 return "setProperty(" + name + ", " + expression + ")";
151 }
152 };
153 }
154
155 /**
156 * Removes the header on the IN message
157 */
158 public static Processor removeHeader(final String name) {
159 return new Processor() {
160 public void process(Exchange exchange) {
161 exchange.getIn().removeHeader(name);
162 }
163
164 @Override
165 public String toString() {
166 return "removeHeader(" + name + ")";
167 }
168 };
169 }
170
171 /**
172 * Removes the header on the FAULT message
173 */
174 public static Processor removeFaultHeader(final String name) {
175 return new Processor() {
176 public void process(Exchange exchange) {
177 exchange.getFault().removeHeader(name);
178 }
179
180 @Override
181 public String toString() {
182 return "removeFaultHeader(" + name + ")";
183 }
184 };
185 }
186
187 /**
188 * Removes the property on the exchange
189 */
190 public static Processor removeProperty(final String name) {
191 return new Processor() {
192 public void process(Exchange exchange) {
193 exchange.removeProperty(name);
194 }
195
196 @Override
197 public String toString() {
198 return "removeProperty(" + name + ")";
199 }
200 };
201 }
202
203 /**
204 * Throws an exception
205 */
206 public static Processor throwException(final Exception ex) {
207 return new Processor() {
208 public void process(Exchange exchange) throws Exception {
209 throw ex;
210 }
211
212 @Override
213 public String toString() {
214 return "throwException(" + ex.toString() + ")";
215 }
216 };
217 }
218 }