Package jakarta.servlet
Class ServletRequestWrapper
- java.lang.Object
-
- jakarta.servlet.ServletRequestWrapper
-
- All Implemented Interfaces:
ServletRequest
- Direct Known Subclasses:
HttpServletRequestWrapper
public class ServletRequestWrapper extends Object implements ServletRequest
Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.- Since:
- Servlet 2.3
- See Also:
ServletRequest
-
-
Constructor Summary
Constructors Constructor Description ServletRequestWrapper(ServletRequest request)Creates a ServletRequest adaptor wrapping the given request object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AsyncContextgetAsyncContext()Gets the AsyncContext that was created or reinitialized by the most recent invocation ofstartAsync()orstartAsync(ServletRequest,ServletResponse)on the wrapped request.ObjectgetAttribute(String name)The default behavior of this method is to call getAttribute(String name) on the wrapped request object.Enumeration<String>getAttributeNames()The default behavior of this method is to return getAttributeNames() on the wrapped request object.StringgetCharacterEncoding()The default behavior of this method is to return getCharacterEncoding() on the wrapped request object.intgetContentLength()The default behavior of this method is to return getContentLength() on the wrapped request object.longgetContentLengthLong()The default behavior of this method is to return getContentLengthLong() on the wrapped request object.StringgetContentType()The default behavior of this method is to return getContentType() on the wrapped request object.DispatcherTypegetDispatcherType()Gets the dispatcher type of the wrapped request.ServletInputStreamgetInputStream()The default behavior of this method is to return getInputStream() on the wrapped request object.StringgetLocalAddr()The default behavior of this method is to return getLocalAddr() on the wrapped request object.LocalegetLocale()The default behavior of this method is to return getLocale() on the wrapped request object.Enumeration<Locale>getLocales()The default behavior of this method is to return getLocales() on the wrapped request object.StringgetLocalName()The default behavior of this method is to return getLocalName() on the wrapped request object.intgetLocalPort()The default behavior of this method is to return getLocalPort() 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.StringgetProtocol()The default behavior of this method is to return getProtocol() on the wrapped request object.StringgetProtocolRequestId()Gets the protocol defined request ID, if any, for the wrapped request.BufferedReadergetReader()The default behavior of this method is to return getReader() on the wrapped request object.StringgetRemoteAddr()The default behavior of this method is to return getRemoteAddr() on the wrapped request object.StringgetRemoteHost()The default behavior of this method is to return getRemoteHost() on the wrapped request object.intgetRemotePort()The default behavior of this method is to return getRemotePort() on the wrapped request object.ServletRequestgetRequest()Return the wrapped request object.RequestDispatchergetRequestDispatcher(String path)The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.StringgetRequestId()Gets the request ID for the wrapped request.StringgetScheme()The default behavior of this method is to return getScheme() on the wrapped request object.StringgetServerName()The default behavior of this method is to return getServerName() on the wrapped request object.intgetServerPort()The default behavior of this method is to return getServerPort() on the wrapped request object.ServletConnectiongetServletConnection()Gets the connection information for the wrapped request.ServletContextgetServletContext()Gets the servlet context to which the wrapped servlet request was last dispatched.booleanisAsyncStarted()Checks if the wrapped request has been put into asynchronous mode.booleanisAsyncSupported()Checks if the wrapped request supports asynchronous operation.booleanisSecure()The default behavior of this method is to return isSecure() on the wrapped request object.booleanisWrapperFor(ServletRequest wrapped)Checks (recursively) if this ServletRequestWrapper wraps the givenServletRequestinstance.booleanisWrapperFor(Class<?> wrappedType)Checks (recursively) if this ServletRequestWrapper wraps aServletRequestof the given class type.voidremoveAttribute(String name)The default behavior of this method is to call removeAttribute(String name) on the wrapped request object.voidsetAttribute(String name, Object o)The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.voidsetCharacterEncoding(String enc)The default behavior of this method is to set the character encoding on the wrapped request object.voidsetRequest(ServletRequest request)Sets the request object being wrapped.AsyncContextstartAsync()The default behavior of this method is to invokeServletRequest.startAsync()on the wrapped request object.AsyncContextstartAsync(ServletRequest servletRequest, ServletResponse servletResponse)The default behavior of this method is to invokeServletRequest.startAsync(ServletRequest, ServletResponse)on the wrapped request object.
-
-
-
Constructor Detail
-
ServletRequestWrapper
public ServletRequestWrapper(ServletRequest request)
Creates a ServletRequest adaptor wrapping the given request object.- Parameters:
request- theServletRequestto be wrapped- Throws:
IllegalArgumentException- if the request is null
-
-
Method Detail
-
getRequest
public ServletRequest getRequest()
Return the wrapped request object.- Returns:
- the wrapped
ServletRequest
-
setRequest
public void setRequest(ServletRequest request)
Sets the request object being wrapped.- Parameters:
request- theServletRequestto be installed- Throws:
IllegalArgumentException- if the request is null.
-
getAttribute
public Object getAttribute(String name)
The default behavior of this method is to call getAttribute(String name) on the wrapped request object.- Specified by:
getAttributein interfaceServletRequest- Parameters:
name- aStringspecifying the name of the attribute- Returns:
- an
Objectcontaining the value of the attribute, ornullif the attribute does not exist
-
getAttributeNames
public Enumeration<String> getAttributeNames()
The default behavior of this method is to return getAttributeNames() on the wrapped request object.- Specified by:
getAttributeNamesin interfaceServletRequest- Returns:
- an
Enumerationof strings containing the names of the request's attributes
-
getCharacterEncoding
public String getCharacterEncoding()
The default behavior of this method is to return getCharacterEncoding() on the wrapped request object.- Specified by:
getCharacterEncodingin interfaceServletRequest- Returns:
- a
Stringcontaining the name of the character encoding, ornullif the request does not specify a character encoding
-
setCharacterEncoding
public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
The default behavior of this method is to set the character encoding on the wrapped request object.- Specified by:
setCharacterEncodingin interfaceServletRequest- Parameters:
enc-Stringcontaining the name of the character encoding.- Throws:
UnsupportedEncodingException- if this ServletRequest is still in a state where a character encoding may be set, but the specified encoding is invalid
-
getContentLength
public int getContentLength()
The default behavior of this method is to return getContentLength() on the wrapped request object.- Specified by:
getContentLengthin interfaceServletRequest- Returns:
- an integer containing the length of the request body or -1 if the length is not known or is greater than Integer.MAX_VALUE.
-
getContentLengthLong
public long getContentLengthLong()
The default behavior of this method is to return getContentLengthLong() on the wrapped request object.- Specified by:
getContentLengthLongin interfaceServletRequest- Returns:
- a long containing the length of the request body or -1L if the length is not known
- Since:
- Servlet 3.1
-
getContentType
public String getContentType()
The default behavior of this method is to return getContentType() on the wrapped request object.- Specified by:
getContentTypein interfaceServletRequest- Returns:
- a
Stringcontaining the name of the MIME type of the request, or null if the type is not known
-
getInputStream
public ServletInputStream getInputStream() throws IOException
The default behavior of this method is to return getInputStream() on the wrapped request object.- Specified by:
getInputStreamin interfaceServletRequest- Returns:
- a
ServletInputStreamobject containing the body of the request - Throws:
IOException- if an input or output exception occurred
-
getParameter
public String getParameter(String name)
The default behavior of this method is to return getParameter(String name) on the wrapped request object.- Specified by:
getParameterin interfaceServletRequest- 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()
The default behavior of this method is to return getParameterMap() on the wrapped request object.- Specified by:
getParameterMapin interfaceServletRequest- 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()
The default behavior of this method is to return getParameterNames() on the wrapped request object.- Specified by:
getParameterNamesin interfaceServletRequest- Returns:
- an
EnumerationofStringobjects, eachStringcontaining the name of a request parameter; or an emptyEnumerationif the request has no parameters
-
getParameterValues
public String[] getParameterValues(String name)
The default behavior of this method is to return getParameterValues(String name) on the wrapped request object.- Specified by:
getParameterValuesin interfaceServletRequest- 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)
-
getProtocol
public String getProtocol()
The default behavior of this method is to return getProtocol() on the wrapped request object.- Specified by:
getProtocolin interfaceServletRequest- Returns:
- a
Stringcontaining the protocol name and version number
-
getScheme
public String getScheme()
The default behavior of this method is to return getScheme() on the wrapped request object.- Specified by:
getSchemein interfaceServletRequest- Returns:
- a
Stringcontaining the name of the scheme used to make this request
-
getServerName
public String getServerName()
The default behavior of this method is to return getServerName() on the wrapped request object.- Specified by:
getServerNamein interfaceServletRequest- Returns:
- a
Stringcontaining the name of the server
-
getServerPort
public int getServerPort()
The default behavior of this method is to return getServerPort() on the wrapped request object.- Specified by:
getServerPortin interfaceServletRequest- Returns:
- an integer specifying the port number
-
getReader
public BufferedReader getReader() throws IOException
The default behavior of this method is to return getReader() on the wrapped request object.- Specified by:
getReaderin interfaceServletRequest- 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()
-
getRemoteAddr
public String getRemoteAddr()
The default behavior of this method is to return getRemoteAddr() on the wrapped request object.- Specified by:
getRemoteAddrin interfaceServletRequest- Returns:
- a
Stringcontaining an IP address
-
getRemoteHost
public String getRemoteHost()
The default behavior of this method is to return getRemoteHost() on the wrapped request object.- Specified by:
getRemoteHostin interfaceServletRequest- Returns:
- a
Stringcontaining a fully qualified name or IP address.
-
setAttribute
public void setAttribute(String name, Object o)
The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.- Specified by:
setAttributein interfaceServletRequest- Parameters:
name- aStringspecifying the name of the attributeo- theObjectto be stored
-
removeAttribute
public void removeAttribute(String name)
The default behavior of this method is to call removeAttribute(String name) on the wrapped request object.- Specified by:
removeAttributein interfaceServletRequest- Parameters:
name- aStringspecifying the name of the attribute to remove
-
getLocale
public Locale getLocale()
The default behavior of this method is to return getLocale() on the wrapped request object.- Specified by:
getLocalein interfaceServletRequest- Returns:
- the preferred
Localefor the client
-
getLocales
public Enumeration<Locale> getLocales()
The default behavior of this method is to return getLocales() on the wrapped request object.- Specified by:
getLocalesin interfaceServletRequest- Returns:
- an
Enumerationof preferredLocaleobjects for the client
-
isSecure
public boolean isSecure()
The default behavior of this method is to return isSecure() on the wrapped request object.- Specified by:
isSecurein interfaceServletRequest- Returns:
- a boolean indicating if the request was made using a secure channel
-
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.- Specified by:
getRequestDispatcherin interfaceServletRequest- Parameters:
path- aStringspecifying the pathname to the resource. If it is relative, it must be relative against the current servlet.- Returns:
- a
RequestDispatcherobject that acts as a wrapper for the resource at the specified path, ornullif the servlet container cannot return aRequestDispatcher - See Also:
RequestDispatcher,ServletContext.getRequestDispatcher(java.lang.String)
-
getRemotePort
public int getRemotePort()
The default behavior of this method is to return getRemotePort() on the wrapped request object.- Specified by:
getRemotePortin interfaceServletRequest- Returns:
- an integer specifying the port number
- Since:
- Servlet 2.4
-
getLocalName
public String getLocalName()
The default behavior of this method is to return getLocalName() on the wrapped request object.- Specified by:
getLocalNamein interfaceServletRequest- Returns:
- a
Stringcontaining the host name of the IP on which the request was received. - Since:
- Servlet 2.4
-
getLocalAddr
public String getLocalAddr()
The default behavior of this method is to return getLocalAddr() on the wrapped request object.- Specified by:
getLocalAddrin interfaceServletRequest- Returns:
- a
Stringcontaining an IP address. - Since:
- Servlet 2.4
-
getLocalPort
public int getLocalPort()
The default behavior of this method is to return getLocalPort() on the wrapped request object.- Specified by:
getLocalPortin interfaceServletRequest- Returns:
- an integer specifying a port number
- Since:
- Servlet 2.4
-
getServletContext
public ServletContext getServletContext()
Gets the servlet context to which the wrapped servlet request was last dispatched.- Specified by:
getServletContextin interfaceServletRequest- Returns:
- the servlet context to which the wrapped servlet request was last dispatched
- Since:
- Servlet 3.0
-
startAsync
public AsyncContext startAsync() throws IllegalStateException
The default behavior of this method is to invokeServletRequest.startAsync()on the wrapped request object.- Specified by:
startAsyncin interfaceServletRequest- Returns:
- the (re)initialized AsyncContext
- Throws:
IllegalStateException- if the request is within the scope of a filter or servlet that does not support asynchronous operations (that is,isAsyncSupported()returns false), or if this method is called again without any asynchronous dispatch (resulting from one of theAsyncContext.dispatch()methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch, or if the response has already been closed- Since:
- Servlet 3.0
- See Also:
ServletRequest.startAsync()
-
startAsync
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
The default behavior of this method is to invokeServletRequest.startAsync(ServletRequest, ServletResponse)on the wrapped request object.- Specified by:
startAsyncin interfaceServletRequest- Parameters:
servletRequest- the ServletRequest used to initialize the AsyncContextservletResponse- the ServletResponse used to initialize the AsyncContext- Returns:
- the (re)initialized AsyncContext
- Throws:
IllegalStateException- if the request is within the scope of a filter or servlet that does not support asynchronous operations (that is,isAsyncSupported()returns false), or if this method is called again without any asynchronous dispatch (resulting from one of theAsyncContext.dispatch()methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch, or if the response has already been closed- Since:
- Servlet 3.0
- See Also:
ServletRequest.startAsync(ServletRequest, ServletResponse)
-
isAsyncStarted
public boolean isAsyncStarted()
Checks if the wrapped request has been put into asynchronous mode.- Specified by:
isAsyncStartedin interfaceServletRequest- Returns:
- true if this request has been put into asynchronous mode, false otherwise
- Since:
- Servlet 3.0
- See Also:
ServletRequest.isAsyncStarted()
-
isAsyncSupported
public boolean isAsyncSupported()
Checks if the wrapped request supports asynchronous operation.- Specified by:
isAsyncSupportedin interfaceServletRequest- Returns:
- true if this request supports asynchronous operation, false otherwise
- Since:
- Servlet 3.0
- See Also:
ServletRequest.isAsyncSupported()
-
getAsyncContext
public AsyncContext getAsyncContext()
Gets the AsyncContext that was created or reinitialized by the most recent invocation ofstartAsync()orstartAsync(ServletRequest,ServletResponse)on the wrapped request.- Specified by:
getAsyncContextin interfaceServletRequest- Returns:
- the AsyncContext that was created or reinitialized by the most recent invocation of
startAsync()orstartAsync(ServletRequest,ServletResponse)on the wrapped request - Throws:
IllegalStateException- if this request has not been put into asynchronous mode, i.e., if neitherstartAsync()norstartAsync(ServletRequest,ServletResponse)has been called- Since:
- Servlet 3.0
- See Also:
ServletRequest.getAsyncContext()
-
isWrapperFor
public boolean isWrapperFor(ServletRequest wrapped)
Checks (recursively) if this ServletRequestWrapper wraps the givenServletRequestinstance.- Parameters:
wrapped- the ServletRequest instance to search for- Returns:
- true if this ServletRequestWrapper wraps the given ServletRequest instance, false otherwise
- Since:
- Servlet 3.0
-
isWrapperFor
public boolean isWrapperFor(Class<?> wrappedType)
Checks (recursively) if this ServletRequestWrapper wraps aServletRequestof the given class type.- Parameters:
wrappedType- the ServletRequest class type to search for- Returns:
- true if this ServletRequestWrapper wraps a ServletRequest of the given class type, false otherwise
- Throws:
IllegalArgumentException- if the given class does not implementServletRequest- Since:
- Servlet 3.0
-
getDispatcherType
public DispatcherType getDispatcherType()
Gets the dispatcher type of the wrapped request.- Specified by:
getDispatcherTypein interfaceServletRequest- Returns:
- the dispatcher type of the wrapped request
- Since:
- Servlet 3.0
- See Also:
ServletRequest.getDispatcherType()
-
getRequestId
public String getRequestId()
Gets the request ID for the wrapped request.- Specified by:
getRequestIdin interfaceServletRequest- Returns:
- the request ID for the wrapped request
- Since:
- Servlet 6.0
-
getProtocolRequestId
public String getProtocolRequestId()
Gets the protocol defined request ID, if any, for the wrapped request.- Specified by:
getProtocolRequestIdin interfaceServletRequest- Returns:
- the protocol defined request ID, if any, for the wrapped request
- Since:
- Servlet 6.0
-
getServletConnection
public ServletConnection getServletConnection()
Gets the connection information for the wrapped request.- Specified by:
getServletConnectionin interfaceServletRequest- Returns:
- the connection information for the wrapped request
- Since:
- Servlet 6.0
-
-