Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7763

BufferedImageHttpMessageConverter not using defaultContentType

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.0.5, 3.1.1
    • Fix Version/s: 3.2.1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Looking at the AnnotationMethodHandler.writeWithmessageConverters() (line: 972). The code loops through each of the acceptedMediaTypes asking the messageConverter if it canWrite(). Using an example acceptedMediaTypes from Firefox would look like:

      [text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8]
      

      Each of the mediaTypes would be passed to the canWrite() and returned as false as it isn't within:

      [image/png, image/jpeg, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
      

      Which is returned by the ImageIO.getWriterMIMETypes().

      Now looking at the BufferedImageHttpMessageConverter there is the ability to specify the defaultContentType to use when no contentType is provided to the write() method. Unfortunately this method never gets called with the MediaType as null. To improve the BufferedImage converter it would make sense to have the isWriteable(MediaType) check for MediaType.ALL. Then within the write() method of the BufferedImageHttpMessageConverter it would use the defaultContentType when passed in a MediaType.ALL.

      BufferedImageHttpMessageConverter.isWriteable()
      	private boolean isWritable(MediaType mediaType) {
      		if (mediaType == null || MediaType.ALL.equals(mediaType)) {
      			return true;
      		}
      		Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(mediaType.toString());
      		return imageWriters.hasNext();
      	}
      

      This method looks very similar to the AbstractHttpMessageConverter.canWrite(MediaType) except that it compares the mediaType to the ImageIO available MIME types instead of the supportedMediaTypes.

      BufferedImageHttpMessageConverter.write(BufferedImage, MediaType, HttpOutputMessage)
      	public void write(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage)
      			throws IOException, HttpMessageNotWritableException {
      
      		if (contentType == null || MediaType.ALL.equals(contentType)) {
      			contentType = getDefaultContentType();
      		}
      		
      		...
      	}
      

      Sorry for not having the source code checked out to be able to provide a patch file.

        Attachments

          Activity

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              shawnclark Shawn Clark
              Last updater:
              Rossen Stoyanchev
            • Votes:
              3 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                6 years, 12 weeks, 3 days ago