Top Description Fields Constructors Methods
org.eclipse.jgit.internal.transport.http

public final Class NetscapeCookieFile

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .ByteArrayOutputStream, .File, .FileNotFoundException, .IOException, .OutputStreamWriter, .StringReader, .Writer, java.net.HttpCookie, .URL, java.nio.charset.StandardCharsets, java.nio.file.Path, java.text.MessageFormat, java.time.Instant, java.util.Arrays, .Collection, .LinkedHashSet, .Set, java.util.concurrent.TimeUnit, org.eclipse.jgit.annotations.NonNull, .Nullable, org.eclipse.jgit.internal.JGitText, org.eclipse.jgit.internal.storage.file.FileSnapshot, .LockFile, org.eclipse.jgit.lib.Constants, org.eclipse.jgit.storage.file.FileBasedConfig, org.eclipse.jgit.util.FileUtils, .IO, .RawParseUtils, org.slf4j.Logger, .LoggerFactory

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
Cookie file format, Netscape Cookie File Format, Cookie format for wget, libcurl Cookie file parsing, libcurl Cookie file writing, NetscapeCookieFileCache

Field Summary

Modifier and TypeField and Description
private static final String
private Set<HttpCookie>
private final Instant
private byte[]
private static final String
private static final String
private static final int
LOCK_ACQUIRE_MAX_RETRY_COUNT

Maximum number of retries to acquire the lock for writing to the underlying file.

private static final int
LOCK_ACQUIRE_RETRY_SLEEP

Sleep time in milliseconds between retries to acquire the lock for writing to the underlying file.

private static final Logger
private final Path
private FileSnapshot

Constructor Summary

AccessConstructor and Description
public
NetscapeCookieFile(Path
where to find the cookie file
path
)

pack-priv
NetscapeCookieFile(Path path, Instant createdAt)

Method Summary

Modifier and TypeMethod and Description
public Set<HttpCookie>

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.
getCookies
(boolean
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.
refresh
)

Return all cookies from the underlying cookie file.

private byte[]

Returns:

the file contents in case the file has been modified since the last access, otherwise null
getFileContentIfModified
()

Read the underlying file and return its content but only in case it has been modified since the last access.

public Path

Returns:

the path
getPath
()

Path to the underlying cookie file.

private static byte[]
hash(final byte[] in)

pack-priv static Set<HttpCookie>

Returns:

the merged cookies
mergeCookies
(Set<HttpCookie>
first set of cookies
cookies1
,
Set<HttpCookie>
second set of cookies
cookies2
)

Merge the given sets in the following way.

private static Set<HttpCookie>

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!
parseCookieFile
(byte[]
the file content to parse
input
,
Instant
cookie creation time; used to calculate the maxAge based on the expiration date given within the file
createdAt
)

Parses the given file and extracts all cookie information from it.

private static HttpCookie
parseLine(String line, Instant createdAt)

public void
write(URL
url for which to write the cookies (important to derive default values for non-explicitly set attributes)
url
)

Writes all the cookies being maintained in the set being returned by getCookies(boolean) to the underlying file.

pack-priv static void
write(Writer
the writer to use to persist the cookies
writer
,
Collection<HttpCookie>
the cookies to write into the file
cookies
,
URL
the url for which to write the cookie (to derive the default values for certain cookie attributes)
url
,
Instant
cookie creation time; used to calculate a cookie's expiration time
createdAt
)

Writes the given cookies to the file in the Netscape Cookie File Format (also used by curl).

private static void
writeCookie(Writer writer, HttpCookie cookie, URL url, Instant createdAt)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COLUMN_SEPARATORback to summary
private static final String COLUMN_SEPARATOR
cookiesback to summary
private Set<HttpCookie> cookies
createdAtback to summary
private final Instant createdAt
hashback to summary
private byte[] hash
HTTP_ONLY_PREAMBLEback to summary
private static final String HTTP_ONLY_PREAMBLE
LINE_SEPARATORback to summary
private static final String LINE_SEPARATOR
LOCK_ACQUIRE_MAX_RETRY_COUNTback to summary
private static final int LOCK_ACQUIRE_MAX_RETRY_COUNT

Maximum number of retries to acquire the lock for writing to the underlying file.

LOCK_ACQUIRE_RETRY_SLEEPback to summary
private static final int LOCK_ACQUIRE_RETRY_SLEEP

Sleep time in milliseconds between retries to acquire the lock for writing to the underlying file.

LOGback to summary
private static final Logger LOG
pathback to summary
private final Path path
snapshotback to summary
private FileSnapshot snapshot

Constructor Detail

NetscapeCookieFileback to summary
public NetscapeCookieFile(Path path)
Parameters
path:Path

where to find the cookie file

NetscapeCookieFileback to summary
pack-priv NetscapeCookieFile(Path path, Instant createdAt)

Method Detail

getCookiesback to summary
public Set<HttpCookie> getCookies(boolean refresh)

Return all cookies from the underlying cookie file.

Parameters
refresh:boolean

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:Set<HttpCookie>

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.

getFileContentIfModifiedback to summary
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:byte[]

the file contents in case the file has been modified since the last access, otherwise null

Exceptions
IOException:
if the file is not found or cannot be read
getPathback to summary
public Path getPath()

Path to the underlying cookie file.

Returns:Path

the path

hashback to summary
private static byte[] hash(final byte[] in)
mergeCookiesback to summary
pack-priv static Set<HttpCookie> mergeCookies(Set<HttpCookie> cookies1, 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:Set<HttpCookie>

first set of cookies

cookies2:Set<HttpCookie>

second set of cookies

Returns:Set<HttpCookie>

the merged cookies

parseCookieFileback to summary
private static Set<HttpCookie> parseCookieFile(byte[] input, Instant createdAt) throws IOException, IllegalArgumentException

Parses the given file and extracts all cookie information from it.

Parameters
input:byte[]

the file content to parse

createdAt:Instant

cookie creation time; used to calculate the maxAge based on the expiration date given within the file

Returns:Set<HttpCookie>

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!

Exceptions
IOException:
if the given file could not be read for some reason
IllegalArgumentException:
if the given file does not have a proper format
parseLineback to summary
private static HttpCookie parseLine(String line, Instant createdAt)
writeback to summary
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

url for which to write the cookies (important to derive default values for non-explicitly set attributes)

Exceptions
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
writeback to summary
pack-priv static void write(Writer writer, Collection<HttpCookie> cookies, URL url, Instant createdAt) throws IOException

Writes the given cookies to the file in the Netscape Cookie File Format (also used by curl).

Parameters
writer:Writer

the writer to use to persist the cookies

cookies:Collection<HttpCookie>

the cookies to write into the file

url:URL

the url for which to write the cookie (to derive the default values for certain cookie attributes)

createdAt:Instant

cookie creation time; used to calculate a cookie's expiration time

Exceptions
IOException:
if an I/O error occurs
writeCookieback to summary
private static void writeCookie(Writer writer, HttpCookie cookie, URL url, Instant createdAt) throws IOException