Class NetscapeCookieFile

    • Constructor Summary

      Constructors 
      Constructor Description
      NetscapeCookieFile​(java.nio.file.Path path)  
      NetscapeCookieFile​(java.nio.file.Path path, java.util.Date creationDate)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Set<java.net.HttpCookie> getCookies​(boolean refresh)
      Return all cookies from the underlying cookie file.
      private byte[] getFileContentIfModified()
      Read the underying file and return its content but only in case it has been modified since the last access.
      java.nio.file.Path getPath()
      Path to the underlying cookie file.
      private static byte[] hash​(byte[] in)  
      (package private) static java.util.Set<java.net.HttpCookie> mergeCookies​(java.util.Set<java.net.HttpCookie> cookies1, java.util.Set<java.net.HttpCookie> cookies2)
      Merge the given sets in the following way.
      private static java.util.Set<java.net.HttpCookie> parseCookieFile​(byte[] input, java.util.Date creationDate)
      Parses the given file and extracts all cookie information from it.
      private static java.net.HttpCookie parseLine​(java.lang.String line, java.util.Date creationDate)  
      (package private) static void write​(java.io.Writer writer, java.util.Collection<java.net.HttpCookie> cookies, java.net.URL url, java.util.Date creationDate)
      Writes the given cookies to the file in the Netscape Cookie File Format (also used by curl).
      void write​(java.net.URL url)
      Writes all the cookies being maintained in the set being returned by getCookies(boolean) to the underlying file.
      private static void writeCookie​(java.io.Writer writer, java.net.HttpCookie cookie, java.net.URL url, java.util.Date creationDate)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HTTP_ONLY_PREAMBLE

        private static final java.lang.String HTTP_ONLY_PREAMBLE
        See Also:
        Constant Field Values
      • COLUMN_SEPARATOR

        private static final java.lang.String COLUMN_SEPARATOR
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
      • 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:
        Constant Field Values
      • path

        private final java.nio.file.Path path
      • hash

        private byte[] hash
      • creationDate

        final java.util.Date creationDate
      • cookies

        private java.util.Set<java.net.HttpCookie> cookies
      • LOG

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

      • NetscapeCookieFile

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

        NetscapeCookieFile​(java.nio.file.Path path,
                           java.util.Date creationDate)
    • Method Detail

      • getPath

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

        public java.util.Set<java.net.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 java.util.Set<java.net.HttpCookie> parseCookieFile​(@NonNull
                                                                          byte[] input,
                                                                          @NonNull
                                                                          java.util.Date creationDate)
                                                                   throws java.io.IOException,
                                                                          java.lang.IllegalArgumentException
        Parses the given file and extracts all cookie information from it.
        Parameters:
        input - the file content to parse
        creationDate - the date for the creation of the cookies (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:
        java.io.IOException - if the given file could not be read for some reason
        java.lang.IllegalArgumentException - if the given file does not have a proper format
      • parseLine

        private static java.net.HttpCookie parseLine​(@NonNull
                                                     java.lang.String line,
                                                     @NonNull
                                                     java.util.Date creationDate)
      • getFileContentIfModified

        private byte[] getFileContentIfModified()
                                         throws java.io.IOException
        Read the underying 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:
        java.io.IOException - if the file is not found or cannot be read
      • hash

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

        public void write​(java.net.URL url)
                   throws java.io.IOException,
                          java.lang.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:
        java.io.IOException - if the underlying cookie file could not be read or written or a problem with the lock file
        java.lang.InterruptedException - if the thread is interrupted while waiting for the lock
      • write

        static void write​(@NonNull
                          java.io.Writer writer,
                          @NonNull
                          java.util.Collection<java.net.HttpCookie> cookies,
                          @NonNull
                          java.net.URL url,
                          @NonNull
                          java.util.Date creationDate)
                   throws java.io.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)
        creationDate - the date when the cookie has been created. Important for calculation the cookie expiration time (calculated from cookie's maxAge and this creation time)
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeCookie

        private static void writeCookie​(@NonNull
                                        java.io.Writer writer,
                                        @NonNull
                                        java.net.HttpCookie cookie,
                                        @NonNull
                                        java.net.URL url,
                                        @NonNull
                                        java.util.Date creationDate)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • mergeCookies

        static java.util.Set<java.net.HttpCookie> mergeCookies​(java.util.Set<java.net.HttpCookie> cookies1,
                                                               @Nullable
                                                               java.util.Set<java.net.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