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.processor; 018 019import org.apache.camel.AsyncCallback; 020import org.apache.camel.AsyncProcessor; 021import org.apache.camel.Exchange; 022import org.apache.camel.Expression; 023import org.apache.camel.Message; 024import org.apache.camel.Traceable; 025import org.apache.camel.spi.IdAware; 026import org.apache.camel.support.ServiceSupport; 027import org.apache.camel.util.AsyncProcessorHelper; 028import org.apache.camel.util.ObjectHelper; 029 030/** 031 * A processor which sets the header on the IN or OUT message with an {@link org.apache.camel.Expression} 032 */ 033public class SetHeaderProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware { 034 private String id; 035 private final Expression headerName; 036 private final Expression expression; 037 038 public SetHeaderProcessor(Expression headerName, Expression expression) { 039 this.headerName = headerName; 040 this.expression = expression; 041 ObjectHelper.notNull(headerName, "headerName"); 042 ObjectHelper.notNull(expression, "expression"); 043 } 044 045 public void process(Exchange exchange) throws Exception { 046 AsyncProcessorHelper.process(this, exchange); 047 } 048 049 @Override 050 public boolean process(Exchange exchange, AsyncCallback callback) { 051 try { 052 Object newHeader = expression.evaluate(exchange, Object.class); 053 054 if (exchange.getException() != null) { 055 // the expression threw an exception so we should break-out 056 callback.done(true); 057 return true; 058 } 059 060 boolean out = exchange.hasOut(); 061 Message old = out ? exchange.getOut() : exchange.getIn(); 062 063 String key = headerName.evaluate(exchange, String.class); 064 old.setHeader(key, newHeader); 065 066 } catch (Throwable e) { 067 exchange.setException(e); 068 } 069 070 callback.done(true); 071 return true; 072 } 073 074 @Override 075 public String toString() { 076 return "SetHeader(" + headerName + ", " + expression + ")"; 077 } 078 079 public String getTraceLabel() { 080 return "setHeader[" + headerName + ", " + expression + "]"; 081 } 082 083 public String getId() { 084 return id; 085 } 086 087 public void setId(String id) { 088 this.id = id; 089 } 090 091 public String getHeaderName() { 092 return headerName.toString(); 093 } 094 095 public Expression getExpression() { 096 return expression; 097 } 098 099 @Override 100 protected void doStart() throws Exception { 101 //noop 102 } 103 104 @Override 105 protected void doStop() throws Exception { 106 // noop 107 } 108}