Browse Source

BufferedImageHttpMessageConverter ignores empty MIME types

Issue: SPR-11581
pull/495/head
Juergen Hoeller 12 years ago
parent
commit
90e3dbb0f5
  1. 48
      spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,20 +42,24 @@ import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage; import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Implementation of {@link HttpMessageConverter} that can read and write {@link BufferedImage BufferedImages}. * Implementation of {@link HttpMessageConverter} that can read and write
* {@link BufferedImage BufferedImages}.
* *
* <p>By default, this converter can read all media types that are supported by the {@linkplain * <p>By default, this converter can read all media types that are supported
* ImageIO#getReaderMIMETypes() registered image readers}, and writes using the media type of the first available * by the {@linkplain ImageIO#getReaderMIMETypes() registered image readers},
* {@linkplain javax.imageio.ImageIO#getWriterMIMETypes() registered image writer}. This behavior can be overriden by * and writes using the media type of the first available
* setting the #setContentType(org.springframework.http.MediaType) contentType} properties. * {@linkplain javax.imageio.ImageIO#getWriterMIMETypes() registered image writer}.
* The latter can be overridden by setting the
* {@link #setDefaultContentType defaultContentType} property.
* *
* <p>If the {@link #setCacheDir(java.io.File) cacheDir} property is set to an existing directory, this converter will * <p>If the {@link #setCacheDir cacheDir} property is set, this converter
* cache image data. * will cache image data.
* *
* <p>The {@link #process(ImageReadParam)} and {@link #process(ImageWriteParam)} template methods allow subclasses to * <p>The {@link #process(ImageReadParam)} and {@link #process(ImageWriteParam)}
* override Image I/O parameters. * template methods allow subclasses to override Image I/O parameters.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
@ -72,15 +76,21 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
public BufferedImageHttpMessageConverter() { public BufferedImageHttpMessageConverter() {
String[] readerMediaTypes = ImageIO.getReaderMIMETypes(); String[] readerMediaTypes = ImageIO.getReaderMIMETypes();
for (String mediaType : readerMediaTypes) { for (String mediaType : readerMediaTypes) {
this.readableMediaTypes.add(MediaType.parseMediaType(mediaType)); if (StringUtils.hasText(mediaType)) {
this.readableMediaTypes.add(MediaType.parseMediaType(mediaType));
}
} }
String[] writerMediaTypes = ImageIO.getWriterMIMETypes(); String[] writerMediaTypes = ImageIO.getWriterMIMETypes();
if (writerMediaTypes.length > 0) { for (String mediaType : writerMediaTypes) {
this.defaultContentType = MediaType.parseMediaType(writerMediaTypes[0]); if (StringUtils.hasText(mediaType)) {
this.defaultContentType = MediaType.parseMediaType(mediaType);
break;
}
} }
} }
/** /**
* Sets the default {@code Content-Type} to be used for writing. * Sets the default {@code Content-Type} to be used for writing.
* @throws IllegalArgumentException if the given content type is not supported by the Java Image I/O API * @throws IllegalArgumentException if the given content type is not supported by the Java Image I/O API
@ -90,7 +100,7 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(defaultContentType.toString()); Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(defaultContentType.toString());
if (!imageWriters.hasNext()) { if (!imageWriters.hasNext()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"ContentType [" + defaultContentType + "] is not supported by the Java Image I/O API"); "Content-Type [" + defaultContentType + "] is not supported by the Java Image I/O API");
} }
this.defaultContentType = defaultContentType; this.defaultContentType = defaultContentType;
@ -245,15 +255,17 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
/** /**
* Template method that allows for manipulating the {@link ImageReadParam} before it is used to read an image. * Template method that allows for manipulating the {@link ImageReadParam}
* <p>Default implementation is empty. * before it is used to read an image.
* <p>The default implementation is empty.
*/ */
protected void process(ImageReadParam irp) { protected void process(ImageReadParam irp) {
} }
/** /**
* Template method that allows for manipulating the {@link ImageWriteParam} before it is used to write an image. * Template method that allows for manipulating the {@link ImageWriteParam}
* <p>Default implementation is empty. * before it is used to write an image.
* <p>The default implementation is empty.
*/ */
protected void process(ImageWriteParam iwp) { protected void process(ImageWriteParam iwp) {
} }

Loading…
Cancel
Save