vendredi 27 mars 2015

Consistent URI/body decoding in Spring Boot 1.2 Upgrade breaks our returned images

I updated one of our web services from 1.9 to 1.2.1 and we had an issue where images were no longer being returned as expected.


They would have a mime type of text/html and the response would be tiny, near ~150 bytes. The browser would of course complain about trying to interpret text as an image and the image would not display.


The strange thing was that the HttpServletResponse would clearly have the real data in the response at the end of the controller function.


Here is the controller and the function that adds the image to the response with some changes to hide company identity:



@RequestMapping(value = "/lot/{someId}/{anotherId}/{width}", method = RequestMethod.GET)
public void getImage(@PathVariable Long someId, @PathVariable Integer anotherId, @PathVariable Integer width, HttpServletResponse response) throws IOException {
if(!validDimension(width)) { return; }
String key = someId+ "-" + anotherId+ "-" + width;
MyMap image = (MyMap) getFromCache(key);
if(image == null) {
image = someGetMapService.getMyMap(openHouseId, lotId);
MyMap processedImage = scaleMapImage(image, width);
if(processedImage != null) {
saveToCache(key, processedImage);
image = processedImage;//processedImage is valid so use that
}
}
processResponse(image.getMimeType(), image.getFile(), response);
}


processResponse Code:



private void processResponse(String mimeType, byte[] image, HttpServletResponse response) throws IOException {
ContentType contentType = ContentType.valueOfMimeType(mimeType, ContentType.findContentTypesByCategory(ContentTypeCategory.IMAGE));
//check for valid content type, and set it before streaming it out to avoid XSS vulnerabilities
//with things like svg - if not valid, don't stream out the image
if (contentType == null) {
LOG.log(Level.WARNING, MessageFormat.format("Unable to find matching content type for: {0}", mimeType));
response.flushBuffer();
return;
}
response.setContentType(contentType.getMimeType());
if (image != null) {
response.setContentLength(image.length);
response.getOutputStream().write(image);
}
response.flushBuffer();
}


All the problems were fixed when I set: spring.http.encoding.enabled = false


This is stated in the 1.2 release notes:



Consistent HTTP URI/body decoding


A CharacterEncodingFilter is now registered automatically for consistent URI/body decoding. You can use the spring.http.encoding.charset property if you need something other than UTF-8 or set spring.http.encoding.enabled to false if you don’t want the CharacterEncodingFilter registered at all.



Link: Spring-Boot-1.2-Release-Notes


This is my question! WHY would that filter be blocking my images?


Aucun commentaire:

Enregistrer un commentaire