Top Description Inners Fields Constructors Methods
org.springframework.web.util

public Class ContentCachingRequestWrapper

extends HttpServletRequestWrapper
Class Inheritance
Imports
java.io.BufferedReader, .IOException, .InputStreamReader, java.net.URLEncoder, java.nio.charset.Charset, java.util.Arrays, .Enumeration, .Iterator, .List, .Map, jakarta.servlet.ReadListener, .ServletInputStream, jakarta.servlet.http.HttpServletRequest, .HttpServletRequestWrapper, org.springframework.http.HttpMethod, .MediaType, org.springframework.lang.Nullable, org.springframework.util.FastByteArrayOutputStream

jakarta.servlet.http.HttpServletRequest wrapper that caches all content read from the input stream and reader, and allows this content to be retrieved via a byte array.

This class acts as an interceptor that only caches content as it is being read but otherwise does not cause content to be read. That means if the request content is not consumed, then the content is not cached, and cannot be retrieved via getContentAsByteArray().

Used e.g. by org.springframework.web.filter.AbstractRequestLoggingFilter.

Authors
Juergen Hoeller, Brian Clozel
Since
4.1.3
See Also
ContentCachingResponseWrapper

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final FastByteArrayOutputStream
private final Integer
private ServletInputStream
private BufferedReader

Constructor Summary

AccessConstructor and Description
public
ContentCachingRequestWrapper(HttpServletRequest
the original servlet request
request
)

Create a new ContentCachingRequestWrapper for the given servlet request.

public
ContentCachingRequestWrapper(HttpServletRequest
the original servlet request
request
,
int
the maximum number of bytes to cache per request
contentCacheLimit
)

Create a new ContentCachingRequestWrapper for the given servlet request.

Method Summary

Modifier and TypeMethod and Description
public String
getCharacterEncoding()

Overrides jakarta.servlet.ServletRequestWrapper.getCharacterEncoding.

Implements jakarta.servlet.ServletRequest.getCharacterEncoding.

Returns the name of the character encoding used in the body of this request.
public byte[]
getContentAsByteArray()

Return the cached request content as a byte array.

public String
getContentAsString()

Return the cached request content as a String, using the configured Charset.

public ServletInputStream
getInputStream()

Overrides jakarta.servlet.ServletRequestWrapper.getInputStream.

Implements jakarta.servlet.ServletRequest.getInputStream.

Retrieves the body of the request as binary data using a ServletInputStream.
public String
getParameter(String
a String specifying the name of the parameter
name
)

Overrides jakarta.servlet.ServletRequestWrapper.getParameter.

Implements jakarta.servlet.ServletRequest.getParameter.

Returns the value of a request parameter as a String, or null if the parameter does not exist.
public Map<String, String[]>
getParameterMap()

Overrides jakarta.servlet.ServletRequestWrapper.getParameterMap.

Implements jakarta.servlet.ServletRequest.getParameterMap.

Returns a java.util.Map of the parameters of this request.
public Enumeration<String>
getParameterNames()

Overrides jakarta.servlet.ServletRequestWrapper.getParameterNames.

Implements jakarta.servlet.ServletRequest.getParameterNames.

Returns an Enumeration of String objects containing the names of the parameters contained in this request.
public String[]
getParameterValues(String
a String containing the name of the parameter whose value is requested
name
)

Overrides jakarta.servlet.ServletRequestWrapper.getParameterValues.

Implements jakarta.servlet.ServletRequest.getParameterValues.

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
public BufferedReader
getReader()

Overrides jakarta.servlet.ServletRequestWrapper.getReader.

Implements jakarta.servlet.ServletRequest.getReader.

Retrieves the body of the request as character data using a BufferedReader.
protected void
handleContentOverflow(int
the maximum number of bytes to cache per request which has just been exceeded
contentCacheLimit
)

Template method for handling a content overflow: specifically, a request body being read that exceeds the specified content cache limit.

private boolean
private void
Inherited from jakarta.servlet.http.HttpServletRequestWrapper:
authenticatechangeSessionIdgetAuthTypegetContextPathgetCookiesgetDateHeadergetHeadergetHeaderNamesgetHeadersgetHttpServletMappinggetIntHeadergetMethodgetPartgetPartsgetPathInfogetPathTranslatedgetQueryStringgetRemoteUsergetRequestedSessionIdgetRequestURIgetRequestURLgetServletPathgetSessiongetSessiongetTrailerFieldsgetUserPrincipalisRequestedSessionIdFromCookieisRequestedSessionIdFromURLisRequestedSessionIdValidisTrailerFieldsReadyisUserInRoleloginlogoutnewPushBuilderupgrade

