Skip to content

Commit ab85aa2

Browse files
committed
BufferedImageHttpMessageConverter ignores empty MIME types
Issue: SPR-11581 (cherry picked from commit 90e3dbb)
1 parent 5c3f6a1 commit ab85aa2

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -42,20 +42,24 @@
4242
import org.springframework.http.HttpOutputMessage;
4343
import org.springframework.http.MediaType;
4444
import org.springframework.util.Assert;
45+
import org.springframework.util.StringUtils;
4546

4647
/**
47-
* Implementation of {@link HttpMessageConverter} that can read and write {@link BufferedImage BufferedImages}.
48+
* Implementation of {@link HttpMessageConverter} that can read and write
49+
* {@link BufferedImage BufferedImages}.
4850
*
49-
* <p>By default, this converter can read all media types that are supported by the {@linkplain
50-
* ImageIO#getReaderMIMETypes() registered image readers}, and writes using the media type of the first available
51-
* {@linkplain javax.imageio.ImageIO#getWriterMIMETypes() registered image writer}. This behavior can be overriden by
52-
* setting the #setContentType(org.springframework.http.MediaType) contentType} properties.
51+
* <p>By default, this converter can read all media types that are supported
52+
* by the {@linkplain ImageIO#getReaderMIMETypes() registered image readers},
53+
* and writes using the media type of the first available
54+
* {@linkplain javax.imageio.ImageIO#getWriterMIMETypes() registered image writer}.
55+
* The latter can be overridden by setting the
56+
* {@link #setDefaultContentType defaultContentType} property.
5357
*
54-
* <p>If the {@link #setCacheDir(java.io.File) cacheDir} property is set to an existing directory, this converter will
55-
* cache image data.
58+
* <p>If the {@link #setCacheDir cacheDir} property is set, this converter
59+
* will cache image data.
5660
*
57-
* <p>The {@link #process(ImageReadParam)} and {@link #process(ImageWriteParam)} template methods allow subclasses to
58-
* override Image I/O parameters.
61+
* <p>The {@link #process(ImageReadParam)} and {@link #process(ImageWriteParam)}
62+
* template methods allow subclasses to override Image I/O parameters.
5963
*
6064
* @author Arjen Poutsma
6165
* @since 3.0
@@ -72,15 +76,21 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
7276
public BufferedImageHttpMessageConverter() {
7377
String[] readerMediaTypes = ImageIO.getReaderMIMETypes();
7478
for (String mediaType : readerMediaTypes) {
75-
this.readableMediaTypes.add(MediaType.parseMediaType(mediaType));
79+
if (StringUtils.hasText(mediaType)) {
80+
this.readableMediaTypes.add(MediaType.parseMediaType(mediaType));
81+
}
7682
}
7783

7884
String[] writerMediaTypes = ImageIO.getWriterMIMETypes();
79-
if (writerMediaTypes.length > 0) {
80-
this.defaultContentType = MediaType.parseMediaType(writerMediaTypes[0]);
85+
for (String mediaType : writerMediaTypes) {
86+
if (StringUtils.hasText(mediaType)) {
87+
this.defaultContentType = MediaType.parseMediaType(mediaType);
88+
break;
89+
}
8190
}
8291
}
8392

93+
8494
/**
8595
* Sets the default {@code Content-Type} to be used for writing.
8696
* @throws IllegalArgumentException if the given content type is not supported by the Java Image I/O API
@@ -90,7 +100,7 @@ public void setDefaultContentType(MediaType defaultContentType) {
90100
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(defaultContentType.toString());
91101
if (!imageWriters.hasNext()) {
92102
throw new IllegalArgumentException(
93-
"ContentType [" + defaultContentType + "] is not supported by the Java Image I/O API");
103+
"Content-Type [" + defaultContentType + "] is not supported by the Java Image I/O API");
94104
}
95105

96106
this.defaultContentType = defaultContentType;
@@ -240,15 +250,17 @@ private ImageOutputStream createImageOutputStream(OutputStream os) throws IOExce
240250

241251

242252
/**
243-
* Template method that allows for manipulating the {@link ImageReadParam} before it is used to read an image.
244-
* <p>Default implementation is empty.
253+
* Template method that allows for manipulating the {@link ImageReadParam}
254+
* before it is used to read an image.
255+
* <p>The default implementation is empty.
245256
*/
246257
protected void process(ImageReadParam irp) {
247258
}
248259

249260
/**
250-
* Template method that allows for manipulating the {@link ImageWriteParam} before it is used to write an image.
251-
* <p>Default implementation is empty.
261+
* Template method that allows for manipulating the {@link ImageWriteParam}
262+
* before it is used to write an image.
263+
* <p>The default implementation is empty.
252264
*/
253265
protected void process(ImageWriteParam iwp) {
254266
}

0 commit comments

Comments
 (0)