A DESCRIPTION OF THE REQUEST :
Currently java.net.CookieManager cannot be easily extended with a CookieStore other than the default one, due to a highly inflexible implementation of java.net.HttpCookie.
CookieManager uses class InMemoryCookieManager as the default CookieStore. This class is unsuitable for cookies that must be persisted across application restarts (e.g. for the case when the "expires" or "max-age" attributes are set for a cookie). For my particular case I was trying to write all cookies with an expiration date to a file. And this is exactly where HttpCookie is causing severe problems now.
A HttpCookie is not serializable, therefore I cannot use object serialization to persist its state. As a workaround I thought it would be possible to manually write the state of the object to a file as most fields have public getters/setters. This idea turned out to be a dead end, because the "whenCreated" field is private and no getter/setter is provided for the field. The "whenCreated" field is needed to calculate the expiration date for cookies that only have a relative expiration date (e.g. Set-Cookie2 uses seconds for the Max-Age attribute). Because this field is inaccessible there is effectively no way to store the state of a HttpCookie in a file now.
I thought it might be possible to subclass HttpCookie and store the "Set-Cookie(2)" string and simply parse this when the cookie is read from the file. This however was also not possible, because the class is declared final and cannot be subclassed.
Effectively the HttpCookie class is unusable by anything other than the default in-memory implementation of CookieStore.
JUSTIFICATION :
Java provides a framework for managing Cookies. The framework is useless if it does not provide enough flexibility to change the default behavior.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Storing cookies in a file rather than in memory should be a pretty common use case and it should either be possible to implement the expected behavior very easily, or better even, a FileBasedCookieStore should be provided by the framework.
ACTUAL -
The state of a HttpCookie cannot be written to a file.
CUSTOMER SUBMITTED WORKAROUND :
Provide my own implementation of HttpCookie and severely hack the existing CookieManager to get it to create custom HttpCookies instead of the default ones.
Currently java.net.CookieManager cannot be easily extended with a CookieStore other than the default one, due to a highly inflexible implementation of java.net.HttpCookie.
CookieManager uses class InMemoryCookieManager as the default CookieStore. This class is unsuitable for cookies that must be persisted across application restarts (e.g. for the case when the "expires" or "max-age" attributes are set for a cookie). For my particular case I was trying to write all cookies with an expiration date to a file. And this is exactly where HttpCookie is causing severe problems now.
A HttpCookie is not serializable, therefore I cannot use object serialization to persist its state. As a workaround I thought it would be possible to manually write the state of the object to a file as most fields have public getters/setters. This idea turned out to be a dead end, because the "whenCreated" field is private and no getter/setter is provided for the field. The "whenCreated" field is needed to calculate the expiration date for cookies that only have a relative expiration date (e.g. Set-Cookie2 uses seconds for the Max-Age attribute). Because this field is inaccessible there is effectively no way to store the state of a HttpCookie in a file now.
I thought it might be possible to subclass HttpCookie and store the "Set-Cookie(2)" string and simply parse this when the cookie is read from the file. This however was also not possible, because the class is declared final and cannot be subclassed.
Effectively the HttpCookie class is unusable by anything other than the default in-memory implementation of CookieStore.
JUSTIFICATION :
Java provides a framework for managing Cookies. The framework is useless if it does not provide enough flexibility to change the default behavior.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Storing cookies in a file rather than in memory should be a pretty common use case and it should either be possible to implement the expected behavior very easily, or better even, a FileBasedCookieStore should be provided by the framework.
ACTUAL -
The state of a HttpCookie cannot be written to a file.
CUSTOMER SUBMITTED WORKAROUND :
Provide my own implementation of HttpCookie and severely hack the existing CookieManager to get it to create custom HttpCookies instead of the default ones.
- duplicates
-
JDK-8243251 java.net.HttpCookie class: Serialization support
-
- Closed
-
- relates to
-
JDK-8243251 java.net.HttpCookie class: Serialization support
-
- Closed
-