Class CachingHttpServletRequest
java.lang.Object
jakarta.servlet.ServletRequestWrapper
jakarta.servlet.http.HttpServletRequestWrapper
org.apache.juneau.rest.server.util.CachingHttpServletRequest
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletRequest
Wraps an
HttpServletRequest and tees a bounded copy of the request body into memory for debug logging.
Unlike a fully-buffering wrapper, this captures at most a configured cap (default 8 KB) of the body while passing the full stream through to the handler untouched. The total number of bytes read is tracked so a truncation marker can be rendered. Memory stays bounded even for large uploads.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault body capture cap, in bytes (8 KB).Fields inherited from interface jakarta.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCachingHttpServletRequest(jakarta.servlet.http.HttpServletRequest req, int cap) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Returns the captured (possibly truncated) request body bytes.jakarta.servlet.ServletInputStreamReturns a character-stream tee over the same underlying byte stream captured bygetInputStream().longReturns the total number of body bytes read so far, including any bytes beyond the capture cap.static CachingHttpServletRequestwrap(jakarta.servlet.http.HttpServletRequest req) Wraps the specified request inside aCachingHttpServletRequestif it isn't already, using the default cap.static CachingHttpServletRequestwrap(jakarta.servlet.http.HttpServletRequest req, int cap) Wraps the specified request inside aCachingHttpServletRequestif it isn't already.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, upgradeMethods 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, startAsyncMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
-
Field Details
-
DEFAULT_CAP
Default body capture cap, in bytes (8 KB).- See Also:
-
-
Constructor Details
-
CachingHttpServletRequest
Constructor.- Parameters:
req- The request being wrapped. Must not benull .cap- The maximum number of body bytes to capture.
-
-
Method Details
-
wrap
Wraps the specified request inside aCachingHttpServletRequestif it isn't already, using the default cap.- Parameters:
req- The request to wrap. Must not benull .- Returns:
- The wrapped request.
-
wrap
Wraps the specified request inside aCachingHttpServletRequestif it isn't already.- Parameters:
req- The request to wrap. Must not benull .cap- The maximum number of body bytes to capture.- Returns:
- The wrapped request.
-
getContent
Returns the captured (possibly truncated) request body bytes.- Returns:
- The captured body bytes (at most the configured cap). Never
null ; empty if the body was not read.
-
getTotalLength
Returns the total number of body bytes read so far, including any bytes beyond the capture cap.- Returns:
- The total body length in bytes.
-
getInputStream
- Specified by:
getInputStreamin interfacejakarta.servlet.ServletRequest- Overrides:
getInputStreamin classjakarta.servlet.ServletRequestWrapper- Throws:
IOException
-
getReader
Returns a character-stream tee over the same underlying byte stream captured bygetInputStream().A handler that reads the request body through
getReader()instead ofgetInputStream()would otherwise bypass capture entirely — the defaultServletRequestWrapper.getReader()delegates straight to the wrapped request. This override routes the reader through the tee'd stream so both access styles are captured identically.- Specified by:
getReaderin interfacejakarta.servlet.ServletRequest- Overrides:
getReaderin classjakarta.servlet.ServletRequestWrapper- Throws:
IOException
-