Class CookieManager

java.lang.Object
com.alpaca.security.manager.CookieManager

public class CookieManager extends Object
Utility class for managing HTTP cookies and serializing/deserializing objects in a secure manner.

Supports operations to read, add, delete cookies, as well as to serialize Java objects into Base64-encoded JSON strings and to deserialize them back into objects. Custom Jackson deserializers are registered to handle OAuth2-specific types like AuthRequestDeserializer.

Since:
1.0
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final com.fasterxml.jackson.databind.module.SimpleModule
     
    private static final com.fasterxml.jackson.databind.ObjectMapper
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addCookie(jakarta.servlet.http.HttpServletResponse response, String name, String value, int maxAge)
    Adds a new cookie with specified attributes to the HTTP response.
    static void
    deleteCookie(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name)
    Deletes a cookie by name by setting its maximum age to zero and adding it to the response.
    static <T> T
    deserialize(jakarta.servlet.http.Cookie cookie, Class<T> t)
    Deserializes a cookie’s Base64-encoded JSON value into a Java object of the specified type.
    static Optional<jakarta.servlet.http.Cookie>
    getCookie(jakarta.servlet.http.HttpServletRequest request, String name)
    Retrieves a cookie by name from the HTTP request.
    static String
    Serializes a Java object to a standard JSON string without Base64 encoding.
    static String
    serialize(Object object)
    Serializes a Java object into a Base64-encoded JSON string suitable for cookie storage.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • objectMapper

      private static final com.fasterxml.jackson.databind.ObjectMapper objectMapper
    • module

      private static final com.fasterxml.jackson.databind.module.SimpleModule module
  • Constructor Details

    • CookieManager

      public CookieManager()
  • Method Details

    • getCookie

      public static Optional<jakarta.servlet.http.Cookie> getCookie(jakarta.servlet.http.HttpServletRequest request, String name)
      Retrieves a cookie by name from the HTTP request.
      Parameters:
      request - the incoming HttpServletRequest
      name - the name of the cookie to retrieve
      Returns:
      an Optional containing the cookie if found, otherwise empty
    • addCookie

      public static void addCookie(jakarta.servlet.http.HttpServletResponse response, String name, String value, int maxAge)
      Adds a new cookie with specified attributes to the HTTP response.
      Parameters:
      response - the HttpServletResponse to which the cookie will be added
      name - the name of the cookie
      value - the value of the cookie
      maxAge - maximum age in seconds for the cookie
    • deleteCookie

      public static void deleteCookie(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name)
      Deletes a cookie by name by setting its maximum age to zero and adding it to the response.
      Parameters:
      request - the HttpServletRequest containing existing cookies
      response - the HttpServletResponse to which the deletion command is sent
      name - the name of the cookie to delete
    • serialize

      public static String serialize(Object object)
      Serializes a Java object into a Base64-encoded JSON string suitable for cookie storage.
      Parameters:
      object - the object to serialize
      Returns:
      a Base64 URL-safe encoded JSON string
      Throws:
      RuntimeException - if serialization fails
    • deserialize

      public static <T> T deserialize(jakarta.servlet.http.Cookie cookie, Class<T> t)
      Deserializes a cookie’s Base64-encoded JSON value into a Java object of the specified type.
      Type Parameters:
      T - the generic type parameter
      Parameters:
      cookie - the cookie containing the encoded value
      t - the target type to deserialize into
      Returns:
      the deserialized object
      Throws:
      RuntimeException - if deserialization fails
    • justSerialize

      public static String justSerialize(Object object)
      Serializes a Java object to a standard JSON string without Base64 encoding.
      Parameters:
      object - the object to serialize
      Returns:
      the JSON string
      Throws:
      RuntimeException - if serialization fails