Field Detail

cachedContentback to summary
private final FastByteArrayOutputStream cachedContent
contentCacheLimitback to summary
private final Integer contentCacheLimit
Annotations
@Nullable
inputStreamback to summary
private ServletInputStream inputStream
Annotations
@Nullable
readerback to summary
private BufferedReader reader
Annotations
@Nullable

Constructor Detail

ContentCachingRequestWrapperback to summary
public ContentCachingRequestWrapper(HttpServletRequest request)

Create a new ContentCachingRequestWrapper for the given servlet request.

Parameters
request:HttpServletRequest

the original servlet request

ContentCachingRequestWrapperback to summary
public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit)

Create a new ContentCachingRequestWrapper for the given servlet request.

Parameters
request:HttpServletRequest

the original servlet request

contentCacheLimit:int

the maximum number of bytes to cache per request

Since
4.3.6
See Also
handleContentOverflow(int)

Method Detail

getCharacterEncodingback to summary
public String getCharacterEncoding()

Overrides jakarta.servlet.ServletRequestWrapper.getCharacterEncoding.

Implements jakarta.servlet.ServletRequest.getCharacterEncoding.

Doc from jakarta.servlet.ServletRequest.getCharacterEncoding.

Returns the name of the character encoding used in the body of this request. This method returns null if the no character encoding has been specified. The following priority order is used to determine the specified encoding:

  1. per request
  2. web application default via the deployment descriptor or ServletContext#setRequestCharacterEncoding(String)
  3. container default via container specific configuration
Returns:String

a String containing the name of the character encoding, or null if the request does not specify a character encoding

Annotations
@Override
getContentAsByteArrayback to summary
public byte[] getContentAsByteArray()

Return the cached request content as a byte array.

The returned array will never be larger than the content cache limit.

Note

The byte array returned from this method reflects the amount of content that has been read at the time when it is called. If the application does not read the content, this method returns an empty array.

See Also
ContentCachingRequestWrapper(HttpServletRequest, int)
getContentAsStringback to summary
public String getContentAsString()

Return the cached request content as a String, using the configured Charset.

Note

The String returned from this method reflects the amount of content that has been read at the time when it is called. If the application does not read the content, this method returns an empty String.

Since
6.1
See Also
getContentAsByteArray()
getInputStreamback to summary
public ServletInputStream getInputStream() throws IOException

Overrides jakarta.servlet.ServletRequestWrapper.getInputStream.

Implements jakarta.servlet.ServletRequest.getInputStream.

Doc from jakarta.servlet.ServletRequest.getInputStream.

Retrieves the body of the request as binary data using a ServletInputStream. Either this method or getReader may be called to read the body, not both.

Returns:ServletInputStream

a ServletInputStream object containing the body of the request

Annotations
@Override
Exceptions
IOException:
if an input or output exception occurred
getParameterback to summary
public String getParameter(String name)

Overrides jakarta.servlet.ServletRequestWrapper.getParameter.

Implements jakarta.servlet.ServletRequest.getParameter.

Doc from jakarta.servlet.ServletRequest.getParameter.

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues.

If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.

If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream or getReader can interfere with the execution of this method.

Parameters
name:String

a String specifying the name of the parameter

Returns:String

a String representing the single value of the parameter

Annotations
@Override
getParameterMapback to summary
public Map<String, String[]> getParameterMap()

Overrides jakarta.servlet.ServletRequestWrapper.getParameterMap.

Implements jakarta.servlet.ServletRequest.getParameterMap.

Doc from jakarta.servlet.ServletRequest.getParameterMap.

Returns a java.util.Map of the parameters of this request. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

Returns:Map<String, String[]>

an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.

Annotations
@Override
getParameterNamesback to summary
public Enumeration<String> getParameterNames()

Overrides jakarta.servlet.ServletRequestWrapper.getParameterNames.

Implements jakarta.servlet.ServletRequest.getParameterNames.

Doc from jakarta.servlet.ServletRequest.getParameterNames.

Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.

Returns:Enumeration<String>

an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters

Annotations
@Override
getParameterValuesback to summary
public String[] getParameterValues(String name)

Overrides jakarta.servlet.ServletRequestWrapper.getParameterValues.

Implements jakarta.servlet.ServletRequest.getParameterValues.

Doc from jakarta.servlet.ServletRequest.getParameterValues.

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

If the parameter has a single value, the array has a length of 1.

Parameters
name:String

a String containing the name of the parameter whose value is requested

Returns:String[]

an array of String objects containing the parameter's values

Annotations
@Override
getReaderback to summary
public BufferedReader getReader() throws IOException

Overrides jakarta.servlet.ServletRequestWrapper.getReader.

