Class MockHttpServletRequest

  • All Implemented Interfaces:
    com.helger.commons.locale.IHasLocale, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest
    Direct Known Subclasses:
    OfflineHttpServletRequest

    @NotThreadSafe
    public class MockHttpServletRequest
    extends Object
    implements jakarta.servlet.http.HttpServletRequest, com.helger.commons.locale.IHasLocale
    Mock implementation of HttpServletRequest.
    Author:
    Philip Helger
    • Field Detail

      • DEFAULT_INVOKE_HTTP_LISTENER

        public static final boolean DEFAULT_INVOKE_HTTP_LISTENER
        See Also:
        Constant Field Values
      • DEFAULT_PROTOCOL

        public static final String DEFAULT_PROTOCOL
      • DEFAULT_SERVER_PORT

        public static final int DEFAULT_SERVER_PORT
      • DEFAULT_METHOD

        public static final com.helger.commons.http.EHttpMethod DEFAULT_METHOD
        The default HTTP method: GET
    • Constructor Detail

      • MockHttpServletRequest

        public MockHttpServletRequest​(@Nullable
                                      jakarta.servlet.ServletContext aServletContext)
        Create a new MockHttpServletRequest.
        Parameters:
        aServletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext)
        See Also:
        MockServletContext
      • MockHttpServletRequest

        public MockHttpServletRequest​(@Nullable
                                      jakarta.servlet.ServletContext aServletContext,
                                      @Nullable
                                      com.helger.commons.http.EHttpMethod eMethod)
        Create a new MockHttpServletRequest.
        Parameters:
        aServletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext)
        eMethod - the request method (may be null)
        See Also:
        MockServletContext
      • MockHttpServletRequest

        public MockHttpServletRequest​(@Nullable
                                      jakarta.servlet.ServletContext aServletContext,
                                      @Nullable
                                      com.helger.commons.http.EHttpMethod eMethod,
                                      boolean bInvokeHttpListeners)
        Create a new MockHttpServletRequest.
        Parameters:
        aServletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext)
        eMethod - the request method (may be null)
        bInvokeHttpListeners - if true than the HTTP request event listeners from MockHttpListener are triggered
        See Also:
        setMethod(com.helger.commons.http.EHttpMethod), MockServletContext
    • Method Detail

      • getServletContext

        @Nonnull
        public final jakarta.servlet.ServletContext getServletContext()
        Specified by:
        getServletContext in interface jakarta.servlet.ServletRequest
        Returns:
        the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.). Never null.
      • isActive

        public boolean isActive()
        Returns:
        whether this request is still active (that is, not completed yet).
      • close

        public void close()
        Mark this request as completed, keeping its state.
      • invalidate

        public void invalidate()
        Invalidate this request, clearing its state and invoking all HTTP event listener.
        See Also:
        close(), clearAttributes()
      • checkActive

        protected void checkActive()
        Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
      • getAttribute

        @Nullable
        public Object getAttribute​(@Nullable
                                   String sName)
        Specified by:
        getAttribute in interface jakarta.servlet.ServletRequest
      • getAttributeNames

        @Nonnull
        public Enumeration<String> getAttributeNames()
        Specified by:
        getAttributeNames in interface jakarta.servlet.ServletRequest
      • setCharacterEncoding

        public void setCharacterEncoding​(@Nullable
                                         String sCharacterEncoding)
        Specified by:
        setCharacterEncoding in interface jakarta.servlet.ServletRequest
      • setCharacterEncoding

        public void setCharacterEncoding​(@Nullable
                                         Charset aCharacterEncoding)
      • getCharacterEncoding

        @Nullable
        public String getCharacterEncoding()
        Specified by:
        getCharacterEncoding in interface jakarta.servlet.ServletRequest
      • getCharacterEncodingObjOrDefault

        @Nonnull
        public Charset getCharacterEncodingObjOrDefault()
      • getContentLength

        @CheckForSigned
        public int getContentLength()
        Specified by:
        getContentLength in interface jakarta.servlet.ServletRequest
      • getContentType

        @Nullable
        public String getContentType()
        Specified by:
        getContentType in interface jakarta.servlet.ServletRequest
      • getInputStream

        @Nullable
        public jakarta.servlet.ServletInputStream getInputStream()
        Note: do not change the content via setContent(byte[]), while an input stream is open, because this may lead to indeterministic results!
        Specified by:
        getInputStream in interface jakarta.servlet.ServletRequest
        Returns:
        null if no content is present. If non- null the caller is responsible for closing the InputStream.
      • setParameter

        @Nonnull
        public MockHttpServletRequest setParameter​(@Nonnull
                                                   String sName,
                                                   @Nullable
                                                   String sValue)
        Set a single value for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, they will be replaced.

        Parameters:
        sName - Parameter name
        sValue - Parameter value
        Returns:
        this
      • setParameter

        @Nonnull
        public MockHttpServletRequest setParameter​(@Nonnull
                                                   String sName,
                                                   @Nullable
                                                   String[] aValues)
        Set an array of values for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, they will be replaced.

        Parameters:
        sName - Parameter name
        aValues - Parameter values
        Returns:
        this
      • setParameters

        @Nonnull
        public MockHttpServletRequest setParameters​(@Nullable
                                                    List<? extends com.helger.commons.url.URLParameter> aParams)
        Sets all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, use addParameters(List).
        Parameters:
        aParams - Parameter name value map. May be null.
        Returns:
        this
      • addParameter

        @Nonnull
        public final MockHttpServletRequest addParameter​(@Nonnull
                                                         String sName,
                                                         @Nullable
                                                         String sValue)
        Add a single value for the specified HTTP parameter.

        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.

        Parameters:
        sName - Parameter name
        sValue - Parameter value
        Returns:
        this
      • addParameter

        @Nonnull
        public final MockHttpServletRequest addParameter​(@Nonnull
                                                         String sName,
                                                         @Nullable
                                                         String[] aValues)
        Add an array of values for the specified HTTP parameter.

        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.

        Parameters:
        sName - Parameter name
        aValues - Parameter values
        Returns:
        this
      • addParameters

        @Nonnull
        public MockHttpServletRequest addParameters​(@Nullable
                                                    List<? extends com.helger.commons.url.URLParameter> aParams)
        Adds all provided parameters without replacing any existing values. To replace existing values, use setParameters(List).
        Parameters:
        aParams - Parameter name value map
        Returns:
        this
      • removeParameter

        @Nonnull
        public MockHttpServletRequest removeParameter​(@Nonnull
                                                      String sName)
        Remove already registered values for the specified HTTP parameter, if any.
        Parameters:
        sName - Parameter name
        Returns:
        this
      • getParameter

        @Nullable
        public String getParameter​(@Nonnull
                                   String sName)
        Specified by:
        getParameter in interface jakarta.servlet.ServletRequest
      • getParameterNames

        @Nonnull
        public Enumeration<String> getParameterNames()
        Specified by:
        getParameterNames in interface jakarta.servlet.ServletRequest
      • getParameterValues

        @Nullable
        @ReturnsMutableCopy
        public String[] getParameterValues​(@Nonnull
                                           String sName)
        Specified by:
        getParameterValues in interface jakarta.servlet.ServletRequest
      • getParameterMap

        @Nonnull
        @ReturnsMutableCopy
        public com.helger.commons.collection.impl.ICommonsOrderedMap<String,​String[]> getParameterMap()
        Specified by:
        getParameterMap in interface jakarta.servlet.ServletRequest
      • getProtocol

        @Nullable
        public String getProtocol()
        Specified by:
        getProtocol in interface jakarta.servlet.ServletRequest
      • getScheme

        @Nullable
        public String getScheme()
        Specified by:
        getScheme in interface jakarta.servlet.ServletRequest
      • getServerName

        @Nullable
        public String getServerName()
        Specified by:
        getServerName in interface jakarta.servlet.ServletRequest
      • getServerPort

        public int getServerPort()
        Specified by:
        getServerPort in interface jakarta.servlet.ServletRequest
      • getReader

        @Nullable
        public BufferedReader getReader()
        Specified by:
        getReader in interface jakarta.servlet.ServletRequest
      • getRemoteAddr

        @Nullable
        public String getRemoteAddr()
        Specified by:
        getRemoteAddr in interface jakarta.servlet.ServletRequest
      • getRemoteHost

        @Nullable
        public String getRemoteHost()
        Specified by:
        getRemoteHost in interface jakarta.servlet.ServletRequest
      • setAttribute

        public void setAttribute​(@Nonnull
                                 String sName,
                                 @Nullable
                                 Object aValue)
        Specified by:
        setAttribute in interface jakarta.servlet.ServletRequest
      • removeAttribute

        public void removeAttribute​(@Nonnull
                                    String sName)
        Specified by:
        removeAttribute in interface jakarta.servlet.ServletRequest
      • getLocale

        @Nonnull
        public Locale getLocale()
        Specified by:
        getLocale in interface com.helger.commons.locale.IHasLocale
        Specified by:
        getLocale in interface jakarta.servlet.ServletRequest
      • getLocales

        @Nonnull
        @Nonempty
        public Enumeration<Locale> getLocales()
        Specified by:
        getLocales in interface jakarta.servlet.ServletRequest
      • isSecure

        public boolean isSecure()
        Specified by:
        isSecure in interface jakarta.servlet.ServletRequest
      • getRemotePort

        public int getRemotePort()
        Specified by:
        getRemotePort in interface jakarta.servlet.ServletRequest
      • getLocalName

        @Nullable
        public String getLocalName()
        Specified by:
        getLocalName in interface jakarta.servlet.ServletRequest
      • getLocalAddr

        @Nullable
        public String getLocalAddr()
        Specified by:
        getLocalAddr in interface jakarta.servlet.ServletRequest
      • getLocalPort

        public int getLocalPort()
        Specified by:
        getLocalPort in interface jakarta.servlet.ServletRequest
      • getAuthType

        @Nullable
        public String getAuthType()
        Specified by:
        getAuthType in interface jakarta.servlet.http.HttpServletRequest
      • getCookies

        @Nullable
        public jakarta.servlet.http.Cookie[] getCookies()
        Specified by:
        getCookies in interface jakarta.servlet.http.HttpServletRequest
      • getDateHeader

        @UnsupportedOperation
        @Deprecated(forRemoval=false)
        public long getDateHeader​(@Nullable
                                  String sName)
        Deprecated.
        Specified by:
        getDateHeader in interface jakarta.servlet.http.HttpServletRequest
      • getIntHeader

        @UnsupportedOperation
        @Deprecated(forRemoval=false)
        public int getIntHeader​(@Nullable
                                String sName)
        Deprecated.
        Specified by:
        getIntHeader in interface jakarta.servlet.http.HttpServletRequest
      • getHeader

        @Nullable
        public String getHeader​(@Nullable
                                String sName)
        Specified by:
        getHeader in interface jakarta.servlet.http.HttpServletRequest
      • getHeaderNames

        @Nonnull
        public Enumeration<String> getHeaderNames()
        Specified by:
        getHeaderNames in interface jakarta.servlet.http.HttpServletRequest
      • getMethodEnum

        @Nullable
        public com.helger.commons.http.EHttpMethod getMethodEnum()
      • getMethod

        @Nullable
        public String getMethod()
        Specified by:
        getMethod in interface jakarta.servlet.http.HttpServletRequest
      • getPathInfo

        @Nullable
        public String getPathInfo()
        Specified by:
        getPathInfo in interface jakarta.servlet.http.HttpServletRequest
      • getPathTranslated

        @Nullable
        public String getPathTranslated()
        Specified by:
        getPathTranslated in interface jakarta.servlet.http.HttpServletRequest
      • getContextPath

        @Nullable
        public String getContextPath()
        Specified by:
        getContextPath in interface jakarta.servlet.http.HttpServletRequest
      • getQueryString

        @Nullable
        public String getQueryString()
        Specified by:
        getQueryString in interface jakarta.servlet.http.HttpServletRequest
      • getRemoteUser

        @Nullable
        public String getRemoteUser()
        Specified by:
        getRemoteUser in interface jakarta.servlet.http.HttpServletRequest
      • isUserInRole

        public boolean isUserInRole​(@Nullable
                                    String sRole)
        Specified by:
        isUserInRole in interface jakarta.servlet.http.HttpServletRequest
      • getUserPrincipal

        @Nullable
        public Principal getUserPrincipal()
        Specified by:
        getUserPrincipal in interface jakarta.servlet.http.HttpServletRequest
      • getRequestedSessionId

        @Nullable
        public String getRequestedSessionId()
        Specified by:
        getRequestedSessionId in interface jakarta.servlet.http.HttpServletRequest
      • getRequestURI

        @Nullable
        public String getRequestURI()
        Specified by:
        getRequestURI in interface jakarta.servlet.http.HttpServletRequest
      • getRequestURL

        @Nonnull
        public StringBuffer getRequestURL()
        Specified by:
        getRequestURL in interface jakarta.servlet.http.HttpServletRequest
      • getServletPath

        @Nullable
        public String getServletPath()
        Specified by:
        getServletPath in interface jakarta.servlet.http.HttpServletRequest
      • setSessionID

        @Nonnull
        public MockHttpServletRequest setSessionID​(@Nullable
                                                   String sSessionID)
        Define the session ID to be used when creating a new session
        Parameters:
        sSessionID - The session ID to be used. If it is null a unique session ID is generated.
        Returns:
        this
      • getSessionID

        @Nullable
        public String getSessionID()
        Returns:
        The session ID to use or null if a new session ID should be generated!
      • getSession

        @Nullable
        public jakarta.servlet.http.HttpSession getSession​(boolean bCreate)
        Specified by:
        getSession in interface jakarta.servlet.http.HttpServletRequest
      • getSession

        @Nonnull
        public jakarta.servlet.http.HttpSession getSession()
        Specified by:
        getSession in interface jakarta.servlet.http.HttpServletRequest
      • isRequestedSessionIdValid

        public boolean isRequestedSessionIdValid()
        Specified by:
        isRequestedSessionIdValid in interface jakarta.servlet.http.HttpServletRequest
      • isRequestedSessionIdFromCookie

        public boolean isRequestedSessionIdFromCookie()
        Specified by:
        isRequestedSessionIdFromCookie in interface jakarta.servlet.http.HttpServletRequest
      • isRequestedSessionIdFromURL

        public boolean isRequestedSessionIdFromURL()
        Specified by:
        isRequestedSessionIdFromURL in interface jakarta.servlet.http.HttpServletRequest
      • isRequestedSessionIdFromUrl

        @Deprecated(forRemoval=false)
        public boolean isRequestedSessionIdFromUrl()
        Deprecated.
        Specified by:
        isRequestedSessionIdFromUrl in interface jakarta.servlet.http.HttpServletRequest
      • startAsync

        @UnsupportedOperation
        public jakarta.servlet.AsyncContext startAsync()
        Specified by:
        startAsync in interface jakarta.servlet.ServletRequest
      • startAsync

        @UnsupportedOperation
        public jakarta.servlet.AsyncContext startAsync​(jakarta.servlet.ServletRequest servletRequest,
                                                       jakarta.servlet.ServletResponse servletResponse)
        Specified by:
        startAsync in interface jakarta.servlet.ServletRequest
      • isAsyncStarted

        @UnsupportedOperation
        public boolean isAsyncStarted()
        Specified by:
        isAsyncStarted in interface jakarta.servlet.ServletRequest
      • isAsyncSupported

        public boolean isAsyncSupported()
        Specified by:
        isAsyncSupported in interface jakarta.servlet.ServletRequest
      • getAsyncContext

        @UnsupportedOperation
        public jakarta.servlet.AsyncContext getAsyncContext()
        Specified by:
        getAsyncContext in interface jakarta.servlet.ServletRequest
      • getDispatcherType

        public jakarta.servlet.DispatcherType getDispatcherType()
        Specified by:
        getDispatcherType in interface jakarta.servlet.ServletRequest
      • authenticate

        @UnsupportedOperation
        public boolean authenticate​(jakarta.servlet.http.HttpServletResponse response)
                             throws IOException,
                                    jakarta.servlet.ServletException
        Specified by:
        authenticate in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        IOException
        jakarta.servlet.ServletException
      • login

        @UnsupportedOperation
        public void login​(String username,
                          String password)
                   throws jakarta.servlet.ServletException
        Specified by:
        login in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        jakarta.servlet.ServletException
      • logout

        @UnsupportedOperation
        public void logout()
                    throws jakarta.servlet.ServletException
        Specified by:
        logout in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        jakarta.servlet.ServletException
      • getParts

        @UnsupportedOperation
        public com.helger.commons.collection.impl.ICommonsCollection<jakarta.servlet.http.Part> getParts()
                                                                                                  throws IOException,
                                                                                                         jakarta.servlet.ServletException
        Specified by:
        getParts in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        IOException
        jakarta.servlet.ServletException
      • getPart

        @UnsupportedOperation
        public jakarta.servlet.http.Part getPart​(String name)
                                          throws IOException,
                                                 jakarta.servlet.ServletException
        Specified by:
        getPart in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        IOException
        jakarta.servlet.ServletException
      • getContentLengthLong

        public long getContentLengthLong()
        Specified by:
        getContentLengthLong in interface jakarta.servlet.ServletRequest
      • changeSessionId

        public String changeSessionId()
        Specified by:
        changeSessionId in interface jakarta.servlet.http.HttpServletRequest
      • upgrade

        public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade​(Class<T> handlerClass)
                                                                      throws IOException,
                                                                             jakarta.servlet.ServletException
        Specified by:
        upgrade in interface jakarta.servlet.http.HttpServletRequest
        Throws:
        IOException
        jakarta.servlet.ServletException