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