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