public class MockHttpServletRequest extends Object implements javax.servlet.http.HttpServletRequest
HttpServletRequest
interface.
Compatible with Servlet 2.5 and partially with Servlet 3.0 (notable exceptions:
the getPart(s)
and startAsync
families of methods).
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_PROTOCOL
The default protocol: 'http'.
|
static String |
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.
|
static String |
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.
|
static String |
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.
|
static String |
DEFAULT_SERVER_NAME
The default server name: 'localhost'.
|
static int |
DEFAULT_SERVER_PORT
The default server port: '80'.
|
Constructor and Description |
---|
MockHttpServletRequest()
Create a new
MockHttpServletRequest with a default
MockServletContext . |
MockHttpServletRequest(javax.servlet.ServletContext servletContext)
Create a new
MockHttpServletRequest with the supplied ServletContext . |
MockHttpServletRequest(javax.servlet.ServletContext servletContext,
String method,
String requestURI)
Create a new
MockHttpServletRequest with the supplied ServletContext ,
method , and requestURI . |
MockHttpServletRequest(String method,
String requestURI)
Create a new
MockHttpServletRequest with a default
MockServletContext . |
Modifier and Type | Method and Description |
---|---|
void |
addHeader(String name,
Object value)
Add a header entry for the given name.
|
void |
addParameter(String name,
String value)
Add a single value for the specified HTTP parameter.
|
void |
addParameter(String name,
String[] values)
Add an array of values for the specified HTTP parameter.
|
void |
addParameters(Map params)
Adds all provided parameters without replacing any
existing values.
|
void |
addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.
|
void |
addUserRole(String role) |
boolean |
authenticate(javax.servlet.http.HttpServletResponse response) |
protected void |
checkActive()
Check whether this request is still active (that is, not completed yet),
throwing an IllegalStateException if not active anymore.
|
void |
clearAttributes()
Clear all of this request's attributes.
|
void |
close()
Mark this request as completed, keeping its state.
|
javax.servlet.AsyncContext |
getAsyncContext() |
Object |
getAttribute(String name) |
Enumeration<String> |
getAttributeNames() |
String |
getAuthType() |
String |
getCharacterEncoding() |
int |
getContentLength() |
String |
getContentType() |
String |
getContextPath() |
javax.servlet.http.Cookie[] |
getCookies() |
long |
getDateHeader(String name) |
javax.servlet.DispatcherType |
getDispatcherType() |
String |
getHeader(String name) |
Enumeration<String> |
getHeaderNames() |
Enumeration<String> |
getHeaders(String name) |
javax.servlet.ServletInputStream |
getInputStream() |
int |
getIntHeader(String name) |
String |
getLocalAddr() |
Locale |
getLocale() |
Enumeration<Locale> |
getLocales() |
String |
getLocalName() |
int |
getLocalPort() |
String |
getMethod() |
String |
getParameter(String name) |
Map<String,String[]> |
getParameterMap() |
Enumeration<String> |
getParameterNames() |
String[] |
getParameterValues(String name) |
javax.servlet.http.Part |
getPart(String name) |
Collection<javax.servlet.http.Part> |
getParts() |
String |
getPathInfo() |
String |
getPathTranslated() |
String |
getProtocol() |
String |
getQueryString() |
BufferedReader |
getReader() |
String |
getRealPath(String path) |
String |
getRemoteAddr() |
String |
getRemoteHost() |
int |
getRemotePort() |
String |
getRemoteUser() |
javax.servlet.RequestDispatcher |
getRequestDispatcher(String path) |
String |
getRequestedSessionId() |
String |
getRequestURI() |
StringBuffer |
getRequestURL() |
String |
getScheme() |
String |
getServerName() |
int |
getServerPort() |
javax.servlet.ServletContext |
getServletContext()
Return the ServletContext that this request is associated with.
|
String |
getServletPath() |
javax.servlet.http.HttpSession |
getSession() |
javax.servlet.http.HttpSession |
getSession(boolean create) |
Principal |
getUserPrincipal() |
void |
invalidate()
Invalidate this request, clearing its state.
|
boolean |
isActive()
Return whether this request is still active (that is, not completed yet).
|
boolean |
isAsyncStarted() |
boolean |
isAsyncSupported() |
boolean |
isRequestedSessionIdFromCookie() |
boolean |
isRequestedSessionIdFromUrl() |
boolean |
isRequestedSessionIdFromURL() |
boolean |
isRequestedSessionIdValid() |
boolean |
isSecure() |
boolean |
isUserInRole(String role) |
void |
login(String username,
String password) |
void |
logout() |
void |
removeAllParameters()
Removes all existing parameters.
|
void |
removeAttribute(String name) |
void |
removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.
|
void |
setAttribute(String name,
Object value) |
void |
setAuthType(String authType) |
void |
setCharacterEncoding(String characterEncoding) |
void |
setContent(byte[] content) |
void |
setContentType(String contentType) |
void |
setContextPath(String contextPath) |
void |
setCookies(javax.servlet.http.Cookie... cookies) |
void |
setLocalAddr(String localAddr) |
void |
setLocalName(String localName) |
void |
setLocalPort(int localPort) |
void |
setMethod(String method) |
void |
setParameter(String name,
String value)
Set a single value for the specified HTTP parameter.
|
void |
setParameter(String name,
String[] values)
Set an array of values for the specified HTTP parameter.
|
void |
setParameters(Map params)
Sets all provided parameters replacing any existing
values for the provided parameter names.
|
void |
setPathInfo(String pathInfo) |
void |
setPreferredLocales(List<Locale> locales)
Set the list of preferred locales, in descending order, effectively replacing
any existing locales.
|
void |
setProtocol(String protocol) |
void |
setQueryString(String queryString) |
void |
setRemoteAddr(String remoteAddr) |
void |
setRemoteHost(String remoteHost) |
void |
setRemotePort(int remotePort) |
void |
setRemoteUser(String remoteUser) |
void |
setRequestedSessionId(String requestedSessionId) |
void |
setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) |
void |
setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) |
void |
setRequestedSessionIdValid(boolean requestedSessionIdValid) |
void |
setRequestURI(String requestURI) |
void |
setScheme(String scheme) |
void |
setSecure(boolean secure) |
void |
setServerName(String serverName) |
void |
setServerPort(int serverPort) |
void |
setServletPath(String servletPath) |
void |
setSession(javax.servlet.http.HttpSession session) |
void |
setUserPrincipal(Principal userPrincipal) |
javax.servlet.AsyncContext |
startAsync() |
javax.servlet.AsyncContext |
startAsync(javax.servlet.ServletRequest servletRequest,
javax.servlet.ServletResponse servletResponse) |
public static final String DEFAULT_PROTOCOL
public static final String DEFAULT_SERVER_ADDR
public static final String DEFAULT_SERVER_NAME
public static final int DEFAULT_SERVER_PORT
public static final String DEFAULT_REMOTE_ADDR
public static final String DEFAULT_REMOTE_HOST
public MockHttpServletRequest()
MockHttpServletRequest
with a default
MockServletContext
.public MockHttpServletRequest(String method, String requestURI)
MockHttpServletRequest
with a default
MockServletContext
.method
- the request method (may be null
)requestURI
- the request URI (may be null
)setMethod(java.lang.String)
,
setRequestURI(java.lang.String)
,
MockHttpServletRequest(ServletContext, String, String)
public MockHttpServletRequest(javax.servlet.ServletContext servletContext)
MockHttpServletRequest
with the supplied ServletContext
.servletContext
- the ServletContext that the request runs in
(may be null
to use a default MockServletContext
)MockHttpServletRequest(ServletContext, String, String)
public MockHttpServletRequest(javax.servlet.ServletContext servletContext, String method, String requestURI)
MockHttpServletRequest
with the supplied ServletContext
,
method
, and requestURI
.
The preferred locale will be set to Locale.ENGLISH
.
servletContext
- the ServletContext that the request runs in (may be
null
to use a default MockServletContext
)method
- the request method (may be null
)requestURI
- the request URI (may be null
)setMethod(java.lang.String)
,
setRequestURI(java.lang.String)
,
setPreferredLocales(java.util.List<java.util.Locale>)
,
MockServletContext
public javax.servlet.ServletContext getServletContext()
getServletContext
in interface javax.servlet.ServletRequest
public boolean isActive()
public void close()
public void invalidate()
protected void checkActive() throws IllegalStateException
IllegalStateException
public Object getAttribute(String name)
getAttribute
in interface javax.servlet.ServletRequest
public Enumeration<String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletRequest
public String getCharacterEncoding()
getCharacterEncoding
in interface javax.servlet.ServletRequest
public void setCharacterEncoding(String characterEncoding)
setCharacterEncoding
in interface javax.servlet.ServletRequest
public void setContent(byte[] content)
public int getContentLength()
getContentLength
in interface javax.servlet.ServletRequest
public void setContentType(String contentType)
public String getContentType()
getContentType
in interface javax.servlet.ServletRequest
public javax.servlet.ServletInputStream getInputStream()
getInputStream
in interface javax.servlet.ServletRequest
public void setParameter(String name, String value)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameter(String name, String[] values)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameters(Map params)
addParameters(java.util.Map)
.public void addParameter(String name, String value)
If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.
public void addParameter(String name, String[] values)
If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.
public void addParameters(Map params)
setParameters(java.util.Map)
.public void removeParameter(String name)
public void removeAllParameters()
public String getParameter(String name)
getParameter
in interface javax.servlet.ServletRequest
public Enumeration<String> getParameterNames()
getParameterNames
in interface javax.servlet.ServletRequest
public String[] getParameterValues(String name)
getParameterValues
in interface javax.servlet.ServletRequest
public Map<String,String[]> getParameterMap()
getParameterMap
in interface javax.servlet.ServletRequest
public void setProtocol(String protocol)
public String getProtocol()
getProtocol
in interface javax.servlet.ServletRequest
public void setScheme(String scheme)
public String getScheme()
getScheme
in interface javax.servlet.ServletRequest
public void setServerName(String serverName)
public String getServerName()
getServerName
in interface javax.servlet.ServletRequest
public void setServerPort(int serverPort)
public int getServerPort()
getServerPort
in interface javax.servlet.ServletRequest
public BufferedReader getReader() throws UnsupportedEncodingException
getReader
in interface javax.servlet.ServletRequest
UnsupportedEncodingException
public void setRemoteAddr(String remoteAddr)
public String getRemoteAddr()
getRemoteAddr
in interface javax.servlet.ServletRequest
public void setRemoteHost(String remoteHost)
public String getRemoteHost()
getRemoteHost
in interface javax.servlet.ServletRequest
public void setAttribute(String name, Object value)
setAttribute
in interface javax.servlet.ServletRequest
public void removeAttribute(String name)
removeAttribute
in interface javax.servlet.ServletRequest
public void clearAttributes()
public void addPreferredLocale(Locale locale)
public void setPreferredLocales(List<Locale> locales)
addPreferredLocale(java.util.Locale)
public Locale getLocale()
getLocale
in interface javax.servlet.ServletRequest
public Enumeration<Locale> getLocales()
getLocales
in interface javax.servlet.ServletRequest
public void setSecure(boolean secure)
public boolean isSecure()
isSecure
in interface javax.servlet.ServletRequest
public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
getRequestDispatcher
in interface javax.servlet.ServletRequest
public String getRealPath(String path)
getRealPath
in interface javax.servlet.ServletRequest
public void setRemotePort(int remotePort)
public int getRemotePort()
getRemotePort
in interface javax.servlet.ServletRequest
public void setLocalName(String localName)
public String getLocalName()
getLocalName
in interface javax.servlet.ServletRequest
public void setLocalAddr(String localAddr)
public String getLocalAddr()
getLocalAddr
in interface javax.servlet.ServletRequest
public void setLocalPort(int localPort)
public int getLocalPort()
getLocalPort
in interface javax.servlet.ServletRequest
public void setAuthType(String authType)
public String getAuthType()
getAuthType
in interface javax.servlet.http.HttpServletRequest
public void setCookies(javax.servlet.http.Cookie... cookies)
public javax.servlet.http.Cookie[] getCookies()
getCookies
in interface javax.servlet.http.HttpServletRequest
public void addHeader(String name, Object value)
If there was no entry for that header name before, the value will be used as-is. In case of an existing entry, a String array will be created, adding the given value (more specifically, its toString representation) as further element.
Multiple values can only be stored as list of Strings, following the
Servlet spec (see getHeaders
accessor). As alternative to
repeated addHeader
calls for individual elements, you can
use a single call with an entire array or Collection of values as
parameter.
public long getDateHeader(String name)
getDateHeader
in interface javax.servlet.http.HttpServletRequest
public String getHeader(String name)
getHeader
in interface javax.servlet.http.HttpServletRequest
public Enumeration<String> getHeaders(String name)
getHeaders
in interface javax.servlet.http.HttpServletRequest
public Enumeration<String> getHeaderNames()
getHeaderNames
in interface javax.servlet.http.HttpServletRequest
public int getIntHeader(String name)
getIntHeader
in interface javax.servlet.http.HttpServletRequest
public void setMethod(String method)
public String getMethod()
getMethod
in interface javax.servlet.http.HttpServletRequest
public void setPathInfo(String pathInfo)
public String getPathInfo()
getPathInfo
in interface javax.servlet.http.HttpServletRequest
public String getPathTranslated()
getPathTranslated
in interface javax.servlet.http.HttpServletRequest
public void setContextPath(String contextPath)
public String getContextPath()
getContextPath
in interface javax.servlet.http.HttpServletRequest
public void setQueryString(String queryString)
public String getQueryString()
getQueryString
in interface javax.servlet.http.HttpServletRequest
public void setRemoteUser(String remoteUser)
public String getRemoteUser()
getRemoteUser
in interface javax.servlet.http.HttpServletRequest
public void addUserRole(String role)
public boolean isUserInRole(String role)
isUserInRole
in interface javax.servlet.http.HttpServletRequest
public void setUserPrincipal(Principal userPrincipal)
public Principal getUserPrincipal()
getUserPrincipal
in interface javax.servlet.http.HttpServletRequest
public void setRequestedSessionId(String requestedSessionId)
public String getRequestedSessionId()
getRequestedSessionId
in interface javax.servlet.http.HttpServletRequest
public void setRequestURI(String requestURI)
public String getRequestURI()
getRequestURI
in interface javax.servlet.http.HttpServletRequest
public StringBuffer getRequestURL()
getRequestURL
in interface javax.servlet.http.HttpServletRequest
public void setServletPath(String servletPath)
public String getServletPath()
getServletPath
in interface javax.servlet.http.HttpServletRequest
public void setSession(javax.servlet.http.HttpSession session)
public javax.servlet.http.HttpSession getSession(boolean create)
getSession
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession()
getSession
in interface javax.servlet.http.HttpServletRequest
public void setRequestedSessionIdValid(boolean requestedSessionIdValid)
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid
in interface javax.servlet.http.HttpServletRequest
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie
in interface javax.servlet.http.HttpServletRequest
public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl
in interface javax.servlet.http.HttpServletRequest
public boolean authenticate(javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException
authenticate
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
public void login(String username, String password) throws javax.servlet.ServletException
login
in interface javax.servlet.http.HttpServletRequest
javax.servlet.ServletException
public void logout() throws javax.servlet.ServletException
logout
in interface javax.servlet.http.HttpServletRequest
javax.servlet.ServletException
public javax.servlet.AsyncContext startAsync() throws IllegalStateException
startAsync
in interface javax.servlet.ServletRequest
IllegalStateException
public javax.servlet.AsyncContext startAsync(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws IllegalStateException
startAsync
in interface javax.servlet.ServletRequest
IllegalStateException
public boolean isAsyncStarted()
isAsyncStarted
in interface javax.servlet.ServletRequest
public boolean isAsyncSupported()
isAsyncSupported
in interface javax.servlet.ServletRequest
public javax.servlet.AsyncContext getAsyncContext()
getAsyncContext
in interface javax.servlet.ServletRequest
public javax.servlet.DispatcherType getDispatcherType()
getDispatcherType
in interface javax.servlet.ServletRequest
public Collection<javax.servlet.http.Part> getParts() throws IOException, javax.servlet.ServletException
getParts
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
public javax.servlet.http.Part getPart(String name) throws IOException, javax.servlet.ServletException
getPart
in interface javax.servlet.http.HttpServletRequest
IOException
javax.servlet.ServletException
Copyright © 2015. All rights reserved.