INT-836 refactored HeaderEnricher to delegate to MessageProcessor implementations
This commit is contained in:
@@ -44,12 +44,12 @@ public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
* Create an instance of XPathHeaderEnricher using a map with header names as keys
|
||||
* and XPathExpressionValueHolders to evaluate the values.
|
||||
*/
|
||||
public XPathHeaderEnricher(Map<String, XPathExpressionValueHolder> expressionMap) {
|
||||
public XPathHeaderEnricher(Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap) {
|
||||
super(expressionMap);
|
||||
}
|
||||
|
||||
|
||||
public static class XPathExpressionValueHolder implements ValueHolder {
|
||||
public static class XPathExpressionEvaluatingHeaderValueMessageProcessor implements HeaderValueMessageProcessor {
|
||||
|
||||
private final XPathExpression expression;
|
||||
|
||||
@@ -60,12 +60,12 @@ public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
private volatile Boolean overwrite = null;
|
||||
|
||||
|
||||
public XPathExpressionValueHolder(String expression) {
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(String expression) {
|
||||
Assert.hasText(expression, "expression must have text");
|
||||
this.expression = XPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
public XPathExpressionValueHolder(XPathExpression expression) {
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(XPathExpression expression) {
|
||||
Assert.notNull(expression, "expression must not be null");
|
||||
this.expression = expression;
|
||||
}
|
||||
@@ -78,7 +78,11 @@ public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
this.overwrite = overwrite;
|
||||
}
|
||||
|
||||
public Object evaluate(Message<?> message) {
|
||||
public Boolean isOverwrite() {
|
||||
return this.overwrite;
|
||||
}
|
||||
|
||||
public Object processMessage(Message<?> message) {
|
||||
Node node = converter.convertToNode(message.getPayload());
|
||||
Object result = this.evaluationType.evaluateXPath(this.expression, node);
|
||||
if (result instanceof String && ((String) result).length() == 0) {
|
||||
@@ -86,10 +90,6 @@ public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Boolean isOverwrite() {
|
||||
return this.overwrite;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.xml.enricher.XPathHeaderEnricher.XPathExpressionValueHolder;
|
||||
import org.springframework.integration.xml.enricher.XPathHeaderEnricher.XPathExpressionEvaluatingHeaderValueMessageProcessor;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
|
||||
/**
|
||||
@@ -39,9 +39,10 @@ public class XPathHeaderEnricherTests {
|
||||
|
||||
@Test
|
||||
public void simpleStringEvaluation() {
|
||||
Map<String, XPathExpressionValueHolder> expressionMap = new HashMap<String, XPathExpressionValueHolder>();
|
||||
expressionMap.put("one", new XPathExpressionValueHolder("/root/elementOne"));
|
||||
expressionMap.put("two", new XPathExpressionValueHolder("/root/elementTwo"));
|
||||
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap =
|
||||
new HashMap<String, XPathExpressionEvaluatingHeaderValueMessageProcessor>();
|
||||
expressionMap.put("one", new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementOne"));
|
||||
expressionMap.put("two", new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
@@ -52,8 +53,9 @@ public class XPathHeaderEnricherTests {
|
||||
|
||||
@Test
|
||||
public void nullValuesSkippedByDefault() {
|
||||
Map<String, XPathExpressionValueHolder> expressionMap = new HashMap<String, XPathExpressionValueHolder>();
|
||||
expressionMap.put("two", new XPathExpressionValueHolder("/root/elementTwo"));
|
||||
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap
|
||||
= new HashMap<String, XPathExpressionEvaluatingHeaderValueMessageProcessor>();
|
||||
expressionMap.put("two", new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
|
||||
@@ -63,8 +65,9 @@ public class XPathHeaderEnricherTests {
|
||||
|
||||
@Test
|
||||
public void notSkippingNullValues() {
|
||||
Map<String, XPathExpressionValueHolder> expressionMap = new HashMap<String, XPathExpressionValueHolder>();
|
||||
expressionMap.put("two", new XPathExpressionValueHolder("/root/elementTwo"));
|
||||
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap =
|
||||
new HashMap<String, XPathExpressionEvaluatingHeaderValueMessageProcessor>();
|
||||
expressionMap.put("two", new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo"));
|
||||
String docAsString = "<root><elementOne>1</elementOne></root>";
|
||||
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
|
||||
enricher.setShouldSkipNulls(false);
|
||||
@@ -76,9 +79,12 @@ public class XPathHeaderEnricherTests {
|
||||
|
||||
@Test
|
||||
public void numberEvaluationResult() {
|
||||
Map<String, XPathExpressionValueHolder> expressionMap = new HashMap<String, XPathExpressionValueHolder>();
|
||||
XPathExpressionValueHolder expression1 = new XPathExpressionValueHolder("/root/elementOne");
|
||||
XPathExpressionValueHolder expression2 = new XPathExpressionValueHolder("/root/elementTwo");
|
||||
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap =
|
||||
new HashMap<String, XPathExpressionEvaluatingHeaderValueMessageProcessor>();
|
||||
XPathExpressionEvaluatingHeaderValueMessageProcessor expression1 =
|
||||
new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementOne");
|
||||
XPathExpressionEvaluatingHeaderValueMessageProcessor expression2 =
|
||||
new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo");
|
||||
expression2.setEvaluationType(XPathEvaluationType.NUMBER_RESULT);
|
||||
expressionMap.put("one", expression1);
|
||||
expressionMap.put("two", expression2);
|
||||
|
||||
Reference in New Issue
Block a user