Class NetscapeCookieFile

java.lang.Object
org.eclipse.jgit.internal.transport.http.NetscapeCookieFile

public final class NetscapeCookieFile extends Object
Wraps all cookies persisted in a Netscape Cookie File Format being referenced via the git config http.cookieFile.

It will only load the cookies lazily, i.e. before calling getCookies(boolean) the file is not evaluated. This class also allows persisting cookies in that file format.

In general this class is not thread-safe. So any consumer needs to take care of synchronization!

See Also:
  • Field Details

    • HTTP_ONLY_PREAMBLE

      private static final String HTTP_ONLY_PREAMBLE
      See Also:
    • COLUMN_SEPARATOR

      private static final String COLUMN_SEPARATOR
      See Also:
    • LINE_SEPARATOR

      private static final String LINE_SEPARATOR
      See Also:
    • LOCK_ACQUIRE_MAX_RETRY_COUNT

      private static final int LOCK_ACQUIRE_MAX_RETRY_COUNT
      Maximum number of retries to acquire the lock for writing to the underlying file.
      See Also:
    • LOCK_ACQUIRE_RETRY_SLEEP

      private static final int LOCK_ACQUIRE_RETRY_SLEEP
      Sleep time in milliseconds between retries to acquire the lock for writing to the underlying file.
      See Also:
    • path

      private final Path path
    • snapshot

      private FileSnapshot snapshot
    • hash

      private byte[] hash
    • createdAt

      private final Instant createdAt
    • cookies

      private Set<HttpCookie> cookies
    • LOG

      private static final org.slf4j.Logger LOG
  • Constructor Details

    • NetscapeCookieFile

      public NetscapeCookieFile(Path path)
      Parameters:
      path - where to find the cookie file
    • NetscapeCookieFile

      NetscapeCookieFile(Path path, Instant createdAt)
  • Method Details

    • getPath

      public Path getPath()
      Path to the underlying cookie file.
      Returns:
      the path
    • getCookies

      public Set<HttpCookie> getCookies(boolean refresh)
      Return all cookies from the underlying cookie file.
      Parameters:
      refresh - if true updates the list from the underlying cookie file if it has been modified since the last read otherwise returns the current transient state. In case the cookie file has never been read before will always read from the underlying file disregarding the value of this parameter.
      Returns:
      all cookies (may contain session cookies as well). This does not return a copy of the list but rather the original one. Every addition to the returned list can afterwards be persisted via write(URL). Errors in the underlying file will not lead to exceptions but rather to an empty set being returned and the underlying error being logged.
    • parseCookieFile

      private static Set<HttpCookie> parseCookieFile(@NonNull byte[] input, @NonNull Instant createdAt) throws IOException, IllegalArgumentException
      Parses the given file and extracts all cookie information from it.
      Parameters:
      input - the file content to parse
      createdAt - cookie creation time; used to calculate the maxAge based on the expiration date given within the file
      Returns:
      the set of parsed cookies from the given file (even expired ones). If there is more than one cookie with the same name in this file the last one overwrites the first one!
      Throws:
      IOException - if the given file could not be read for some reason
      IllegalArgumentException - if the given file does not have a proper format
    • parseLine

      private static HttpCookie parseLine(@NonNull String line, @NonNull Instant createdAt)
    • getFileContentIfModified

      private byte[] getFileContentIfModified() throws IOException
      Read the underlying file and return its content but only in case it has been modified since the last access.

      Internally calculates the hash and maintains FileSnapshots to prevent issues described as "Racy Git problem". Inspired by FileBasedConfig.load().

      Returns:
      the file contents in case the file has been modified since the last access, otherwise null
      Throws:
      IOException - if the file is not found or cannot be read
    • hash

      private static byte[] hash(byte[] in)
    • write

      public void write(URL url) throws IOException, InterruptedException
      Writes all the cookies being maintained in the set being returned by getCookies(boolean) to the underlying file.

      Session-cookies will not be persisted.

      Parameters:
      url - url for which to write the cookies (important to derive default values for non-explicitly set attributes)
      Throws:
      IOException - if the underlying cookie file could not be read or written or a problem with the lock file
      InterruptedException - if the thread is interrupted while waiting for the lock
    • write

      static void write(@NonNull Writer writer, @NonNull Collection<HttpCookie> cookies, @NonNull URL url, @NonNull Instant createdAt) throws IOException
      Writes the given cookies to the file in the Netscape Cookie File Format (also used by curl).
      Parameters:
      writer - the writer to use to persist the cookies
      cookies - the cookies to write into the file
      url - the url for which to write the cookie (to derive the default values for certain cookie attributes)
      createdAt - cookie creation time; used to calculate a cookie's expiration time
      Throws:
      IOException - if an I/O error occurs
    • writeCookie

      private static void writeCookie(@NonNull Writer writer, @NonNull HttpCookie cookie, @NonNull URL url, @NonNull Instant createdAt) throws IOException
      Throws:
      IOException
    • mergeCookies

      static Set<HttpCookie> mergeCookies(Set<HttpCookie> cookies1, @Nullable Set<HttpCookie> cookies2)
      Merge the given sets in the following way. All cookies from cookies1 and cookies2 are contained in the resulting set which have unique names. If there is a duplicate entry for one name only the entry from set cookies1 ends up in the resulting set.
      Parameters:
      cookies1 - first set of cookies
      cookies2 - second set of cookies
      Returns:
      the merged cookies