Ensure all converters don't close InputStream

Closes gh-27969
This commit is contained in:
rstoyanchev
2022-02-02 15:32:21 +00:00
parent 4effca35b5
commit 8d5a6520ce
19 changed files with 151 additions and 39 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
/**
@@ -207,6 +208,7 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
}
private ImageInputStream createImageInputStream(InputStream is) throws IOException {
is = StreamUtils.nonClosing(is);
if (this.cacheDir != null) {
return new FileCacheImageInputStream(is, this.cacheDir);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.http.converter.feed;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
@@ -35,6 +36,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
/**
@@ -74,7 +76,8 @@ public abstract class AbstractWireFeedHttpMessageConverter<T extends WireFeed>
Charset charset = (contentType != null && contentType.getCharset() != null ?
contentType.getCharset() : DEFAULT_CHARSET);
try {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody());
Reader reader = new InputStreamReader(inputStream, charset);
return (T) feedInput.build(reader);
}
catch (FeedException ex) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.http.converter.json;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
@@ -361,24 +362,25 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
"UTF-16".equals(charset.name()) ||
"UTF-32".equals(charset.name());
try {
InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody());
if (inputMessage instanceof MappingJacksonInputMessage) {
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
if (deserializationView != null) {
ObjectReader objectReader = objectMapper.readerWithView(deserializationView).forType(javaType);
if (isUnicode) {
return objectReader.readValue(inputMessage.getBody());
return objectReader.readValue(inputStream);
}
else {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
Reader reader = new InputStreamReader(inputStream, charset);
return objectReader.readValue(reader);
}
}
}
if (isUnicode) {
return objectMapper.readValue(inputMessage.getBody(), javaType);
return objectMapper.readValue(inputStream, javaType);
}
else {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
Reader reader = new InputStreamReader(inputStream, charset);
return objectMapper.readValue(reader, javaType);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.http.converter.xml;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
@@ -33,6 +34,7 @@ import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;
/**
* Abstract base class for {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverters}
@@ -66,7 +68,8 @@ public abstract class AbstractXmlHttpMessageConverter<T> extends AbstractHttpMes
throws IOException, HttpMessageNotReadableException {
try {
return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody()));
InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody());
return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputStream));
}
catch (IOException | HttpMessageConversionException ex) {
throw ex;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -146,7 +146,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
InputStream body = StreamUtils.nonClosing(inputMessage.getBody());
if (DOMSource.class == clazz) {
return (T) readDOMSource(body, inputMessage);
}