Skip to content

Commit 2afae43

Browse files
committed
Update list of support multipart media types
See gh-24582
1 parent e706fcb commit 2afae43

File tree

8 files changed

+109
-37
lines changed

8 files changed

+109
-37
lines changed

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -301,6 +301,18 @@ public class MediaType extends MimeType implements Serializable {
301301
*/
302302
public static final String MULTIPART_MIXED_VALUE = "multipart/mixed";
303303

304+
/**
305+
* Public constant media type for {@code multipart/related}.
306+
* @since 5.2.5
307+
*/
308+
public static final MediaType MULTIPART_RELATED;
309+
310+
/**
311+
* A String equivalent of {@link MediaType#MULTIPART_RELATED}.
312+
* @since 5.2.5
313+
*/
314+
public static final String MULTIPART_RELATED_VALUE = "multipart/related";
315+
304316
/**
305317
* Public constant media type for {@code text/event-stream}.
306318
* @since 4.3.6
@@ -381,6 +393,7 @@ public class MediaType extends MimeType implements Serializable {
381393
IMAGE_PNG = new MediaType("image", "png");
382394
MULTIPART_FORM_DATA = new MediaType("multipart", "form-data");
383395
MULTIPART_MIXED = new MediaType("multipart", "mixed");
396+
MULTIPART_RELATED = new MediaType("multipart", "related");
384397
TEXT_EVENT_STREAM = new MediaType("text", "event-stream");
385398
TEXT_HTML = new MediaType("text", "html");
386399
TEXT_MARKDOWN = new MediaType("text", "markdown");

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageReader.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.http.codec.multipart;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -55,6 +56,9 @@ public class MultipartHttpMessageReader extends LoggingCodecSupport
5556
private static final ResolvableType MULTIPART_VALUE_TYPE = ResolvableType.forClassWithGenerics(
5657
MultiValueMap.class, String.class, Part.class);
5758

59+
static final List<MediaType> MIME_TYPES = Collections.unmodifiableList(Arrays.asList(
60+
MediaType.MULTIPART_FORM_DATA, MediaType.MULTIPART_MIXED, MediaType.MULTIPART_RELATED));
61+
5862

5963
private final HttpMessageReader<Part> partReader;
6064

@@ -75,13 +79,22 @@ public HttpMessageReader<Part> getPartReader() {
7579

7680
@Override
7781
public List<MediaType> getReadableMediaTypes() {
78-
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
82+
return MIME_TYPES;
7983
}
8084

8185
@Override
8286
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {
83-
return MULTIPART_VALUE_TYPE.isAssignableFrom(elementType) &&
84-
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
87+
if (MULTIPART_VALUE_TYPE.isAssignableFrom(elementType)) {
88+
if (mediaType == null) {
89+
return true;
90+
}
91+
for (MediaType supportedMediaType : MIME_TYPES) {
92+
if (supportedMediaType.isCompatibleWith(mediaType)) {
93+
return true;
94+
}
95+
}
96+
}
97+
return false;
8598
}
8699

87100

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,8 +132,7 @@ public MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters,
132132
}
133133

134134
private static List<MediaType> initMediaTypes(@Nullable HttpMessageWriter<?> formWriter) {
135-
List<MediaType> result = new ArrayList<>();
136-
result.add(MediaType.MULTIPART_FORM_DATA);
135+
List<MediaType> result = new ArrayList<>(MultipartHttpMessageReader.MIME_TYPES);
137136
if (formWriter != null) {
138137
result.addAll(formWriter.getWritableMediaTypes());
139138
}
@@ -197,7 +196,7 @@ public Mono<Void> write(Publisher<? extends MultiValueMap<String, ?>> inputStrea
197196
return Mono.from(inputStream)
198197
.flatMap(map -> {
199198
if (this.formWriter == null || isMultipart(map, mediaType)) {
200-
return writeMultipart(map, outputMessage, hints);
199+
return writeMultipart(map, outputMessage, mediaType, hints);
201200
}
202201
else {
203202
@SuppressWarnings("unchecked")
@@ -209,7 +208,7 @@ public Mono<Void> write(Publisher<? extends MultiValueMap<String, ?>> inputStrea
209208

210209
private boolean isMultipart(MultiValueMap<String, ?> map, @Nullable MediaType contentType) {
211210
if (contentType != null) {
212-
return MediaType.MULTIPART_FORM_DATA.includes(contentType);
211+
return contentType.getType().equalsIgnoreCase("multipart");
213212
}
214213
for (List<?> values : map.values()) {
215214
for (Object value : values) {
@@ -221,16 +220,22 @@ private boolean isMultipart(MultiValueMap<String, ?> map, @Nullable MediaType co
221220
return false;
222221
}
223222

224-
private Mono<Void> writeMultipart(
225-
MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
223+
private Mono<Void> writeMultipart(MultiValueMap<String, ?> map,
224+
ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
226225

227226
byte[] boundary = generateMultipartBoundary();
228227

229-
Map<String, String> params = new HashMap<>(2);
228+
Map<String, String> params = new HashMap<>();
229+
if (mediaType != null) {
230+
params.putAll(mediaType.getParameters());
231+
}
230232
params.put("boundary", new String(boundary, StandardCharsets.US_ASCII));
231233
params.put("charset", getCharset().name());
232234

233-
outputMessage.getHeaders().setContentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params));
235+
mediaType = (mediaType != null ? mediaType : MediaType.MULTIPART_FORM_DATA);
236+
mediaType = new MediaType(mediaType, params);
237+
238+
outputMessage.getHeaders().setContentType(mediaType);
234239

235240
LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " +
236241
(isEnableLoggingRequestDetails() ?

spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import java.nio.file.OpenOption;
2626
import java.nio.file.Path;
2727
import java.nio.file.StandardOpenOption;
28-
import java.util.Collections;
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.Optional;
@@ -153,13 +152,22 @@ public int getMaxParts() {
153152

154153
@Override
155154
public List<MediaType> getReadableMediaTypes() {
156-
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
155+
return MultipartHttpMessageReader.MIME_TYPES;
157156
}
158157

159158
@Override
160159
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {
161-
return Part.class.equals(elementType.toClass()) &&
162-
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
160+
if (Part.class.equals(elementType.toClass())) {
161+
if (mediaType == null) {
162+
return true;
163+
}
164+
for (MediaType supportedMediaType : getReadableMediaTypes()) {
165+
if (supportedMediaType.isCompatibleWith(mediaType)) {
166+
return true;
167+
}
168+
}
169+
}
170+
return false;
163171
}
164172

165173
@Override

spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,8 +160,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
160160
*/
161161
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
162162

163-
static final MediaType MULTIPART_ALL = new MediaType("multipart", "*");
164-
165163
private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE =
166164
new MediaType(MediaType.APPLICATION_FORM_URLENCODED, DEFAULT_CHARSET);
167165

@@ -301,7 +299,7 @@ public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
301299
return true;
302300
}
303301
for (MediaType supportedMediaType : getSupportedMediaTypes()) {
304-
if (MULTIPART_ALL.includes(supportedMediaType)) {
302+
if (supportedMediaType.getType().equalsIgnoreCase("multipart")) {
305303
// We can't read multipart, so skip this supported media type.
306304
continue;
307305
}
@@ -369,7 +367,7 @@ public void write(MultiValueMap<String, ?> map, @Nullable MediaType contentType,
369367

370368
private boolean isMultipart(MultiValueMap<String, ?> map, @Nullable MediaType contentType) {
371369
if (contentType != null) {
372-
return MULTIPART_ALL.includes(contentType);
370+
return contentType.getType().equalsIgnoreCase("multipart");
373371
}
374372
for (List<?> values : map.values()) {
375373
for (Object value : values) {

spring-web/src/test/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriterTests.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,17 +68,23 @@ public void canWrite() {
6868
assertThat(this.writer.canWrite(
6969
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
7070
MediaType.MULTIPART_FORM_DATA)).isTrue();
71-
7271
assertThat(this.writer.canWrite(
73-
ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class),
74-
MediaType.MULTIPART_FORM_DATA)).isFalse();
72+
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
73+
MediaType.MULTIPART_MIXED)).isTrue();
74+
assertThat(this.writer.canWrite(
75+
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
76+
MediaType.MULTIPART_RELATED)).isTrue();
7577
assertThat(this.writer.canWrite(
7678
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
7779
MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
80+
81+
assertThat(this.writer.canWrite(
82+
ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class),
83+
MediaType.MULTIPART_FORM_DATA)).isFalse();
7884
}
7985

8086
@Test
81-
public void writeMultipart() throws Exception {
87+
public void writeMultipartFormData() throws Exception {
8288
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
8389
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
8490
@Override
@@ -109,7 +115,8 @@ public String getFilename() {
109115
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
110116

111117
Map<String, Object> hints = Collections.emptyMap();
112-
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
118+
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints)
119+
.block(Duration.ofSeconds(5));
113120

114121
MultiValueMap<String, Part> requestParts = parse(hints);
115122
assertThat(requestParts.size()).isEqualTo(7);
@@ -167,6 +174,33 @@ public String getFilename() {
167174
assertThat(value).isEqualTo("AaBbCc");
168175
}
169176

177+
@Test // gh-24582
178+
public void writeMultipartRelated() {
179+
180+
MediaType mediaType = MediaType.parseMediaType("multipart/related;type=foo");
181+
182+
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
183+
bodyBuilder.part("name 1", "value 1");
184+
bodyBuilder.part("name 2", "value 2");
185+
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
186+
187+
Map<String, Object> hints = Collections.emptyMap();
188+
this.writer.write(result, null, mediaType, this.response, hints)
189+
.block(Duration.ofSeconds(5));
190+
191+
MediaType contentType = this.response.getHeaders().getContentType();
192+
assertThat(contentType).isNotNull();
193+
assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
194+
assertThat(contentType.getParameter("type")).isEqualTo("foo");
195+
assertThat(contentType.getParameter("boundary")).isNotEmpty();
196+
assertThat(contentType.getParameter("charset")).isEqualTo("UTF-8");
197+
198+
MultiValueMap<String, Part> requestParts = parse(hints);
199+
assertThat(requestParts.size()).isEqualTo(2);
200+
assertThat(requestParts.getFirst("name 1").name()).isEqualTo("name 1");
201+
assertThat(requestParts.getFirst("name 2").name()).isEqualTo("name 2");
202+
}
203+
170204
@SuppressWarnings("ConstantConditions")
171205
private String decodeToString(Part part) {
172206
return StringDecoder.textPlainOnly().decodeToMono(part.content(),

spring-web/src/test/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReaderTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,11 +68,13 @@ public class SynchronossPartHttpMessageReaderTests extends AbstractLeakCheckingT
6868
private static final ResolvableType PARTS_ELEMENT_TYPE =
6969
forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
7070

71+
7172
@Test
7273
void canRead() {
73-
assertThat(this.reader.canRead(
74-
PARTS_ELEMENT_TYPE,
75-
MediaType.MULTIPART_FORM_DATA)).isTrue();
74+
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_FORM_DATA)).isTrue();
75+
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_MIXED)).isTrue();
76+
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_RELATED)).isTrue();
77+
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, null)).isTrue();
7678

7779
assertThat(this.reader.canRead(
7880
forClassWithGenerics(MultiValueMap.class, String.class, Object.class),

spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,6 @@
5252
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
5353
import static org.springframework.http.MediaType.MULTIPART_MIXED;
5454
import static org.springframework.http.MediaType.TEXT_XML;
55-
import static org.springframework.http.converter.FormHttpMessageConverter.MULTIPART_ALL;
5655

5756
/**
5857
* Unit tests for {@link FormHttpMessageConverter} and
@@ -278,7 +277,7 @@ private void assertCanRead(Class<?> clazz, MediaType mediaType) {
278277
}
279278

280279
private void asssertCannotReadMultipart() {
281-
assertCannotRead(MULTIPART_ALL);
280+
assertCannotRead(new MediaType("multipart", "*"));
282281
assertCannotRead(MULTIPART_FORM_DATA);
283282
assertCannotRead(MULTIPART_MIXED);
284283
assertCannotRead(MULTIPART_RELATED);

0 commit comments

Comments
 (0)