Implements jakarta.servlet.ServletRequest.getReader.

Doc from jakarta.servlet.ServletRequest.getReader.

Retrieves the body of the request as character data using a BufferedReader. The reader translates the character data according to the character encoding used on the body. Either this method or getInputStream may be called to read the body, not both.

Returns:BufferedReader

a BufferedReader containing the body of the request

Annotations
@Override
Exceptions
IOException:
if an input or output exception occurred
handleContentOverflowback to summary
protected void handleContentOverflow(int contentCacheLimit)

Template method for handling a content overflow: specifically, a request body being read that exceeds the specified content cache limit.

The default implementation is empty. Subclasses may override this to throw a payload-too-large exception or the like.

Parameters
contentCacheLimit:int

the maximum number of bytes to cache per request which has just been exceeded

Since
4.3.6
See Also
ContentCachingRequestWrapper(HttpServletRequest, int)
isFormPostback to summary
private boolean isFormPost()
writeRequestParametersToCachedContentback to summary
private void writeRequestParametersToCachedContent()
org.springframework.web.util back to summary

private Class ContentCachingRequestWrapper.ContentCachingInputStream

extends ServletInputStream
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final ServletInputStream
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
isFinished()

Implements abstract jakarta.servlet.ServletInputStream.isFinished.

Has the end of this InputStream been reached?
public boolean
isReady()

Implements abstract jakarta.servlet.ServletInputStream.isReady.

Can data be read from this InputStream without blocking?
public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.
public int
read(byte[]
the buffer into which the data is read.
b
)

Overrides java.io.InputStream.read.

Reads some number of bytes from the input stream and stores them into the buffer array b.
public int
read(final byte[]
the buffer into which the data is read.
b
,
final int
the start offset in array b at which the data is written.
off
,
final int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.
public int
readLine(final byte[]
an array of bytes into which data is read
b
,
final int
an integer specifying the character at which this method begins reading
off
,
final int
an integer specifying the maximum number of bytes to read
len
)

Overrides jakarta.servlet.ServletInputStream.readLine.

Reads the input stream, one line at a time.
public void
setReadListener(ReadListener
The non-blocking IO read listener
readListener
)

Implements abstract jakarta.servlet.ServletInputStream.setReadListener.

Sets the ReadListener for this ServletInputStream and thereby switches to non-blocking IO.
private void
writeToCache(final byte[] b, final int off, int count)

Field Detail

isback to summary
private final ServletInputStream is
overflowback to summary
private boolean overflow

Constructor Detail

ContentCachingInputStreamback to summary
public ContentCachingInputStream(ServletInputStream is)

Method Detail

isFinishedback to summary
public boolean isFinished()

Implements abstract jakarta.servlet.ServletInputStream.isFinished.

Doc from jakarta.servlet.ServletInputStream.isFinished.

Has the end of this InputStream been reached?

Returns:boolean

true if all the data has been read from the stream, else false

Annotations
@Override
isReadyback to summary
public boolean isReady()

Implements abstract jakarta.servlet.ServletInputStream.isReady.

Doc from jakarta.servlet.ServletInputStream.isReady.

Can data be read from this InputStream without blocking? Returns If this method is called and returns false, the container will invoke ReadListener#onDataAvailable() when data is available.

Returns:boolean

true if data can be read without blocking, else false

Annotations
@Override
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
readLineback to summary
public int readLine(final byte[] b, final int off, final int len) throws IOException

Overrides jakarta.servlet.ServletInputStream.readLine.

Doc from jakarta.servlet.ServletInputStream.readLine.

Reads the input stream, one line at a time. Starting at an offset, reads bytes into an array, until it reads a certain number of bytes or reaches a newline character, which it reads into the array as well.

This method returns -1 if it reaches the end of the input stream before reading the maximum number of bytes.

Parameters
b:byte[]

an array of bytes into which data is read

off:int

an integer specifying the character at which this method begins reading

len:int

an integer specifying the maximum number of bytes to read

Returns:int

an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached

Annotations
@Override
Exceptions
IOException:
if an input or output exception has occurred
setReadListenerback to summary
public void setReadListener(ReadListener readListener)

Implements abstract jakarta.servlet.ServletInputStream.setReadListener.

Doc from jakarta.servlet.ServletInputStream.setReadListener.

Sets the ReadListener for this ServletInputStream and thereby switches to non-blocking IO. It is only valid to switch to non-blocking IO within async processing or HTTP upgrade processing.

Parameters
readListener:ReadListener

The non-blocking IO read listener

Annotations
@Override
writeToCacheback to summary
private void writeToCache(final byte[] b, final int off, int count) throws IOException