New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve ContentCachingRequestWrapper [SPR-12815] #17412
Comments
cemo koc commented Perhaps a boolean property to indicate creation a new instance of InputStream with cached content can be a solution in a backward compatible manner. |
cemo koc commented Brian, |
Brian Clozel commented Hi cemo koc Yes, sorry - I forgot to add a comment here. Could you explain a bit more why this can be useful in an application (i.e. why reading the same InputStream several times)? |
cemo koc commented Hi Brian, Our API server is consuming JSON requests and in case an error I would like to log input stream to log as well. I am aware of consequences of this choice but duplicating request content entirely is lesser concern on my side. Having information regarding error with content is priceless for me. An option to provide necessary data will be enough on my side. |
Brian Clozel commented I think you can achieve something like this with Unless you'd like to do this within a Either way, doing this can lead to complex behavior: as long as the full request body has not been read in its entirety, getting a another InputStream would result in an incomplete copy of the request body. Note that Spring Boot is using a Trace filter copying+storing request information in a repository; but not the request body itself. |
Anna commented Looks like the problem is in the section below : every time Read is called, ch element pulls directly from input stream rather than the cache. private class ContentCachingInputStream extends ServletInputStream {
private final ServletInputStream is;
public ContentCachingInputStream(ServletInputStream is) {
this.is = is;
}
@Override
public int read() throws IOException {
int ch = this.is.read();
if (ch != -1) {
cachedContent.write(ch);
}
return ch;
}
} |
Brian Clozel commented Hello Anna, I don't understand your comment. Is this related to the current issue which is about reading multiple times the stream? If you think this is not the correct behavior, could you create a new issue explaining how it should behave and why? |
cemo koc opened SPR-12815 and commented
I have an issue with reading inputstream from request multiple times. Before trying an implementation, I have searched in Spring and came across ContentCachingRequestWrapper. This is a nice utility but requires you to have an instance of ContentCachingRequestWrapper to use it as this:
This is pretty much limiting because servlet filters are usually wrapping requests into another request instance to provide additional capabilities. I can access this content if only I have an instance of this filter.
It would be great If this behaviour can be changed to provide a new inputstream with cached data.
I have questioned myself why It was not implemented like this in the first place but really could not find a reason.
Shortly, please improve getInputStream and getReader methods to provide cached content in case a consumed stream. Thus we do not need to consider whether request is an instance of ContentCachingRequestWrapper or not.
Affects: 4.1.5
Issue Links:
1 votes, 5 watchers
The text was updated successfully, but these errors were encountered: