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.Message; 022 import org.apache.camel.Processor; 023 024 /** 025 * A builder of a number of different {@link Processor} implementations 026 * 027 * @version $Revision: 47234 $ 028 */ 029 public final class ProcessorBuilder { 030 031 /** 032 * Utility classes should not have a public constructor. 033 */ 034 private ProcessorBuilder() { 035 } 036 037 /** 038 * Creates a processor which sets the body of the IN message to the value of the expression 039 */ 040 public static Processor setBody(final Expression expression) { 041 return new Processor() { 042 public void process(Exchange exchange) { 043 Object newBody = expression.evaluate(exchange); 044 exchange.getIn().setBody(newBody); 045 } 046 047 @Override 048 public String toString() { 049 return "setBody(" + expression + ")"; 050 } 051 }; 052 } 053 054 /** 055 * Creates a processor which sets the body of the OUT message to the value of the expression 056 */ 057 public static Processor setOutBody(final Expression expression) { 058 return new Processor() { 059 public void process(Exchange exchange) { 060 Object newBody = expression.evaluate(exchange); 061 exchange.getOut().setBody(newBody); 062 } 063 064 @Override 065 public String toString() { 066 return "setOutBody(" + expression + ")"; 067 } 068 }; 069 } 070 071 /** 072 * Creates a processor which sets the body of the FAULT message to the value of the expression 073 */ 074 public static Processor setFaultBody(final Expression expression) { 075 return new Processor() { 076 public void process(Exchange exchange) { 077 Object newBody = expression.evaluate(exchange); 078 exchange.getFault().setBody(newBody); 079 } 080 081 @Override 082 public String toString() { 083 return "setFaultBody(" + expression + ")"; 084 } 085 }; 086 } 087 088 /** 089 * Sets the header on the IN message 090 */ 091 public static Processor setHeader(final String name, final Expression expression) { 092 return new Processor() { 093 public void process(Exchange exchange) { 094 Object value = expression.evaluate(exchange); 095 exchange.getIn().setHeader(name, value); 096 } 097 098 @Override 099 public String toString() { 100 return "setHeader(" + name + ", " + expression + ")"; 101 } 102 }; 103 } 104 105 /** 106 * Sets the header on the OUT message 107 */ 108 public static Processor setOutHeader(final String name, final Expression expression) { 109 return new Processor() { 110 public void process(Exchange exchange) { 111 Object value = expression.evaluate(exchange); 112 exchange.getOut().setHeader(name, value); 113 } 114 115 @Override 116 public String toString() { 117 return "setOutHeader(" + name + ", " + expression + ")"; 118 } 119 }; 120 } 121 122 /** 123 * Sets the header on the FAULT message 124 */ 125 public static Processor setFaultHeader(final String name, final Expression expression) { 126 return new Processor() { 127 public void process(Exchange exchange) { 128 Object value = expression.evaluate(exchange); 129 exchange.getFault().setHeader(name, value); 130 } 131 132 @Override 133 public String toString() { 134 return "setFaultHeader(" + name + ", " + expression + ")"; 135 } 136 }; 137 } 138 139 /** 140 * Sets the property on the exchange 141 */ 142 public static Processor setProperty(final String name, final Expression expression) { 143 return new Processor() { 144 public void process(Exchange exchange) { 145 Object value = expression.evaluate(exchange); 146 exchange.setProperty(name, value); 147 } 148 149 @Override 150 public String toString() { 151 return "setProperty(" + name + ", " + expression + ")"; 152 } 153 }; 154 } 155 156 /** 157 * Removes the header on the IN message 158 */ 159 public static Processor removeHeader(final String name) { 160 return new Processor() { 161 public void process(Exchange exchange) { 162 exchange.getIn().removeHeader(name); 163 } 164 165 @Override 166 public String toString() { 167 return "removeHeader(" + name + ")"; 168 } 169 }; 170 } 171 172 /** 173 * Removes the header on the OUT message 174 */ 175 public static Processor removeOutHeader(final String name) { 176 return new Processor() { 177 public void process(Exchange exchange) { 178 Message out = exchange.getOut(false); 179 if (out != null) { 180 out.removeHeader(name); 181 } 182 } 183 184 @Override 185 public String toString() { 186 return "removeOutHeader(" + name + ")"; 187 } 188 }; 189 } 190 191 /** 192 * Removes the header on the FAULT message 193 */ 194 public static Processor removeFaultHeader(final String name) { 195 return new Processor() { 196 public void process(Exchange exchange) { 197 exchange.getFault().removeHeader(name); 198 } 199 200 @Override 201 public String toString() { 202 return "removeFaultHeader(" + name + ")"; 203 } 204 }; 205 } 206 207 /** 208 * Removes the property on the exchange 209 */ 210 public static Processor removeProperty(final String name) { 211 return new Processor() { 212 public void process(Exchange exchange) { 213 exchange.removeProperty(name); 214 } 215 216 @Override 217 public String toString() { 218 return "removeProperty(" + name + ")"; 219 } 220 }; 221 } 222 223 /** 224 * Throws an exception 225 */ 226 public static Processor throwException(final Exception ex) { 227 return new Processor() { 228 public void process(Exchange exchange) throws Exception { 229 throw ex; 230 } 231 232 @Override 233 public String toString() { 234 return "throwException(" + ex.toString() + ")"; 235 } 236 }; 237 } 238 }