INT-2228 - Enricher fixes based on code review

This commit is contained in:
Gunnar Hillert
2011-11-08 17:50:46 -05:00
committed by Mark Fisher
parent 33e356da28
commit a22ec2f8b0
3 changed files with 82 additions and 54 deletions

View File

@@ -41,16 +41,9 @@ public class EnricherParser extends AbstractConsumerEndpointParser {
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ContentEnricher.class);
String requestChannel = element.getAttribute("request-channel");
String replyChannel = element.getAttribute("reply-channel");
if (StringUtils.hasText(requestChannel)) {
builder.addConstructorArgReference(requestChannel);
}
if (StringUtils.hasText(replyChannel)) {
builder.addConstructorArgReference(replyChannel);
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-channel");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel");
List<Element> propertyElements = DomUtils.getChildElementsByTagName(element, "property");
if (!CollectionUtils.isEmpty(propertyElements)) {

View File

@@ -36,8 +36,10 @@ import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* Content Enricher is a Message Transformer that invokes any downstream message flow via
* its request channel and then applies values from the reply Message to the original payload.
* Content Enricher is a Message Transformer that can augment a message's payload
* with either static values or by optionally invoking a downstream message flow
* via its request channel and then applying values from the reply Message to the
* original payload.
*
* @author Mark Fisher
* @since 2.1
@@ -46,7 +48,7 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
private final Map<Expression, Expression> propertyExpressions = new HashMap<Expression, Expression>();
private final Gateway gateway;
private Gateway gateway = null;
private final SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
@@ -56,38 +58,8 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
private Expression requestPayloadExpression;
/**
* Create a Content Enricher without providing a request channel. This is
* useful when only static values shall be enriched.
*/
public ContentEnricher() {
this.evaluationContext.addPropertyAccessor(new MapAccessor());
this.gateway = null;
}
/**
* Create a Content Enricher with the given request channel. An anonymous reply channel
* will be created for each request.
*/
public ContentEnricher(MessageChannel requestChannel) {
this(requestChannel, null);
}
/**
* Create a Content Enricher with the given request and reply channels.
*/
public ContentEnricher(MessageChannel requestChannel, MessageChannel replyChannel) {
Assert.notNull(requestChannel, "requestChannel must not be null");
this.gateway = new Gateway();
this.gateway.setRequestChannel(requestChannel);
if (replyChannel != null) {
this.gateway.setReplyChannel(replyChannel);
}
this.evaluationContext.addPropertyAccessor(new MapAccessor());
}
private MessageChannel requestChannel;
private MessageChannel replyChannel;
/**
* Provide the map of expressions to evaluate when enriching the target payload.
@@ -108,6 +80,25 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
}
}
/**
* Sets the content enricher's request channel. If specified, then an internal
* {@link Gateway} will be initialized. Setting a request channel is optional.
* Not setting a request channel is useful in situations where
* message payloads shall be enriched with static values only.
*/
public void setRequestChannel(MessageChannel requestChannel) {
this.requestChannel = requestChannel;
}
/**
* Sets the content enricher's reply channel. If not specified, yet the request
* channel is set, an anonymous reply channel will automatically created
* for each request.
*/
public void setReplyChannel(MessageChannel replyChannel) {
this.replyChannel = replyChannel;
}
/**
* By default the original message's payload will be used as the actual payload
* that will be send to the request-channel.
@@ -144,14 +135,32 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
this.shouldClonePayload = shouldClonePayload;
}
/**
* Initializes the Content Enricher. Will instantiate an internal Gateway if
* the requestChannel is set.
*/
@Override
public void onInit() {
super.onInit();
if (this.gateway != null) {
this.gateway.afterPropertiesSet();
if (this.replyChannel != null) {
Assert.notNull(this.requestChannel, "If the replyChannel is set, then the requestChannel must not be null");
}
if (this.requestChannel != null) {
this.gateway = new Gateway();
this.gateway.setRequestChannel(requestChannel);
if (replyChannel != null) {
this.gateway.setReplyChannel(replyChannel);
}
this.gateway.afterPropertiesSet();
}
this.evaluationContext.addPropertyAccessor(new MapAccessor());
}
@Override

View File

@@ -52,11 +52,16 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
enricher.setPropertyExpressions(propertyExpressions);
enricher.afterPropertiesSet();
Target target = new Target("replace me");
Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
enricher.handleMessage(requestMessage);
@@ -82,10 +87,13 @@ public class ContentEnricherTests {
@Test
public void testContentEnricherWithNullRequestChannel() {
ContentEnricher enricher = new ContentEnricher();
enricher.setReplyChannel(new QueueChannel());
try {
new ContentEnricher(null);
enricher.afterPropertiesSet();
} catch (IllegalArgumentException e) {
assertEquals("requestChannel must not be null", e.getMessage());
assertEquals("If the replyChannel is set, then the requestChannel must not be null", e.getMessage());
return;
}
@@ -102,11 +110,15 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
propertyExpressions.put("child.name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
enricher.setPropertyExpressions(propertyExpressions);
enricher.afterPropertiesSet();
Target target = new Target("test");
Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
enricher.handleMessage(requestMessage);
@@ -126,12 +138,16 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
enricher.setShouldClonePayload(true);
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
enricher.setPropertyExpressions(propertyExpressions);
enricher.afterPropertiesSet();
Target target = new Target("replace me");
Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
enricher.handleMessage(requestMessage);
@@ -151,12 +167,16 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
enricher.setShouldClonePayload(true);
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
enricher.setPropertyExpressions(propertyExpressions);
enricher.afterPropertiesSet();
TargetUser target = new TargetUser();
target.setName("replace me");
@@ -179,12 +199,16 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
enricher.setShouldClonePayload(true);
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
enricher.setPropertyExpressions(propertyExpressions);
enricher.afterPropertiesSet();
UncloneableTargetUser target = new UncloneableTargetUser();
target.setName("replace me");
@@ -222,7 +246,9 @@ public class ContentEnricherTests {
return new Source("John", "Doe");
}
});
ContentEnricher enricher = new ContentEnricher(requestChannel);
ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
enricher.afterPropertiesSet();