Class CookieHelper


  • @Immutable
    public final class CookieHelper
    extends Object
    Misc. helper methods on HTTP cookies.
    Author:
    Philip Helger
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsCookie​(jakarta.servlet.http.HttpServletRequest aHttpRequest, String sCookieName)  
      static jakarta.servlet.http.Cookie createContextCookie​(String sName, String sValue, boolean bExpireWhenBrowserIsClosed, boolean bSecure)
      Create a cookie that is bound to the servlet context path within the local web server.
      static jakarta.servlet.http.Cookie createCookie​(String sName, String sValue, String sPath, boolean bExpireWhenBrowserIsClosed, boolean bSecure)
      Create a cookie that is bound on a certain path within the local web server.
      static com.helger.commons.collection.impl.ICommonsOrderedMap<String,​jakarta.servlet.http.Cookie> getAllCookies​(jakarta.servlet.http.HttpServletRequest aHttpRequest)  
      static jakarta.servlet.http.Cookie getCookie​(jakarta.servlet.http.HttpServletRequest aHttpRequest, String sCookieName)  
      static void removeCookie​(jakarta.servlet.http.HttpServletResponse aHttpResponse, jakarta.servlet.http.Cookie aCookie)
      Remove a cookie by setting the max age to 0.
    • Field Detail

      • DEFAULT_MAX_AGE_SECONDS

        public static final int DEFAULT_MAX_AGE_SECONDS
        See Also:
        Constant Field Values
    • Method Detail

      • getAllCookies

        @Nonnull
        @ReturnsMutableCopy
        public static com.helger.commons.collection.impl.ICommonsOrderedMap<String,​jakarta.servlet.http.Cookie> getAllCookies​(@Nonnull
                                                                                                                                    jakarta.servlet.http.HttpServletRequest aHttpRequest)
      • getCookie

        @Nullable
        public static jakarta.servlet.http.Cookie getCookie​(@Nonnull
                                                            jakarta.servlet.http.HttpServletRequest aHttpRequest,
                                                            @Nonnull
                                                            String sCookieName)
      • containsCookie

        public static boolean containsCookie​(@Nonnull
                                             jakarta.servlet.http.HttpServletRequest aHttpRequest,
                                             @Nonnull
                                             String sCookieName)
      • createCookie

        @Nonnull
        public static jakarta.servlet.http.Cookie createCookie​(@Nonnull
                                                               String sName,
                                                               @Nullable
                                                               String sValue,
                                                               String sPath,
                                                               boolean bExpireWhenBrowserIsClosed,
                                                               boolean bSecure)
        Create a cookie that is bound on a certain path within the local web server.
        Parameters:
        sName - The cookie name.
        sValue - The cookie value.
        sPath - The path the cookie is valid for.
        bExpireWhenBrowserIsClosed - true if this is a browser session cookie
        bSecure - true to send the cookie from the browser to the server only when using a secure protocol (e.g. https)
        Returns:
        The created cookie object.
        Since:
        9.3.2
      • createContextCookie

        @Nonnull
        public static jakarta.servlet.http.Cookie createContextCookie​(@Nonnull
                                                                      String sName,
                                                                      @Nullable
                                                                      String sValue,
                                                                      boolean bExpireWhenBrowserIsClosed,
                                                                      boolean bSecure)
        Create a cookie that is bound to the servlet context path within the local web server.
        Parameters:
        sName - The cookie name.
        sValue - The cookie value.
        bExpireWhenBrowserIsClosed - true if this is a browser session cookie
        bSecure - true to send the cookie from the browser to the server only when using a secure protocol (e.g. https)
        Returns:
        The created cookie object.
        Since:
        9.3.2
      • removeCookie

        public static void removeCookie​(@Nonnull
                                        jakarta.servlet.http.HttpServletResponse aHttpResponse,
                                        @Nonnull
                                        jakarta.servlet.http.Cookie aCookie)
        Remove a cookie by setting the max age to 0.
        Parameters:
        aHttpResponse - The HTTP response. May not be null.
        aCookie - The cookie to be removed. May not be null.