INT-1751 added mapped-request/response-type attributes to http inbound gateway/adapter

This commit is contained in:
Oleg Zhurakousky
2011-01-20 13:35:15 -05:00
parent c86ad76323
commit 129df10cd6
7 changed files with 134 additions and 14 deletions

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.3.3.201005102200-CI-R3771-B739]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.5.2.201101121000-SR1]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

View File

@@ -92,7 +92,26 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "errors-key");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "error-code");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "header-mapper");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "header-mapper");
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' " +
"attributes are allowed when a 'header-mapper' has been specified.", parserContext.extractSource(element));
}
builder.addPropertyReference("headerMapper", headerMapper);
}
else if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.http.support.DefaultHttpHeaderMapper");
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-request-headers", "outboundHeaderNames");
IntegrationNamespaceUtils.setValueIfAttributeDefined(headerMapperBuilder, element, "mapped-response-headers", "inboundHeaderNames");
builder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
}
}
private String getInputChannelAttributeName() {

View File

@@ -85,6 +85,16 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapped-response-headers" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
Comma-separated list of names of HttpHeaders to be mapped from the HTTP response into the MessageHeaders.
This can only be provided if the 'header-mapper' reference is not being set directly. The values in
this list can also be simple patterns to be matched against the header names (e.g. "foo*" or "*foo").
The String "HTTP_RESPONSE_HEADERS" will match against any of the standard HTTP Response headers.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
@@ -159,6 +169,26 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapped-request-headers" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
Comma-separated list of names of MessageHeaders to be mapped into the HttpHeaders of the HTTP request.
This can only be provided if the 'header-mapper' reference is not being set directly. The values in
this list can also be simple patterns to be matched against the header names (e.g. "foo*" or "*foo").
The String "HTTP_REQUEST_HEADERS" will match against any of the standard HTTP Request headers.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapped-response-headers" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
Comma-separated list of names of HttpHeaders to be mapped from the HTTP response into the MessageHeaders.
This can only be provided if the 'header-mapper' reference is not being set directly. The values in
this list can also be simple patterns to be matched against the header names (e.g. "foo*" or "*foo").
The String "HTTP_RESPONSE_HEADERS" will match against any of the standard HTTP Response headers.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="reply-key" type="xsd:string" />
<xsd:attribute name="reply-timeout" type="xsd:string" />
</xsd:extension>

View File

@@ -23,5 +23,8 @@
<inbound-channel-adapter id="putOrDeleteAdapter" channel="requests" supported-methods="PUT, delete"/>
<inbound-channel-adapter id="inboundController" channel="requests" view-name="foo" error-code="oops"/>
<inbound-channel-adapter id="withMappedHeaders" channel="requests"
mapped-response-headers="foo,bar"/>
</beans:beans>

View File

@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.HttpServletResponse;
@@ -34,6 +35,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
@@ -41,6 +43,7 @@ import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.http.MockHttpServletRequest;
import org.springframework.integration.http.inbound.HttpRequestHandlingController;
import org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
@@ -49,6 +52,7 @@ import org.springframework.util.MultiValueMap;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@@ -65,6 +69,9 @@ public class HttpInboundChannelAdapterParserTests {
@Autowired
private HttpRequestHandlingMessagingGateway putOrDeleteAdapter;
@Autowired
private HttpRequestHandlingMessagingGateway withMappedHeaders;
@Autowired
private HttpRequestHandlingController inboundController;
@@ -89,6 +96,22 @@ public class HttpInboundChannelAdapterParserTests {
assertEquals(1, map.get("foo").size());
assertEquals("bar", map.getFirst("foo"));
}
@Test
@SuppressWarnings("unchecked")
public void getRequestWithHeaders() throws Exception {
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeaders, "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"));
}
@Test
public void getRequestNotAllowed() throws Exception {

View File

@@ -20,5 +20,9 @@
<inbound-gateway id="inboundGateway" request-channel="requests" reply-channel="responses" convert-exceptions="true"/>
<inbound-gateway id="inboundController" request-channel="requests" reply-channel="responses" view-name="foo" error-code="oops"/>
<inbound-gateway id="withMappedHeaders" request-channel="requests"
mapped-response-headers="foo,bar"
mapped-request-headers="abc, xyz"/>
</beans:beans>

View File

@@ -21,9 +21,14 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.integration.test.util.TestUtils.getPropertyValue;
import static org.springframework.integration.test.util.TestUtils.handlerExpecting;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
@@ -31,26 +36,37 @@ import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.http.MockHttpServletRequest;
import org.springframework.integration.http.MockHttpServletResponse;
import org.springframework.integration.http.inbound.HttpRequestHandlingController;
import org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Iwein Fuld
* @author Oleg Zhurakousky
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HttpInboundGatewayParserTests {
@Autowired
@Qualifier("inboundGateway")
private HttpRequestHandlingMessagingGateway gateway;
@Autowired
@Qualifier("withMappedHeaders")
private HttpRequestHandlingMessagingGateway withMappedHeaders;
@Autowired
private HttpRequestHandlingController inboundController;
@@ -89,5 +105,30 @@ public class HttpInboundGatewayParserTests {
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
}
@Test
@SuppressWarnings("unchecked")
public void requestWithHeaders() throws Exception {
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeaders, "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");
MessageHeaders mh = new MessageHeaders(mapOfHeaders);
headers = new HttpHeaders();
headerMapper.fromHeaders(mh, headers);
assertTrue(headers.size() == 1);
List<String> abc = headers.get("X-abc");
assertEquals("abc", abc.get(0));
}
}