Package org.springframework.web.util
Class ContentCachingRequestWrapper
- java.lang.Object
- javax.servlet.ServletRequestWrapper
- javax.servlet.http.HttpServletRequestWrapper
- org.springframework.web.util.ContentCachingRequestWrapper
- All Implemented Interfaces:
HttpServletRequest,ServletRequest
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper
HttpServletRequestwrapper that caches all content read from the input stream and reader, and allows this content to be retrieved via abyte array.Used e.g. by
AbstractRequestLoggingFilter. Note: As of Spring Framework 5.0, this wrapper is built on the Servlet 3.1 API.- Since:
- 4.1.3
- Author:
- Juergen Hoeller, Brian Clozel
- See Also:
ContentCachingResponseWrapper
Field Summary
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
Constructor Summary
Constructors Constructor Description ContentCachingRequestWrapper(HttpServletRequest request)Create a new ContentCachingRequestWrapper for the given servlet request.ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit)Create a new ContentCachingRequestWrapper for the given servlet request.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetCharacterEncoding()The default behavior of this method is to return getCharacterEncoding() on the wrapped request object.byte[]getContentAsByteArray()Return the cached request content as a byte array.ServletInputStreamgetInputStream()The default behavior of this method is to return getInputStream() on the wrapped request object.StringgetParameter(String name)The default behavior of this method is to return getParameter(String name) on the wrapped request object.Map<String,String[]>getParameterMap()The default behavior of this method is to return getParameterMap() on the wrapped request object.Enumeration<String>getParameterNames()The default behavior of this method is to return getParameterNames() on the wrapped request object.String[]getParameterValues(String name)The default behavior of this method is to return getParameterValues(String name) on the wrapped request object.BufferedReadergetReader()The default behavior of this method is to return getReader() on the wrapped request object.protected voidhandleContentOverflow(int contentCacheLimit)Template method for handling a content overflow: specifically, a request body being read that exceeds the specified content cache limit.Methods inherited from class javax.servlet.http.HttpServletRequestWrapper
authenticate, changeSessionId, getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getHttpServletMapping, getIntHeader, getMethod, getPart, getParts, getPathInfo, getPathTranslated, getQueryString, getRemoteUser, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getTrailerFields, getUserPrincipal, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isTrailerFieldsReady, isUserInRole, login, logout, newPushBuilder, upgrade
Methods inherited from class javax.servlet.ServletRequestWrapper
getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, setRequest, startAsync, startAsync
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
Constructor Detail
ContentCachingRequestWrapper
public ContentCachingRequestWrapper(HttpServletRequest request)
Create a new ContentCachingRequestWrapper for the given servlet request.- Parameters:
request- the original servlet request
ContentCachingRequestWrapper
public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit)
Create a new ContentCachingRequestWrapper for the given servlet request.- Parameters:
request- the original servlet requestcontentCacheLimit- the maximum number of bytes to cache per request- Since:
- 4.3.6
- See Also:
handleContentOverflow(int)
Method Detail
getInputStream
public ServletInputStream getInputStream() throws IOException
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getInputStream() on the wrapped request object.- Specified by:
getInputStreamin interfaceServletRequest- Overrides:
getInputStreamin classServletRequestWrapper- Returns:
- a
ServletInputStreamobject containing the body of the request - Throws:
IOException- if an input or output exception occurred
getCharacterEncoding
public String getCharacterEncoding()
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getCharacterEncoding() on the wrapped request object.- Specified by:
getCharacterEncodingin interfaceServletRequest- Overrides:
getCharacterEncodingin classServletRequestWrapper- Returns:
- a
Stringcontaining the name of the character encoding, ornullif the request does not specify a character encoding
getReader
public BufferedReader getReader() throws IOException
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getReader() on the wrapped request object.- Specified by:
getReaderin interfaceServletRequest- Overrides:
getReaderin classServletRequestWrapper- Returns:
- a
BufferedReadercontaining the body of the request - Throws:
UnsupportedEncodingException- if the character set encoding used is not supported and the text cannot be decodedIOException- if an input or output exception occurred- See Also:
ServletRequest.getInputStream()
getParameter
public String getParameter(String name)
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getParameter(String name) on the wrapped request object.- Specified by:
getParameterin interfaceServletRequest- Overrides:
getParameterin classServletRequestWrapper- Parameters:
name- aStringspecifying the name of the parameter- Returns:
- a
Stringrepresenting the single value of the parameter - See Also:
ServletRequest.getParameterValues(java.lang.String)
getParameterMap
public Map<String,String[]> getParameterMap()
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getParameterMap() on the wrapped request object.- Specified by:
getParameterMapin interfaceServletRequest- Overrides:
getParameterMapin classServletRequestWrapper- Returns:
- 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.
getParameterNames
public Enumeration<String> getParameterNames()
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getParameterNames() on the wrapped request object.- Specified by:
getParameterNamesin interfaceServletRequest- Overrides:
getParameterNamesin classServletRequestWrapper- Returns:
- an
EnumerationofStringobjects, eachStringcontaining the name of a request parameter; or an emptyEnumerationif the request has no parameters
getParameterValues
public String[] getParameterValues(String name)
Description copied from class:javax.servlet.ServletRequestWrapperThe default behavior of this method is to return getParameterValues(String name) on the wrapped request object.- Specified by:
getParameterValuesin interfaceServletRequest- Overrides:
getParameterValuesin classServletRequestWrapper- Parameters:
name- aStringcontaining the name of the parameter whose value is requested- Returns:
- an array of
Stringobjects containing the parameter's values - See Also:
ServletRequest.getParameter(java.lang.String)
getContentAsByteArray
public byte[] getContentAsByteArray()
Return the cached request content as a byte array.The returned array will never be larger than the content cache limit.
handleContentOverflow
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- the maximum number of bytes to cache per request which has just been exceeded- Since:
- 4.3.6
- See Also:
ContentCachingRequestWrapper(HttpServletRequest, int)