Skip to content

Commit 68dfbbe

Browse files
committed
Fix HttpHeaderMapper to support MimeType
It turns out that `contentType` header (as well as `Accept`) may be converted into `MimeType` upstream before the message is sent into HTTP Outbound Channel Adapter meanwhile the logic is only to support String and MediaType * Change the `DefaultHttpHeaderMapper` to support more generic `MimeType` for the `Content-Type` and `Accept` HTTP headers which can simply be converted to the expected `MediaType` **Cherry-pick to 5.1.x & 5.0.x** # Conflicts: # spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java
1 parent eea5089 commit 68dfbbe

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.messaging.MessageHeaders;
5656
import org.springframework.util.Assert;
5757
import org.springframework.util.CollectionUtils;
58+
import org.springframework.util.MimeType;
5859
import org.springframework.util.ObjectUtils;
5960
import org.springframework.util.PatternMatchUtils;
6061
import org.springframework.util.StringUtils;
@@ -591,8 +592,8 @@ private void setHttpHeader(HttpHeaders target, String name, Object value) {
591592
if (!CollectionUtils.isEmpty(values)) {
592593
List<MediaType> acceptableMediaTypes = new ArrayList<>();
593594
for (Object type : values) {
594-
if (type instanceof MediaType) {
595-
acceptableMediaTypes.add((MediaType) type);
595+
if (type instanceof MimeType) {
596+
acceptableMediaTypes.add(MediaType.asMediaType((MimeType) type));
596597
}
597598
else if (type instanceof String) {
598599
acceptableMediaTypes.addAll(MediaType.parseMediaTypes((String) type));
@@ -749,8 +750,8 @@ else if (value instanceof String) {
749750
}
750751
}
751752
else if (MessageHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
752-
if (value instanceof MediaType) {
753-
target.setContentType((MediaType) value);
753+
if (value instanceof MimeType) {
754+
target.setContentType(MediaType.asMediaType((MimeType) value));
754755
}
755756
else if (value instanceof String) {
756757
target.setContentType(MediaType.parseMediaType((String) value));

spring-integration-http/src/test/java/org/springframework/integration/http/support/DefaultHttpHeaderMapperFromMessageOutboundTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.integration.support.MessageBuilder;
4444
import org.springframework.messaging.Message;
4545
import org.springframework.messaging.MessageHeaders;
46+
import org.springframework.util.MimeType;
4647
import org.springframework.util.StopWatch;
4748

4849
/**
@@ -345,6 +346,18 @@ public void validateContentTypeAsMediaType() {
345346
assertEquals("html", headers.getContentType().getSubtype());
346347
}
347348

349+
@Test
350+
public void validateContentTypeAsMimeType() {
351+
HeaderMapper<HttpHeaders> mapper = DefaultHttpHeaderMapper.outboundMapper();
352+
Map<String, Object> messageHeaders = new HashMap<>();
353+
messageHeaders.put(MessageHeaders.CONTENT_TYPE, new MimeType("text", "plain"));
354+
HttpHeaders headers = new HttpHeaders();
355+
356+
mapper.fromHeaders(new MessageHeaders(messageHeaders), headers);
357+
assertEquals("text", headers.getContentType().getType());
358+
assertEquals("plain", headers.getContentType().getSubtype());
359+
}
360+
348361
// Date test
349362

350363
@Test

0 commit comments

Comments
 (0)