INT-1866 moved HeaderMapper building logic to parsers
This commit is contained in:
@@ -98,6 +98,8 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
|
||||
String headerMapper = element.getAttribute("header-mapper");
|
||||
String mappedRequestHeaders = element.getAttribute("mapped-request-headers");
|
||||
String mappedResponseHeaders = element.getAttribute("mapped-response-headers");
|
||||
|
||||
|
||||
if (StringUtils.hasText(headerMapper)) {
|
||||
if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
|
||||
parserContext.getReaderContext().error("Neither 'mappped-request-headers' or 'mapped-response-headers' " +
|
||||
@@ -105,9 +107,11 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
|
||||
}
|
||||
builder.addPropertyReference("headerMapper", headerMapper);
|
||||
}
|
||||
else if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
|
||||
else {
|
||||
BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.integration.http.support.DefaultHttpHeaderMapper");
|
||||
"org.springframework.integration.http.support.DefaultHttpHeaderMapper");
|
||||
headerMapperBuilder.setFactoryMethod("inboundMapper");
|
||||
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-request-headers", "inboundHeaderNames");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-response-headers", "outboundHeaderNames");
|
||||
builder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
|
||||
|
||||
@@ -72,7 +72,9 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
|
||||
else if (StringUtils.hasText(mappedRequestHeaders)) {
|
||||
BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.integration.http.support.DefaultHttpHeaderMapper");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-request-headers", "outboundHeaderNames");
|
||||
if (StringUtils.hasText(mappedRequestHeaders)) {
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-request-headers", "outboundHeaderNames");
|
||||
}
|
||||
builder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
|
||||
}
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "charset");
|
||||
|
||||
@@ -71,9 +71,10 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
|
||||
}
|
||||
builder.addPropertyReference("headerMapper", headerMapper);
|
||||
}
|
||||
else if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
|
||||
else {//if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
|
||||
BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.integration.http.support.DefaultHttpHeaderMapper");
|
||||
headerMapperBuilder.setFactoryMethod("outboundMapper");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-request-headers", "outboundHeaderNames");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-response-headers", "inboundHeaderNames");
|
||||
builder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration/http"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
<beans:beans
|
||||
xmlns="http://www.springframework.org/schema/integration/http"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/http
|
||||
http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
|
||||
|
||||
<si:publish-subscribe-channel id="requests"/>
|
||||
|
||||
@@ -24,5 +25,19 @@
|
||||
<inbound-gateway id="withMappedHeaders" request-channel="requests"
|
||||
mapped-response-headers="abc, xyz"
|
||||
mapped-request-headers="foo,bar"/>
|
||||
|
||||
<inbound-gateway id="withMappedHeadersAndConverter" request-channel="requests"
|
||||
mapped-response-headers="abc, xyz, person"
|
||||
mapped-request-headers="foo,bar"/>
|
||||
|
||||
<beans:bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
|
||||
<beans:property name="converters">
|
||||
<util:set>
|
||||
<beans:bean class="org.springframework.integration.http.config.HttpInboundGatewayParserTests.PersonConverter"/>
|
||||
</util:set>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -35,8 +35,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
@@ -59,6 +63,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class HttpInboundGatewayParserTests {
|
||||
@Autowired
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("inboundGateway")
|
||||
@@ -67,6 +73,10 @@ public class HttpInboundGatewayParserTests {
|
||||
@Autowired
|
||||
@Qualifier("withMappedHeaders")
|
||||
private HttpRequestHandlingMessagingGateway withMappedHeaders;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("withMappedHeadersAndConverter")
|
||||
private HttpRequestHandlingMessagingGateway withMappedHeadersAndConverter;
|
||||
|
||||
@Autowired
|
||||
private HttpRequestHandlingController inboundController;
|
||||
@@ -130,5 +140,55 @@ public class HttpInboundGatewayParserTests {
|
||||
List<String> abc = headers.get("X-abc");
|
||||
assertEquals("abc", abc.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void requestWithHeadersWithConversionService() throws Exception {
|
||||
DefaultHttpHeaderMapper headerMapper =
|
||||
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeadersAndConverter, "headerMapper");
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("foo", "foo");
|
||||
headers.set("bar", "bar");
|
||||
headers.set("baz", "baz");
|
||||
Map<String, String> map = (Map<String, String>) headerMapper.toHeaders(headers);
|
||||
assertTrue(map.size() == 2);
|
||||
assertEquals("foo", map.get("foo"));
|
||||
assertEquals("bar", map.get("bar"));
|
||||
|
||||
Map<String, Object> mapOfHeaders = new HashMap<String, Object>();
|
||||
mapOfHeaders.put("abc", "abc");
|
||||
Person person = new Person();
|
||||
person.setName("Oleg");
|
||||
mapOfHeaders.put("person", person);
|
||||
MessageHeaders mh = new MessageHeaders(mapOfHeaders);
|
||||
headers = new HttpHeaders();
|
||||
headerMapper.fromHeaders(mh, headers);
|
||||
assertTrue(headers.size() == 2);
|
||||
List<String> abc = headers.get("X-abc");
|
||||
assertEquals("abc", abc.get(0));
|
||||
List<String> personHeaders = headers.get("X-person");
|
||||
assertEquals("Oleg", personHeaders.get(0));
|
||||
}
|
||||
|
||||
public static class Person{
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PersonConverter implements Converter<Person, String>{
|
||||
|
||||
public String convert(Person source) {
|
||||
return source.getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user