Class CookieJar

All Implemented Interfaces:
SessionFeature, Proxy
Direct Known Subclasses:
CookieJarDB, CookieJarText

@Generated("org.javagi.JavaGI") public class CookieJar extends GObject implements SessionFeature

Automatic cookie handling for SoupSession.

A CookieJar stores Cookies and arrange for them to be sent with the appropriate Messages. CookieJar implements SessionFeature, so you can add a cookie jar to a session with Session.addFeature(SessionFeature) or Session.addFeatureByType(Type).

Note that the base CookieJar class does not support any form of long-term cookie persistence.

  • Constructor Details

    • CookieJar

      public CookieJar(MemorySegment address)
      Create a CookieJar instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • CookieJar

      public CookieJar()
      Create a new CookieJar.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the CookieJar class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected CookieJar asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • addCookie

      public void addCookie(Cookie cookie)

      Adds cookie to jar.

      Emits the CookieJar::changed signal if we are modifying an existing cookie or adding a valid new cookie ('valid' means that the cookie's expire date is not in the past).

      cookie will be 'stolen' by the jar, so don't free it afterwards.

      Parameters:
      cookie - a SoupCookie
    • addCookieFull

      public void addCookieFull(Cookie cookie, @Nullable Uri uri, @Nullable Uri firstParty)

      Adds cookie to jar.

      Emits the CookieJar::changed signal if we are modifying an existing cookie or adding a valid new cookie ('valid' means that the cookie's expire date is not in the past).

      firstParty will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

      uri will be used to reject setting or overwriting secure cookies from insecure origins. null is treated as secure.

      cookie will be 'stolen' by the jar, so don't free it afterwards.

      Parameters:
      cookie - a SoupCookie
      uri - the URI setting the cookie
      firstParty - the URI for the main document
    • addCookieWithFirstParty

      public void addCookieWithFirstParty(Uri firstParty, Cookie cookie)

      Adds cookie to jar.

      Emits the CookieJar::changed signal if we are modifying an existing cookie or adding a valid new cookie ('valid' means that the cookie's expire date is not in the past).

      firstParty will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

      cookie will be 'stolen' by the jar, so don't free it afterwards.

      For secure cookies to work properly you may want to use addCookieFull(Cookie, Uri, Uri).

      Parameters:
      firstParty - the URI for the main document
      cookie - a SoupCookie
    • allCookies

      public SList<Cookie> allCookies()

      Constructs a GLib.List with every cookie inside the jar.

      The cookies in the list are a copy of the original, so you have to free them when you are done with them.

      For historical reasons this list is in reverse order.

      Returns:
      a GSList with all the cookies in the jar.
    • deleteCookie

      public void deleteCookie(Cookie cookie)

      Deletes cookie from jar.

      Emits the CookieJar::changed signal.

      Parameters:
      cookie - a SoupCookie
    • getAcceptPolicy

      public CookieJarAcceptPolicy getAcceptPolicy()
      Gets jar's CookieJarAcceptPolicy.
      Returns:
      the SoupCookieJarAcceptPolicy set in the this CookieJar
    • getCookieList

      public SList<Cookie> getCookieList(Uri uri, boolean forHttp)

      Retrieves the list of cookies that would be sent with a request to uri as a GLib.List of Cookie objects.

      If forHttp is true, the return value will include cookies marked "HttpOnly" (that is, cookies that the server wishes to keep hidden from client-side scripting operations such as the JavaScript document.cookies property). Since CookieJar sets the Cookie header itself when making the actual HTTP request, you should almost certainly be setting forHttp to false if you are calling this.

      Parameters:
      uri - a GUri
      forHttp - whether or not the return value is being passed directly to an HTTP operation
      Returns:
      a GSList with the cookies in the this CookieJar that would be sent with a request to uri.
    • getCookieListWithSameSiteInfo

      public SList<Cookie> getCookieListWithSameSiteInfo(Uri uri, @Nullable Uri topLevel, @Nullable Uri siteForCookies, boolean forHttp, boolean isSafeMethod, boolean isTopLevelNavigation)

      This is an extended version of getCookieList(Uri, boolean) that provides more information required to use SameSite cookies.

      See the SameSite cookies spec for more detailed information.

      Parameters:
      uri - a GUri
      topLevel - a GUri for the top level document
      siteForCookies - a GUri indicating the origin to get cookies for
      forHttp - whether or not the return value is being passed directly to an HTTP operation
      isSafeMethod - if the HTTP method is safe, as defined by RFC 7231, ignored when forHttp is false
      isTopLevelNavigation - whether or not the HTTP request is part of top level navigation
      Returns:
      a GSList with the cookies in the this CookieJar that would be sent with a request to uri.
    • getCookies

      public @Nullable String getCookies(Uri uri, boolean forHttp)

      Retrieves (in Cookie-header form) the list of cookies that would be sent with a request to uri.

      If forHttp is true, the return value will include cookies marked "HttpOnly" (that is, cookies that the server wishes to keep hidden from client-side scripting operations such as the JavaScript document.cookies property). Since CookieJar sets the Cookie header itself when making the actual HTTP request, you should almost certainly be setting forHttp to false if you are calling this.

      Parameters:
      uri - a GUri
      forHttp - whether or not the return value is being passed directly to an HTTP operation
      Returns:
      the cookies, in string form, or null if there are no cookies for uri.
    • isPersistent

      public boolean isPersistent()
      Gets whether this CookieJar stores cookies persistenly.
      Returns:
      true if this CookieJar storage is persistent or false otherwise.
    • setAcceptPolicy

      public void setAcceptPolicy(CookieJarAcceptPolicy policy)
      Sets policy as the cookie acceptance policy for jar.
      Parameters:
      policy - a SoupCookieJarAcceptPolicy
    • setCookie

      public void setCookie(Uri uri, String cookie)

      Adds cookie to jar, exactly as though it had appeared in a Set-Cookie header returned from a request to uri.

      Keep in mind that if the CookieJarAcceptPolicy set is either CookieJarAcceptPolicy.NO_THIRD_PARTY or CookieJarAcceptPolicy.GRANDFATHERED_THIRD_PARTY you'll need to use setCookieWithFirstParty(Uri, Uri, String), otherwise the jar will have no way of knowing if the cookie is being set by a third party or not.

      Parameters:
      uri - the URI setting the cookie
      cookie - the stringified cookie to set
    • setCookieWithFirstParty

      public void setCookieWithFirstParty(Uri uri, Uri firstParty, String cookie)

      Adds cookie to jar, exactly as though it had appeared in a Set-Cookie header returned from a request to uri.

      firstParty will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

      Parameters:
      uri - the URI setting the cookie
      firstParty - the URI for the main document
      cookie - the stringified cookie to set
    • changed

      protected void changed(Cookie oldCookie, Cookie newCookie)
    • save

      protected void save()
    • onChanged

      Emitted when jar changes.

      If a cookie has been added, newCookie will contain the newly-added cookie and oldCookie will be null. If a cookie has been deleted, oldCookie will contain the to-be-deleted cookie and newCookie will be null. If a cookie has been changed, oldCookie will contain its old value, and newCookie its new value.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitChanged

      public void emitChanged(@Nullable Cookie oldCookie, @Nullable Cookie newCookie)
      Emits the "changed" signal. See onChanged(CookieJar.ChangedCallback).
    • builder

      public static CookieJar.Builder<? extends CookieJar.Builder> builder()
      A CookieJar.Builder object constructs a CookieJar with the specified properties. Use the various set...() methods to set properties, and finish construction with CookieJar.Builder.build().
      Returns:
      the builder object