Class NetscapeCookieFile
java.lang.Object
org.eclipse.jgit.internal.transport.http.NetscapeCookieFile
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!
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private Set<HttpCookie>
private final Instant
private byte[]
private static final String
private static final String
private static final int
Maximum number of retries to acquire the lock for writing to the underlying file.private static final int
Sleep time in milliseconds between retries to acquire the lock for writing to the underlying file.private static final org.slf4j.Logger
private final Path
private FileSnapshot
-
Constructor Summary
ConstructorsConstructorDescriptionNetscapeCookieFile
(Path path) NetscapeCookieFile
(Path path, Instant createdAt) -
Method Summary
Modifier and TypeMethodDescriptiongetCookies
(boolean refresh) Return all cookies from the underlying cookie file.private byte[]
Read the underlying file and return its content but only in case it has been modified since the last access.getPath()
Path to the underlying cookie file.private static byte[]
hash
(byte[] in) (package private) static Set<HttpCookie>
mergeCookies
(Set<HttpCookie> cookies1, Set<HttpCookie> cookies2) Merge the given sets in the following way.private static Set<HttpCookie>
parseCookieFile
(byte[] input, Instant createdAt) Parses the given file and extracts all cookie information from it.private static HttpCookie
(package private) static void
write
(Writer writer, Collection<HttpCookie> cookies, URL url, Instant createdAt) Writes the given cookies to the file in the Netscape Cookie File Format (also used by curl).void
Writes all the cookies being maintained in the set being returned bygetCookies(boolean)
to the underlying file.private static void
writeCookie
(Writer writer, HttpCookie cookie, URL url, Instant createdAt)
-
Field Details
-
HTTP_ONLY_PREAMBLE
- See Also:
-
COLUMN_SEPARATOR
- See Also:
-
LINE_SEPARATOR
- See Also:
-
LOCK_ACQUIRE_MAX_RETRY_COUNT
private static final int LOCK_ACQUIRE_MAX_RETRY_COUNTMaximum 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_SLEEPSleep time in milliseconds between retries to acquire the lock for writing to the underlying file.- See Also:
-
path
-
snapshot
-
hash
private byte[] hash -
createdAt
-
cookies
-
LOG
private static final org.slf4j.Logger LOG
-
-
Constructor Details
-
NetscapeCookieFile
- Parameters:
path
- where to find the cookie file
-
NetscapeCookieFile
-
-
Method Details
-
getPath
Path to the underlying cookie file.- Returns:
- the path
-
getCookies
Return all cookies from the underlying cookie file.- Parameters:
refresh
- iftrue
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 parsecreatedAt
- 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 reasonIllegalArgumentException
- if the given file does not have a proper format
-
parseLine
-
getFileContentIfModified
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
FileSnapshot
s to prevent issues described as "Racy Git problem". Inspired byFileBasedConfig.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
Writes all the cookies being maintained in the set being returned bygetCookies(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 fileInterruptedException
- 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 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)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
Merge the given sets in the following way. All cookies fromcookies1
andcookies2
are contained in the resulting set which have unique names. If there is a duplicate entry for one name only the entry from setcookies1
ends up in the resulting set.- Parameters:
cookies1
- first set of cookiescookies2
- second set of cookies- Returns:
- the merged cookies
-