INT-878 The sub-elements of <header-enricher/> now support a "type" attribute.
This commit is contained in:
@@ -85,24 +85,27 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar
|
||||
Class<?> headerType = null;
|
||||
if ("header".equals(elementName)) {
|
||||
headerName = headerElement.getAttribute("name");
|
||||
String headerTypeName = headerElement.getAttribute("type");
|
||||
if (StringUtils.hasText(headerTypeName)) {
|
||||
ClassLoader classLoader = parserContext.getReaderContext().getBeanClassLoader();
|
||||
if (classLoader != null) {
|
||||
try {
|
||||
headerType = ClassUtils.forName(headerTypeName, classLoader);
|
||||
}
|
||||
catch (Exception e) {
|
||||
parserContext.getReaderContext().error("unable to resolve type [" +
|
||||
headerTypeName + "] for header '" + headerName + "'", element, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
headerName = elementToNameMap.get(elementName);
|
||||
headerType = elementToTypeMap.get(elementName);
|
||||
}
|
||||
if (headerType == null) {
|
||||
String headerTypeName = headerElement.getAttribute("type");
|
||||
if (StringUtils.hasText(headerTypeName)) {
|
||||
ClassLoader classLoader = parserContext.getReaderContext().getBeanClassLoader();
|
||||
if (classLoader == null) {
|
||||
classLoader = getClass().getClassLoader();
|
||||
}
|
||||
try {
|
||||
headerType = ClassUtils.forName(headerTypeName, classLoader);
|
||||
}
|
||||
catch (Exception e) {
|
||||
parserContext.getReaderContext().error("unable to resolve type [" +
|
||||
headerTypeName + "] for header '" + headerName + "'", element, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (headerName != null) {
|
||||
String value = headerElement.getAttribute("value");
|
||||
String ref = headerElement.getAttribute("ref");
|
||||
|
||||
@@ -937,6 +937,14 @@
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="referenceOrValueHeaderType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Provides a header value for the given header name. Requires
|
||||
exactly one of the 'ref', 'value', or 'expression' attributes.
|
||||
The 'type' attribute allows for the specification of the expected
|
||||
type when using a 'value' or 'expression', but it is optional.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="referenceHeaderType">
|
||||
<xsd:attribute name="value" type="xsd:string">
|
||||
@@ -954,6 +962,13 @@
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="type" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:java.lang.Class"><![CDATA[
|
||||
The fully qualified class name of the header value's expected type.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
|
||||
@@ -38,6 +38,10 @@
|
||||
<correlation-id value="ABC"/>
|
||||
</header-enricher>
|
||||
|
||||
<header-enricher input-channel="correlationIdValueWithTypeInput">
|
||||
<correlation-id value="123" type="java.lang.Long"/>
|
||||
</header-enricher>
|
||||
|
||||
<header-enricher input-channel="correlationIdRefInput">
|
||||
<correlation-id ref="testCorrelationId"/>
|
||||
</header-enricher>
|
||||
@@ -70,4 +74,12 @@
|
||||
<header name="testHeader2" expression="headers.testHeader1 + 'bar'"/>
|
||||
</header-enricher>
|
||||
|
||||
<header-enricher input-channel="expressionWithDateTypeInput">
|
||||
<header name="currentDate" expression="new java.util.Date()" type="java.util.Date"/>
|
||||
</header-enricher>
|
||||
|
||||
<header-enricher input-channel="expressionWithLongTypeInput">
|
||||
<header name="number" expression="12345" type="java.lang.Long"/>
|
||||
</header-enricher>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -18,6 +18,9 @@ package org.springframework.integration.config.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -80,6 +83,17 @@ public class HeaderEnricherTests {
|
||||
assertEquals("ABC", result.getHeaders().getCorrelationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correlationIdValueWithType() {
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway();
|
||||
gateway.setRequestChannel(context.getBean("correlationIdValueWithTypeInput", MessageChannel.class));
|
||||
Message<?> result = gateway.sendAndReceiveMessage("test");
|
||||
assertNotNull(result);
|
||||
Object correlationId = result.getHeaders().getCorrelationId();
|
||||
assertEquals(Long.class, correlationId.getClass());
|
||||
assertEquals(new Long(123), correlationId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correlationIdRef() {
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway();
|
||||
@@ -135,6 +149,28 @@ public class HeaderEnricherTests {
|
||||
assertEquals("foobar", result.getHeaders().get("testHeader2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionWithDateType() {
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway();
|
||||
gateway.setRequestChannel(context.getBean("expressionWithDateTypeInput", MessageChannel.class));
|
||||
Message<?> result = gateway.sendAndReceiveMessage("test");
|
||||
assertNotNull(result);
|
||||
Object headerValue = result.getHeaders().get("currentDate");
|
||||
assertEquals(Date.class, headerValue.getClass());
|
||||
Date date = (Date) headerValue;
|
||||
assertTrue(new Date().getTime() - date.getTime() < 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionWithLongType() {
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway();
|
||||
gateway.setRequestChannel(context.getBean("expressionWithLongTypeInput", MessageChannel.class));
|
||||
Message<?> result = gateway.sendAndReceiveMessage("test");
|
||||
assertNotNull(result);
|
||||
assertEquals(Long.class, result.getHeaders().get("number").getClass());
|
||||
assertEquals(new Long(12345), result.getHeaders().get("number"));
|
||||
}
|
||||
|
||||
|
||||
public static class TestBean {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user