diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java
index a8c5810c87..100e96b430 100644
--- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java
+++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2010 the original author or authors.
+ * Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.BeanNameAware;
+import org.springframework.beans.factory.InitializingBean;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelParserConfiguration;
@@ -36,17 +38,17 @@ import org.springframework.integration.support.MessageBuilder;
/**
* A Transformer that adds statically configured header values to a Message.
- * Accepts the boolean 'overwrite' property that specifies whether values
- * should be overwritten. By default, any existing header values for
- * a given key, will not be replaced.
+ * Accepts the boolean 'overwrite' property that specifies whether values should
+ * be overwritten. By default, any existing header values for a given key, will
+ * not be replaced.
*
* @author Mark Fisher
+ * @author David Turanski
*/
-public class HeaderEnricher implements Transformer {
+public class HeaderEnricher implements Transformer, BeanNameAware, InitializingBean {
private static final Log logger = LogFactory.getLog(HeaderEnricher.class);
-
private final Map> headersToAdd;
private volatile MessageProcessor> messageProcessor;
@@ -55,6 +57,7 @@ public class HeaderEnricher implements Transformer {
private volatile boolean shouldSkipNulls = true;
+ private Object beanName;
public HeaderEnricher() {
this(null);
@@ -64,10 +67,10 @@ public class HeaderEnricher implements Transformer {
* Create a HeaderEnricher with the given map of headers.
*/
public HeaderEnricher(Map> headersToAdd) {
- this.headersToAdd = (headersToAdd != null) ? headersToAdd : new HashMap>();
+ this.headersToAdd = (headersToAdd != null) ? headersToAdd
+ : new HashMap>();
}
-
public void setMessageProcessor(MessageProcessor messageProcessor) {
this.messageProcessor = messageProcessor;
}
@@ -77,9 +80,11 @@ public class HeaderEnricher implements Transformer {
}
/**
- * Specify whether null values, such as might be returned from an expression evaluation,
- * should be skipped. The default value is true. Set this to false if a
- * null value should trigger removal of the corresponding header instead.
+ * Specify whether null values, such as might be returned from
+ * an expression evaluation, should be skipped. The default value is
+ * true. Set this to false if a
+ * null value should trigger removal of the
+ * corresponding header instead.
*/
public void setShouldSkipNulls(boolean shouldSkipNulls) {
this.shouldSkipNulls = shouldSkipNulls;
@@ -92,27 +97,29 @@ public class HeaderEnricher implements Transformer {
for (Map.Entry> entry : this.headersToAdd.entrySet()) {
String key = entry.getKey();
HeaderValueMessageProcessor> valueProcessor = entry.getValue();
+
Boolean shouldOverwrite = valueProcessor.isOverwrite();
if (shouldOverwrite == null) {
shouldOverwrite = this.defaultOverwrite;
}
+
+ boolean headerDoesNotExist = headerMap.get(key) == null;
+
/**
- * Only evaluate value if necessary
+ * Only evaluate value expression if necessary
*/
- Object value = null;
- if (headerMap.get(key) == null || shouldOverwrite || !this.shouldSkipNulls){
- value = valueProcessor.processMessage(message);
- }
-
- if ((value != null && shouldOverwrite) || headerMap.get(key) == null || (value == null && !this.shouldSkipNulls)) {
- headerMap.put(key, value);
+ if (headerDoesNotExist || shouldOverwrite) {
+ Object value = valueProcessor.processMessage(message);
+ if (value != null || !this.shouldSkipNulls) {
+ headerMap.put(key, value);
+ }
}
}
- return MessageBuilder.withPayload(message.getPayload()).copyHeaders(headerMap).build();
- }
+ return MessageBuilder.withPayload(message.getPayload()).copyHeaders(headerMap).build();
+ }
catch (Exception e) {
- throw new MessagingException(message, "failed to transform message headers", e);
- }
+ throw new MessagingException(message, "failed to transform message headers", e);
+ }
}
@SuppressWarnings("rawtypes")
@@ -138,17 +145,16 @@ public class HeaderEnricher implements Transformer {
}
}
-
public static interface HeaderValueMessageProcessor extends MessageProcessor {
Boolean isOverwrite();
}
-
static abstract class AbstractHeaderValueMessageProcessor implements HeaderValueMessageProcessor {
- // null indicates no explicit setting; use header-enricher's 'default-overwrite' value
+ // null indicates no explicit setting; use header-enricher's
+ // 'default-overwrite' value
private volatile Boolean overwrite = null;
public void setOverwrite(Boolean overwrite) {
@@ -161,7 +167,6 @@ public class HeaderEnricher implements Transformer {
}
-
static class StaticHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor {
private final T value;
@@ -175,24 +180,27 @@ public class HeaderEnricher implements Transformer {
}
}
+ static class ExpressionEvaluatingHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor
+ implements BeanFactoryAware {
- static class ExpressionEvaluatingHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor implements BeanFactoryAware {
-
- private static final ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
+ private static final ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(
+ true, true));
private final ExpressionEvaluatingMessageProcessor targetProcessor;
/**
- * Create a header value processor for the given Expression and the expected type
- * of the expression evaluation result. The expectedType may be null if unknown.
+ * Create a header value processor for the given Expression and the
+ * expected type of the expression evaluation result. The expectedType
+ * may be null if unknown.
*/
public ExpressionEvaluatingHeaderValueMessageProcessor(Expression expression, Class expectedType) {
this.targetProcessor = new ExpressionEvaluatingMessageProcessor(expression, expectedType);
}
/**
- * Create a header value processor for the given expression string and the expected type
- * of the expression evaluation result. The expectedType may be null if unknown.
+ * Create a header value processor for the given expression string and
+ * the expected type of the expression evaluation result. The
+ * expectedType may be null if unknown.
*/
public ExpressionEvaluatingHeaderValueMessageProcessor(String expressionString, Class expectedType) {
Expression expression = expressionParser.parseExpression(expressionString);
@@ -209,6 +217,37 @@ public class HeaderEnricher implements Transformer {
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang
+ * .String)
+ */
+ public void setBeanName(String beanName) {
+ this.beanName = beanName;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
+ */
+ public void afterPropertiesSet() throws Exception {
+ boolean shouldOverwrite = this.defaultOverwrite;
+ for (HeaderValueMessageProcessor> processor : this.headersToAdd.values()) {
+ Boolean processerOverwrite = processor.isOverwrite();
+ if (processerOverwrite != null) {
+ shouldOverwrite |= processerOverwrite.booleanValue();
+ }
+ }
+ if (!shouldOverwrite && !this.shouldSkipNulls) {
+ logger.warn(this.beanName
+ + " is configured to not overwrite existing headers. 'shouldSkipNulls = false' will have no effect");
+ }
+ }
static class MethodInvokingHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor