java.lang.Object
de.craftsblock.craftsnet.api.http.cookies.Cookie

public class Cookie extends Object
Represents a http cookie with various attributes such as name, value, path, domain, expiry date, same-site policy, security, and HttpOnly flag.
Since:
3.0.6
  • Constructor Details

    • Cookie

      public Cookie(@NotNull @NotNull String name)
      Constructs a new Cookie with the specified name and no value.
      Parameters:
      name - The name of the cookie, cannot be null
    • Cookie

      public Cookie(@NotNull @NotNull String name, @Nullable @Nullable Object value)
      Constructs a new Cookie with the specified name and value.
      Parameters:
      name - The name of the cookie, cannot be null
      value - The value of the cookie, can be null
  • Method Details

    • setFlag

      public Cookie setFlag(@NotNull @NotNull String flag, @Nullable @Nullable String arg)
      Sets a specified attribute of the cookie based on the flag provided.

      This method allows setting various cookie attributes using a flag and its corresponding argument. Recognized flags are "Path", "Domain", "Expires", "SameSite", "Secure", and "HttpOnly".

      Parameters:
      flag - The attribute to be set, cannot be null
      arg - The value for the attribute, can be null for attributes that accept null values
      Returns:
      The current Cookie object, for method chaining
    • getName

      @NotNull public @NotNull String getName()
      Returns the name of the cookie.
      Returns:
      The name of the cookie
    • getValue

      @Nullable public <T> T getValue()
      Retrieves the value of the cookie, cast to the specified type.

      This method returns the value of the cookie if it is set, casting it to the type specified by the caller. If the value is null, it returns null. The caller must ensure that the type cast is correct.

      Type Parameters:
      T - The type to which the cookie value should be cast
      Returns:
      The value of the cookie cast to the specified type, or null if the value is not set
      Throws:
      ClassCastException - If the value cannot be cast to the specified type
    • setValue

      public Cookie setValue(@Nullable @Nullable Object value)
      Sets the value of the cookie.
      Parameters:
      value - The new value of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • getPath

      public String getPath()
      Returns the path of the cookie.
      Returns:
      The path of the cookie, or null if not set
    • setPath

      public Cookie setPath(@Nullable @Nullable String path)
      Sets the path of the cookie.
      Parameters:
      path - The new path of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • getDomain

      public String getDomain()
      Returns the domain of the cookie.
      Returns:
      The domain of the cookie, or null if not set
    • setDomain

      public Cookie setDomain(@Nullable @Nullable String domain)
      Sets the domain of the cookie.
      Parameters:
      domain - The new domain of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • getExpiresAt

      public OffsetDateTime getExpiresAt()
      Returns the expiry date of the cookie.
      Returns:
      The expiry date of the cookie, or null if not set
    • setExpiresAt

      public Cookie setExpiresAt(@Nullable @Nullable String expiresAt)
      Sets the expiry date of the cookie using a string representation.
      Parameters:
      expiresAt - The new expiry date of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • setExpiresAt

      public Cookie setExpiresAt(@Nullable @Nullable OffsetDateTime expiresAt)
      Sets the expiry date of the cookie using a TemporalAccessor.
      Parameters:
      expiresAt - The new expiry date of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • getMaxAge

      public long getMaxAge()
      Returns the maximum age of the cookie.
      Returns:
      The maximum age of the cookie, if disabled -1 will be returned.
    • setMaxAge

      public Cookie setMaxAge(long maxAge)
      Sets the maximum age of this cookie. Set to -2 to disable this flag.
      Parameters:
      maxAge - The number in seconds the cookie is valid.
      Returns:
      The current Cookie object, for method chaining
    • getSameSite

      public SameSite getSameSite()
      Returns the SameSite policy of the cookie.
      Returns:
      The SameSite policy of the cookie, or null if not set
    • setSameSite

      public Cookie setSameSite(@Nullable @Nullable String sameSite)
      Sets the SameSite policy of the cookie using a string representation.
      Parameters:
      sameSite - The new SameSite policy of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • setSameSite

      public Cookie setSameSite(@Nullable @Nullable SameSite sameSite)
      Sets the SameSite policy of the cookie.
      Parameters:
      sameSite - The new SameSite policy of the cookie, can be null
      Returns:
      The current Cookie object, for method chaining
    • isSecure

      public boolean isSecure()
      Returns whether the cookie is secure.
      Returns:
      true if the cookie is secure, false otherwise
    • setSecure

      public Cookie setSecure(boolean secure)
      Sets whether the cookie is secure.
      Parameters:
      secure - true to make the cookie secure, false otherwise
      Returns:
      The current Cookie object, for method chaining
    • isHttpOnly

      public boolean isHttpOnly()
      Returns whether the cookie is HttpOnly.
      Returns:
      true if the cookie is HttpOnly, false otherwise
    • setHttpOnly

      public Cookie setHttpOnly(boolean httpOnly)
      Sets whether the cookie is HttpOnly.
      Parameters:
      httpOnly - true to make the cookie HttpOnly, false otherwise
      Returns:
      The current Cookie object, for method chaining
    • override

      public Cookie override(Cookie cookie)
      Overrides the current cookie's attributes with those of another cookie.
      Parameters:
      cookie - The cookie to copy attributes from
      Returns:
      The current Cookie object, for method chaining
    • markDeleted

      public Cookie markDeleted()
      Marks the cookie as deleted by setting its expiry date to a date in the past.
      Returns:
      The current Cookie object, for method chaining
    • toString

      public String toString()
      Returns a string representation of the cookie.
      Overrides:
      toString in class Object
      Returns:
      The string representation of the cookie
    • equals

      public boolean equals(Object o)
      Compares this cookie to the specified object for equality.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare this cookie against
      Returns:
      true if the specified object is equal to this cookie, false otherwise
    • hashCode

      public int hashCode()
      Returns the hash code value for this cookie.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code value for this cookie