public final class NetscapeCookieFile
extends java.lang.Object
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!
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
COLUMN_SEPARATOR |
private java.util.Set<java.net.HttpCookie> |
cookies |
(package private) java.util.Date |
creationDate |
private byte[] |
hash |
private static java.lang.String |
HTTP_ONLY_PREAMBLE |
private static java.lang.String |
LINE_SEPARATOR |
private static int |
LOCK_ACQUIRE_MAX_RETRY_COUNT
Maximum number of retries to acquire the lock for writing to the
underlying file.
|
private static int |
LOCK_ACQUIRE_RETRY_SLEEP
Sleep time in milliseconds between retries to acquire the lock for
writing to the underlying file.
|
private static org.slf4j.Logger |
LOG |
private java.nio.file.Path |
path |
private FileSnapshot |
snapshot |
Constructor and Description |
---|
NetscapeCookieFile(java.nio.file.Path path) |
NetscapeCookieFile(java.nio.file.Path path,
java.util.Date creationDate) |
Modifier and Type | Method and 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) |
void |
write(java.net.URL url)
Writes all the cookies being maintained in the set being returned by
getCookies(boolean) to the underlying file. |
(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).
|
private static void |
writeCookie(java.io.Writer writer,
java.net.HttpCookie cookie,
java.net.URL url,
java.util.Date creationDate) |
private static final java.lang.String HTTP_ONLY_PREAMBLE
private static final java.lang.String COLUMN_SEPARATOR
private static final java.lang.String LINE_SEPARATOR
private static final int LOCK_ACQUIRE_MAX_RETRY_COUNT
private static final int LOCK_ACQUIRE_RETRY_SLEEP
private final java.nio.file.Path path
private FileSnapshot snapshot
private byte[] hash
final java.util.Date creationDate
private java.util.Set<java.net.HttpCookie> cookies
private static final org.slf4j.Logger LOG
public NetscapeCookieFile(java.nio.file.Path path)
path
- where to find the cookie fileNetscapeCookieFile(java.nio.file.Path path, java.util.Date creationDate)
public java.nio.file.Path getPath()
public java.util.Set<java.net.HttpCookie> getCookies(boolean refresh)
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.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.private static java.util.Set<java.net.HttpCookie> parseCookieFile(@NonNull byte[] input, @NonNull java.util.Date creationDate) throws java.io.IOException, java.lang.IllegalArgumentException
input
- the file content to parsecreationDate
- the date for the creation of the cookies (used to calculate
the maxAge based on the expiration date given within the file)java.io.IOException
- if the given file could not be read for some reasonjava.lang.IllegalArgumentException
- if the given file does not have a proper formatprivate static java.net.HttpCookie parseLine(@NonNull java.lang.String line, @NonNull java.util.Date creationDate)
private byte[] getFileContentIfModified() throws java.io.IOException
Internally calculates the hash and maintains FileSnapshot
s to
prevent issues described as "Racy
Git problem". Inspired by FileBasedConfig.load()
.
null
java.io.IOException
- if the file is not found or cannot be readprivate static byte[] hash(byte[] in)
public void write(java.net.URL url) throws java.io.IOException, java.lang.InterruptedException
getCookies(boolean)
to the underlying file.
Session-cookies will not be persisted.
url
- url for which to write the cookies (important to derive
default values for non-explicitly set attributes)java.io.IOException
- if the underlying cookie file could not be read or written or
a problem with the lock filejava.lang.InterruptedException
- if the thread is interrupted while waiting for the lockstatic 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
writer
- the writer to use to persist the cookiescookies
- the cookies to write into the fileurl
- 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)java.io.IOException
- if an I/O error occursprivate 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
java.io.IOException
static java.util.Set<java.net.HttpCookie> mergeCookies(java.util.Set<java.net.HttpCookie> cookies1, @Nullable java.util.Set<java.net.HttpCookie> cookies2)
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.cookies1
- first set of cookiescookies2
- second set of cookies