Class ContentCachingRequestWrapper

java.lang.Object
jakarta.servlet.ServletRequestWrapper
jakarta.servlet.http.HttpServletRequestWrapper
org.springframework.web.util.ContentCachingRequestWrapper
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest

public class ContentCachingRequestWrapper extends jakarta.servlet.http.HttpServletRequestWrapper
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, for example, by AbstractRequestLoggingFilter.

Since:
4.1.3
Author:
Juergen Hoeller, Brian Clozel
See Also:
  • Field Summary

    Fields inherited from interface jakarta.servlet.http.HttpServletRequest

    BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
  • Constructor Summary

    Constructors
    Constructor
    Description
    ContentCachingRequestWrapper(jakarta.servlet.http.HttpServletRequest request, int cacheLimit)
    Create a new ContentCachingRequestWrapper for the given servlet request.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    byte[]
    Return the cached request content as a byte array.
    Return the cached request content as a String, using the configured Charset.
    jakarta.servlet.ServletInputStream
     
     
     
     
     
     
    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.

    Methods inherited from class jakarta.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, isRequestedSessionIdValid, isTrailerFieldsReady, isUserInRole, login, logout, newPushBuilder, upgrade

    Methods inherited from class jakarta.servlet.ServletRequestWrapper

    getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getProtocolRequestId, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getRequestId, getScheme, getServerName, getServerPort, getServletConnection, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, setCharacterEncoding, setRequest, startAsync, startAsync

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface jakarta.servlet.ServletRequest

    getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getProtocolRequestId, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getRequestId, getScheme, getServerName, getServerPort, getServletConnection, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, setCharacterEncoding, startAsync, startAsync
  • Constructor Details

    • ContentCachingRequestWrapper

      public ContentCachingRequestWrapper(jakarta.servlet.http.HttpServletRequest request, int cacheLimit)
      Create a new ContentCachingRequestWrapper for the given servlet request.
      Parameters:
      request - the original servlet request
      cacheLimit - the maximum number of bytes to cache per request; no limit is set if the value is 0 or less. It is recommended to set a concrete limit in order to avoid using too much memory.
      Since:
      4.3.6
      See Also:
  • Method Details

    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream() throws IOException
      Specified by:
      getInputStream in interface jakarta.servlet.ServletRequest
      Overrides:
      getInputStream in class jakarta.servlet.ServletRequestWrapper
      Throws:
      IOException
    • getCharacterEncoding

      public String getCharacterEncoding()
      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletRequest
      Overrides:
      getCharacterEncoding in class jakarta.servlet.ServletRequestWrapper
    • getReader

      public BufferedReader getReader() throws IOException
      Specified by:
      getReader in interface jakarta.servlet.ServletRequest
      Overrides:
      getReader in class jakarta.servlet.ServletRequestWrapper
      Throws:
      IOException
    • getParameter

      public String getParameter(String name)
      Specified by:
      getParameter in interface jakarta.servlet.ServletRequest
      Overrides:
      getParameter in class jakarta.servlet.ServletRequestWrapper
    • getParameterMap

      public Map<String,String[]> getParameterMap()
      Specified by:
      getParameterMap in interface jakarta.servlet.ServletRequest
      Overrides:
      getParameterMap in class jakarta.servlet.ServletRequestWrapper
    • getParameterNames

      public Enumeration<String> getParameterNames()
      Specified by:
      getParameterNames in interface jakarta.servlet.ServletRequest
      Overrides:
      getParameterNames in class jakarta.servlet.ServletRequestWrapper
    • getParameterValues

      public String[] getParameterValues(String name)
      Specified by:
      getParameterValues in interface jakarta.servlet.ServletRequest
      Overrides:
      getParameterValues in class jakarta.servlet.ServletRequestWrapper
    • 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.

      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:
    • getContentAsString

      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:
    • 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: