INT-2405 fix MessageProcessor for <header-value>
1. MethodInvokingHeaderValueMessageProcessor => MessageProcessingHeaderValueMessageProcessor because now he uses MessageProcessor 2. polishing HeaderEnricherParserSupport 3. GroovyHeaderEnricherTests: add check to HeaderValueMessageProcessor for Groovy scripts. 4. build.gradle: add dependency to 'spring-integration-test' for 'spring-integration-groovy'.
This commit is contained in:
@@ -333,6 +333,7 @@ project('spring-integration-groovy') {
|
||||
compile project(":spring-integration-scripting")
|
||||
compile "org.codehaus.groovy:groovy-all:$groovyVersion"
|
||||
compile "org.springframework:spring-context-support:$springVersion"
|
||||
testCompile project(":spring-integration-test")
|
||||
}
|
||||
bundlor {
|
||||
bundleSymbolicName = 'org.springframework.integration.groovy'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -32,6 +32,8 @@ import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.springframework.integration.expression.DynamicExpression;
|
||||
import org.springframework.integration.transformer.HeaderEnricher;
|
||||
|
||||
/**
|
||||
* Base support class for 'header-enricher' parsers.
|
||||
@@ -50,7 +52,7 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
|
||||
@Override
|
||||
protected final String getTransformerClassName() {
|
||||
return IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher";
|
||||
return HeaderEnricher.class.getName();
|
||||
}
|
||||
|
||||
protected final void addElementToHeaderMapping(String elementName, String headerName) {
|
||||
@@ -85,7 +87,7 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
String elementName = node.getLocalName();
|
||||
Class<?> headerType = null;
|
||||
if ("header".equals(elementName)) {
|
||||
headerName = headerElement.getAttribute("name");
|
||||
headerName = headerElement.getAttribute(NAME_ATTRIBUTE);
|
||||
}
|
||||
else {
|
||||
headerName = elementToNameMap.get(elementName);
|
||||
@@ -114,9 +116,9 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
}
|
||||
if (headerName != null) {
|
||||
String value = headerElement.getAttribute("value");
|
||||
String ref = headerElement.getAttribute("ref");
|
||||
String method = headerElement.getAttribute("method");
|
||||
String expression = headerElement.getAttribute("expression");
|
||||
String ref = headerElement.getAttribute(REF_ATTRIBUTE);
|
||||
String method = headerElement.getAttribute(METHOD_ATTRIBUTE);
|
||||
String expression = headerElement.getAttribute(EXPRESSION_ATTRIBUTE);
|
||||
|
||||
Element beanElement = null;
|
||||
Element scriptElement = null;
|
||||
@@ -188,8 +190,7 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$ExpressionEvaluatingHeaderValueMessageProcessor");
|
||||
if (expressionElement != null) {
|
||||
BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.integration.expression.DynamicExpression");
|
||||
BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicExpression.class);
|
||||
dynamicExpressionBuilder.addConstructorArgValue(expressionElement.getAttribute("key"));
|
||||
dynamicExpressionBuilder.addConstructorArgReference(expressionElement.getAttribute("source"));
|
||||
valueProcessorBuilder.addConstructorArgValue(dynamicExpressionBuilder.getBeanDefinition());
|
||||
@@ -206,9 +207,11 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
}
|
||||
if (hasMethod || isScript) {
|
||||
valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$MethodInvokingHeaderValueMessageProcessor");
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$MessageProcessingHeaderValueMessageProcessor");
|
||||
valueProcessorBuilder.addConstructorArgValue(innerComponentDefinition);
|
||||
valueProcessorBuilder.addConstructorArgValue(hasMethod ? method : null);
|
||||
if (hasMethod) {
|
||||
valueProcessorBuilder.addConstructorArgValue(method);
|
||||
}
|
||||
}
|
||||
else {
|
||||
valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
@@ -223,7 +226,7 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
}
|
||||
if (hasMethod) {
|
||||
valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$MethodInvokingHeaderValueMessageProcessor");
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$MessageProcessingHeaderValueMessageProcessor");
|
||||
valueProcessorBuilder.addConstructorArgReference(ref);
|
||||
valueProcessorBuilder.addConstructorArgValue(method);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -44,6 +44,7 @@ import org.springframework.integration.support.MessageBuilder;
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author David Turanski
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class HeaderEnricher implements Transformer, BeanNameAware, InitializingBean {
|
||||
|
||||
@@ -249,11 +250,19 @@ public class HeaderEnricher implements Transformer, BeanNameAware, InitializingB
|
||||
}
|
||||
}
|
||||
|
||||
static class MethodInvokingHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor<Object> {
|
||||
static class MessageProcessingHeaderValueMessageProcessor extends AbstractHeaderValueMessageProcessor<Object> {
|
||||
|
||||
private final MethodInvokingMessageProcessor<Object> targetProcessor;
|
||||
private final MessageProcessor<?> targetProcessor;
|
||||
|
||||
public MethodInvokingHeaderValueMessageProcessor(Object targetObject, String method) {
|
||||
public <T> MessageProcessingHeaderValueMessageProcessor(MessageProcessor<T> targetProcessor) {
|
||||
this.targetProcessor = targetProcessor;
|
||||
}
|
||||
|
||||
public MessageProcessingHeaderValueMessageProcessor(Object targetObject) {
|
||||
this(targetObject, null);
|
||||
}
|
||||
|
||||
public MessageProcessingHeaderValueMessageProcessor(Object targetObject, String method) {
|
||||
this.targetProcessor = new MethodInvokingMessageProcessor<Object>(targetObject, method);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
<int:channel id="inputB"/>
|
||||
|
||||
<int:header-enricher input-channel="inputB" output-channel="outputB">
|
||||
<int:header-enricher id="headerEnricherWithInlineGroovyScript" input-channel="inputB" output-channel="outputB">
|
||||
<int:header name="TEST_HEADER">
|
||||
<int-groovy:script>
|
||||
<![CDATA[
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -17,6 +17,10 @@
|
||||
package org.springframework.integration.groovy.config;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -24,12 +28,18 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.transformer.HeaderEnricher;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Artem Bilan
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@@ -48,14 +58,26 @@ public class GroovyHeaderEnricherTests {
|
||||
@Autowired
|
||||
private QueueChannel outputB;
|
||||
|
||||
@Autowired
|
||||
private EventDrivenConsumer headerEnricherWithInlineGroovyScript;
|
||||
|
||||
@Test
|
||||
public void referencedScript() throws Exception{
|
||||
inputA.send(new GenericMessage<String>("Hello"));
|
||||
assertEquals("groovy", outputA.receive(1000).getHeaders().get("TEST_HEADER"));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void inlineScript() throws Exception{
|
||||
Map<String, HeaderEnricher.HeaderValueMessageProcessor> headers =
|
||||
TestUtils.getPropertyValue(headerEnricherWithInlineGroovyScript, "handler.transformer.headersToAdd", Map.class);
|
||||
assertEquals(1, headers.size());
|
||||
HeaderEnricher.HeaderValueMessageProcessor headerValueMessageProcessor = headers.get("TEST_HEADER");
|
||||
assertThat(headerValueMessageProcessor.getClass().getName(), Matchers.containsString("HeaderEnricher$MessageProcessingHeaderValueMessageProcessor"));
|
||||
Object targetProcessor = TestUtils.getPropertyValue(headerValueMessageProcessor, "targetProcessor");
|
||||
assertEquals(GroovyScriptExecutingMessageProcessor.class, targetProcessor.getClass());
|
||||
|
||||
inputB.send(new GenericMessage<String>("Hello"));
|
||||
assertEquals("groovy", outputB.receive(1000).getHeaders().get("TEST_HEADER"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user