Class MockHttpServletResponse

  • All Implemented Interfaces:
    jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletResponse

    @NotThreadSafe
    public class MockHttpServletResponse
    extends Object
    implements jakarta.servlet.http.HttpServletResponse
    Mock implementation of HttpServletResponse.
    Author:
    Philip Helger
    • Field Detail

      • DEFAULT_SERVER_PORT

        public static final int DEFAULT_SERVER_PORT
      • DEFAULT_CHARSET_OBJ

        public static final Charset DEFAULT_CHARSET_OBJ
    • Constructor Detail

      • MockHttpServletResponse

        public MockHttpServletResponse()
    • Method Detail

      • setOutputStreamAccessAllowed

        public void setOutputStreamAccessAllowed​(boolean bOutputStreamAccessAllowed)
        Set whether getOutputStream() access is allowed.

        Default is true.

        Parameters:
        bOutputStreamAccessAllowed - true to enabled
      • isOutputStreamAccessAllowed

        public boolean isOutputStreamAccessAllowed()
        Returns:
        whether getOutputStream() access is allowed.
      • setWriterAccessAllowed

        public void setWriterAccessAllowed​(boolean bWriterAccessAllowed)
        Set whether getWriter() access is allowed.

        Default is true.

        Parameters:
        bWriterAccessAllowed - true to enabled
      • isWriterAccessAllowed

        public boolean isWriterAccessAllowed()
        Returns:
        whether getOutputStream() access is allowed.
      • setCharacterEncoding

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

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

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

        @Nonnull
        public Charset getCharacterEncodingObjOrDefault()
      • getOutputStream

        @Nonnull
        public jakarta.servlet.ServletOutputStream getOutputStream()
        Specified by:
        getOutputStream in interface jakarta.servlet.ServletResponse
      • getWriter

        @Nonnull
        public PrintWriter getWriter()
        Specified by:
        getWriter in interface jakarta.servlet.ServletResponse
      • getContentAsByteArray

        @Nonnull
        @ReturnsMutableCopy
        public byte[] getContentAsByteArray()
      • setContentLength

        public void setContentLength​(int nContentLength)
        Specified by:
        setContentLength in interface jakarta.servlet.ServletResponse
      • getContentLength

        public int getContentLength()
      • setContentType

        public void setContentType​(@Nullable
                                   String sContentType)
        Specified by:
        setContentType in interface jakarta.servlet.ServletResponse
      • getContentType

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

        public void setBufferSize​(int nBufferSize)
        Specified by:
        setBufferSize in interface jakarta.servlet.ServletResponse
      • getBufferSize

        public int getBufferSize()
        Specified by:
        getBufferSize in interface jakarta.servlet.ServletResponse
      • flushBuffer

        public void flushBuffer()
        Specified by:
        flushBuffer in interface jakarta.servlet.ServletResponse
      • resetBuffer

        public void resetBuffer()
        Specified by:
        resetBuffer in interface jakarta.servlet.ServletResponse
      • setCommitted

        public void setCommitted​(boolean bCommitted)
      • isCommitted

        public boolean isCommitted()
        Specified by:
        isCommitted in interface jakarta.servlet.ServletResponse
      • reset

        public void reset()
        Specified by:
        reset in interface jakarta.servlet.ServletResponse
      • setLocale

        public void setLocale​(@Nullable
                              Locale aLocale)
        Specified by:
        setLocale in interface jakarta.servlet.ServletResponse
      • getLocale

        @Nullable
        public Locale getLocale()
        Specified by:
        getLocale in interface jakarta.servlet.ServletResponse
      • addCookie

        public void addCookie​(@Nonnull
                              jakarta.servlet.http.Cookie aCookie)
        Specified by:
        addCookie in interface jakarta.servlet.http.HttpServletResponse
      • getCookies

        @Nonnull
        @ReturnsMutableCopy
        public jakarta.servlet.http.Cookie[] getCookies()
      • containsHeader

        public boolean containsHeader​(@Nullable
                                      String sName)
        Specified by:
        containsHeader in interface jakarta.servlet.http.HttpServletResponse
      • getHeaderNames

        @Nonnull
        @ReturnsMutableCopy
        public com.helger.commons.collection.impl.ICommonsSet<String> getHeaderNames()
        Return the names of all specified headers as a Set of Strings.
        Specified by:
        getHeaderNames in interface jakarta.servlet.http.HttpServletResponse
        Returns:
        the Set of header name Strings, or an empty Set if none
      • getHeader

        @Nullable
        public String getHeader​(@Nullable
                                String sName)
        Return the primary value for the given header, if any.

        Will return the first value in case of multiple values.

        Specified by:
        getHeader in interface jakarta.servlet.http.HttpServletResponse
        Parameters:
        sName - the name of the header
        Returns:
        the associated header value, or null if none
      • getHeaders

        @Nonnull
        public com.helger.commons.collection.impl.ICommonsList<String> getHeaders​(@Nullable
                                                                                  String sName)
        Return all values for the given header as a List of value objects.
        Specified by:
        getHeaders in interface jakarta.servlet.http.HttpServletResponse
        Parameters:
        sName - the name of the header
        Returns:
        the associated header values, or an empty List if none
      • encodeURL

        @Nullable
        public String encodeURL​(@Nullable
                                String sUrl)
        The default implementation returns the given URL String as-is. Use setEncodeUrlSuffix(String) to define a suffix to be appended.
        Specified by:
        encodeURL in interface jakarta.servlet.http.HttpServletResponse
        Returns:
        the encoded URL
      • encodeRedirectURL

        @Nullable
        public String encodeRedirectURL​(@Nullable
                                        String sUrl)
        The default implementation returns the given URL String as-is. Use setEncodeRedirectUrlSuffix(String) to define a suffix to be appended.
        Specified by:
        encodeRedirectURL in interface jakarta.servlet.http.HttpServletResponse
        Returns:
        the encoded URL
      • encodeRedirectUrl

        @Deprecated(forRemoval=false)
        public String encodeRedirectUrl​(@Nullable
                                        String sUrl)
        Deprecated.
        Specified by:
        encodeRedirectUrl in interface jakarta.servlet.http.HttpServletResponse
      • sendError

        public void sendError​(int nStatus,
                              @Nullable
                              String sErrorMessage)
                       throws IOException
        Specified by:
        sendError in interface jakarta.servlet.http.HttpServletResponse
        Throws:
        IOException
      • sendError

        public void sendError​(int nStatus)
                       throws IOException
        Specified by:
        sendError in interface jakarta.servlet.http.HttpServletResponse
        Throws:
        IOException
      • sendRedirect

        public void sendRedirect​(@Nonnull
                                 String sUrl)
                          throws IOException
        Specified by:
        sendRedirect in interface jakarta.servlet.http.HttpServletResponse
        Throws:
        IOException
      • setDateHeader

        public void setDateHeader​(@Nullable
                                  String sName,
                                  long nValue)
        Specified by:
        setDateHeader in interface jakarta.servlet.http.HttpServletResponse
      • addDateHeader

        public void addDateHeader​(@Nullable
                                  String sName,
                                  long nValue)
        Specified by:
        addDateHeader in interface jakarta.servlet.http.HttpServletResponse
      • setHeader

        public void setHeader​(@Nullable
                              String sName,
                              @Nullable
                              String sValue)
        Specified by:
        setHeader in interface jakarta.servlet.http.HttpServletResponse
      • addHeader

        public void addHeader​(@Nullable
                              String sName,
                              @Nullable
                              String sValue)
        Specified by:
        addHeader in interface jakarta.servlet.http.HttpServletResponse
      • setIntHeader

        public void setIntHeader​(@Nullable
                                 String sName,
                                 int nValue)
        Specified by:
        setIntHeader in interface jakarta.servlet.http.HttpServletResponse
      • addIntHeader

        public void addIntHeader​(@Nullable
                                 String sName,
                                 int nValue)
        Specified by:
        addIntHeader in interface jakarta.servlet.http.HttpServletResponse
      • setStatus

        public void setStatus​(int nStatus)
        Specified by:
        setStatus in interface jakarta.servlet.http.HttpServletResponse
      • setStatus

        @Deprecated(forRemoval=false)
        public void setStatus​(int nStatus,
                              @Nullable
                              String sErrorMessage)
        Deprecated.
        Specified by:
        setStatus in interface jakarta.servlet.http.HttpServletResponse
      • getStatus

        public int getStatus()
        Specified by:
        getStatus in interface jakarta.servlet.http.HttpServletResponse
      • setForwardedUrl

        public void setForwardedUrl​(@Nullable
                                    String sForwardedUrl)
      • setIncludedUrl

        public void setIncludedUrl​(@Nullable
                                   String sIncludedUrl)
      • setEncodeUrlSuffix

        public void setEncodeUrlSuffix​(@Nullable
                                       String sEncodeUrlSuffix)
      • setEncodeRedirectUrlSuffix

        public void setEncodeRedirectUrlSuffix​(@Nullable
                                               String sEncodeRedirectUrlSuffix)
      • getEncodeRedirectUrlSuffix

        @Nullable
        public String getEncodeRedirectUrlSuffix()
      • setContentLengthLong

        public void setContentLengthLong​(long len)
        Specified by:
        setContentLengthLong in interface jakarta.servlet.ServletResponse