1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
42
42
import org .springframework .http .HttpOutputMessage ;
43
43
import org .springframework .http .MediaType ;
44
44
import org .springframework .util .Assert ;
45
+ import org .springframework .util .StringUtils ;
45
46
46
47
/**
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}.
48
50
*
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.
53
57
*
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.
56
60
*
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.
59
63
*
60
64
* @author Arjen Poutsma
61
65
* @since 3.0
@@ -72,15 +76,21 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
72
76
public BufferedImageHttpMessageConverter () {
73
77
String [] readerMediaTypes = ImageIO .getReaderMIMETypes ();
74
78
for (String mediaType : readerMediaTypes ) {
75
- this .readableMediaTypes .add (MediaType .parseMediaType (mediaType ));
79
+ if (StringUtils .hasText (mediaType )) {
80
+ this .readableMediaTypes .add (MediaType .parseMediaType (mediaType ));
81
+ }
76
82
}
77
83
78
84
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
+ }
81
90
}
82
91
}
83
92
93
+
84
94
/**
85
95
* Sets the default {@code Content-Type} to be used for writing.
86
96
* @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) {
90
100
Iterator <ImageWriter > imageWriters = ImageIO .getImageWritersByMIMEType (defaultContentType .toString ());
91
101
if (!imageWriters .hasNext ()) {
92
102
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" );
94
104
}
95
105
96
106
this .defaultContentType = defaultContentType ;
@@ -240,15 +250,17 @@ private ImageOutputStream createImageOutputStream(OutputStream os) throws IOExce
240
250
241
251
242
252
/**
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.
245
256
*/
246
257
protected void process (ImageReadParam irp ) {
247
258
}
248
259
249
260
/**
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.
252
264
*/
253
265
protected void process (ImageWriteParam iwp ) {
254
266
}
0 commit comments