Class CachedBodyHttpServletRequest

java.lang.Object
jakarta.servlet.ServletRequestWrapper
jakarta.servlet.http.HttpServletRequestWrapper
io.github.ravocode.avoonce.spring.CachedBodyHttpServletRequest
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest

public final class CachedBodyHttpServletRequest extends jakarta.servlet.http.HttpServletRequestWrapper
An HttpServletRequestWrapper that caches the HTTP request body in memory, allowing it to be read multiple times (e.g. for SHA-256 hashing and downstream controller processing).
  • Field Summary

    Fields inherited from interface jakarta.servlet.http.HttpServletRequest

    BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
  • Constructor Summary

    Constructors
    Constructor
    Description
    CachedBodyHttpServletRequest(jakarta.servlet.http.HttpServletRequest request)
    Constructs a CachedBodyHttpServletRequest by reading and caching the input stream of the given request.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the cached byte array of the request body.
    jakarta.servlet.ServletInputStream
    Returns a servlet input stream backed by the cached request body.
    Returns a buffered reader over the cached request body.

    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, getCharacterEncoding, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, 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, getCharacterEncoding, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getProtocolRequestId, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getRequestId, getScheme, getServerName, getServerPort, getServletConnection, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, setCharacterEncoding, startAsync, startAsync
  • Constructor Details

    • CachedBodyHttpServletRequest

      public CachedBodyHttpServletRequest(jakarta.servlet.http.HttpServletRequest request) throws IOException
      Constructs a CachedBodyHttpServletRequest by reading and caching the input stream of the given request.
      Parameters:
      request - the original HTTP servlet request.
      Throws:
      IOException - if an I/O error occurs while reading the request body.
  • Method Details

    • getCachedBody

      public byte[] getCachedBody()
      Returns the cached byte array of the request body.
      Returns:
      the raw request body bytes.
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream()
      Returns a servlet input stream backed by the cached request body.
      Specified by:
      getInputStream in interface jakarta.servlet.ServletRequest
      Overrides:
      getInputStream in class jakarta.servlet.ServletRequestWrapper
      Returns:
      a reusable input stream over the cached bytes
    • getReader

      public BufferedReader getReader()
      Returns a buffered reader over the cached request body.
      Specified by:
      getReader in interface jakarta.servlet.ServletRequest
      Overrides:
      getReader in class jakarta.servlet.ServletRequestWrapper
      Returns:
      a reader over the cached bytes