INT-1681 The values 'HTTP_REQUEST_HEADERS' and 'HTTP_RESPONSE_HEADERS' are now 'patterns' for matching standard headers in DefaultHttpHeaderMapper

This commit is contained in:
Mark Fisher
2010-12-12 17:24:00 -05:00
parent 37553b6e5f
commit 28d5205970
4 changed files with 90 additions and 4 deletions

View File

@@ -214,6 +214,10 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders> {
WWW_AUTHENTICATE
};
public static final String HTTP_REQUEST_HEADER_NAME_PATTERN = "HTTP_REQUEST_HEADERS";
public static final String HTTP_RESPONSE_HEADER_NAME_PATTERN = "HTTP_RESPONSE_HEADERS";
private volatile String[] outboundHeaderNames = new String[0];
@@ -307,6 +311,14 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders> {
if (PatternMatchUtils.simpleMatch(pattern, headerName)) {
return true;
}
else if (HTTP_REQUEST_HEADER_NAME_PATTERN.equals(pattern)
&& ObjectUtils.containsElement(HTTP_REQUEST_HEADER_NAMES, headerName)) {
return true;
}
else if (HTTP_RESPONSE_HEADER_NAME_PATTERN.equals(pattern)
&& ObjectUtils.containsElement(HTTP_RESPONSE_HEADER_NAMES, headerName)) {
return true;
}
}
}
return false;

View File

@@ -239,7 +239,9 @@
<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.
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>
@@ -334,7 +336,9 @@
<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.
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>
@@ -342,7 +346,9 @@
<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.
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>

View File

@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNull;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
@@ -32,6 +33,7 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.mapping.HeaderMapper;
import org.springframework.util.CollectionUtils;
@@ -299,6 +301,23 @@ public class DefaultHttpHeaderMapperFromMessageInboundTests {
assertEquals("abcdef-value", headers.getFirst("X-abcdef"));
}
@Test
public void validateCustomHeaderNamePatternsAndStandardResponseHeadersMappedToHttpHeaders() throws Exception{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
mapper.setOutboundHeaderNames(new String[] {"foo*", "HTTP_RESPONSE_HEADERS"});
Map<String, Object> messageHeaders = new HashMap<String, Object>();
messageHeaders.put("foobar", "abc");
messageHeaders.put("Accept", "text/html");
messageHeaders.put("Content-Type", "text/xml");
HttpHeaders headers = new HttpHeaders();
mapper.fromHeaders(new MessageHeaders(messageHeaders), headers);
assertEquals(2, headers.size());
assertTrue(headers.getAccept().isEmpty());
assertEquals(MediaType.TEXT_XML, headers.getContentType());
assertEquals(1, headers.get("X-foobar").size());
assertEquals("abc", headers.getFirst("X-foobar"));
}
@Test
public void validateCustomHeaderNamesMappedFromHttpHeaders() throws Exception{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
@@ -335,4 +354,19 @@ public class DefaultHttpHeaderMapperFromMessageInboundTests {
assertEquals("abcdef-value", result.get("abcdef"));
}
@Test
public void validateCustomHeaderNamePatternsAndStandardRequestHeadersMappedFromHttpHeaders() throws Exception{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
mapper.setInboundHeaderNames(new String[] {"foo*", "HTTP_REQUEST_HEADERS"});
HttpHeaders headers = new HttpHeaders();
headers.set("foobar", "abc");
headers.setAccept(Collections.singletonList(MediaType.TEXT_XML));
headers.setLocation(new URI("http://example.org"));
Map<String, ?> result = mapper.toHeaders(headers);
assertEquals(2, result.size());
assertNull(result.get("Location"));
assertEquals("abc", result.get("foobar"));
assertEquals(MediaType.TEXT_XML, result.get("Accept"));
}
}

View File

@@ -25,6 +25,7 @@ import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
@@ -507,7 +508,7 @@ public class DefaultHttpHeaderMapperFromMessageOutboundTests {
assertTrue(headers.get("X-foo").size() == 1);
assertEquals("foo", headers.get("X-foo").get(0));
}
@Test
public void validateCustomHeaderWithHeaderNamePatterns() throws ParseException{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
@@ -535,4 +536,37 @@ public class DefaultHttpHeaderMapperFromMessageOutboundTests {
assertEquals("abcdef-value", headers.getFirst("X-abcdef"));
}
@Test
public void validateCustomHeaderWithHeaderNamePatternsAndStandardRequestHeaders() throws ParseException{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
mapper.setOutboundHeaderNames(new String[]{"foo*", "HTTP_REQUEST_HEADERS"});
Map<String, Object> messageHeaders = new HashMap<String, Object>();
messageHeaders.put("foobar", "abc");
messageHeaders.put("Content-Type", "text/html");
messageHeaders.put("Accept", "text/xml");
HttpHeaders headers = new HttpHeaders();
mapper.fromHeaders(new MessageHeaders(messageHeaders), headers);
assertEquals(3, headers.size());
assertEquals(1, headers.get("X-foobar").size());
assertEquals("abc", headers.getFirst("X-foobar"));
assertEquals("text/html", headers.getContentType().toString());
assertEquals(1, headers.getAccept().size());
assertEquals(MediaType.TEXT_XML, headers.getAccept().get(0));
}
@Test
public void validateCustomHeaderWithHeaderNamePatternsAndStandardResponseHeaders() throws ParseException{
DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
mapper.setInboundHeaderNames(new String[]{"foo*", "HTTP_RESPONSE_HEADERS"});
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("foobar", "abc");
httpHeaders.setContentType(MediaType.TEXT_HTML);
httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
Map<String, ?> messageHeaders = mapper.toHeaders(httpHeaders);
assertEquals(2, messageHeaders.size());
assertNull(messageHeaders.get("Accept"));
assertEquals("abc", messageHeaders.get("foobar"));
assertEquals("text/html", messageHeaders.get("Content-Type").toString());
}
}