Top Description Inners Fields Constructors Methods
java.net

public Class CookieManager

extends CookieHandler
Class Inheritance
Imports
java.util.Map, .List, .Comparator, java.io.IOException, sun.util.logging.PlatformLogger

CookieManager provides a concrete implementation of CookieHandler, which separates the storage of cookies from the policy surrounding accepting and rejecting cookies. A CookieManager is initialized with a CookieStore which manages storage, and a CookiePolicy object, which makes policy decisions on cookie acceptance/rejection.

The HTTP cookie management in java.net package looks like:

use
CookieHandler <------- HttpURLConnection
      ^
      | impl
      |         use
CookieManager -------> CookiePolicy
            |   use
            |--------> HttpCookie
            |              ^
            |              | use
            |   use        |
            |--------> CookieStore
                           ^
                           | impl
                           |
                 Internal in-memory implementation

There're various ways user can hook up his own HTTP cookie management behavior, e.g.

The implementation conforms to RFC 2965, section 3.3.

Author
Edward Wang
Since
1.6
External Specification
https://www.rfc-editor.org/info/rfc2965
See Also
CookiePolicy

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private CookieStore
private CookiePolicy

Constructor Summary

AccessConstructor and Description
public
CookieManager()

Create a new cookie manager.

public
CookieManager(CookieStore
a CookieStore to be used by cookie manager. if null, cookie manager will use a default one, which is an in-memory CookieStore implementation.
store
,
CookiePolicy
a CookiePolicy instance to be used by cookie manager as policy callback. if null, ACCEPT_ORIGINAL_SERVER will be used.
cookiePolicy
)

Create a new cookie manager with specified cookie store and cookie policy.

Method Summary

Modifier and TypeMethod and Description
public Map<String, List<String>>
get(URI
a URI representing the intended use for the cookies
uri
,
Map<String, List<String>>
a Map from request header field names to lists of field values representing the current request headers
requestHeaders
)

Implements abstract java.net.CookieHandler.get.

Gets all the applicable cookies from a cookie cache for the specified uri in the request header.

public CookieStore

Returns:

the cookie store currently used by cookie manager.
getCookieStore
()

To retrieve current cookie store.

private static boolean
isInPortList(String lst, int port)

private boolean
pathMatches(String path, String pathToMatchWith)

public void
put(URI
a URI where the cookies come from
uri
,
Map<String, List<String>>
an immutable map from field names to lists of field values representing the response header fields returned
responseHeaders
)

Implements abstract java.net.CookieHandler.put.

Sets all the applicable cookies, examples are response header fields that are named Set-Cookie2, present in the response headers into a cookie cache.

public void
setCookiePolicy(CookiePolicy
the cookie policy. Can be null, which has no effects on current cookie policy.
cookiePolicy
)

To set the cookie policy of this cookie manager.

private boolean
pack-priv static List<String>
Inherited from java.net.CookieHandler:
getDefaultsetDefault

Field Detail

cookieJarback to summary
private CookieStore cookieJar
policyCallbackback to summary
private CookiePolicy policyCallback

Constructor Detail

CookieManagerback to summary
public CookieManager()

Create a new cookie manager.

This constructor will create new cookie manager with default cookie store and accept policy. The effect is same as CookieManager(null, null).

CookieManagerback to summary
public CookieManager(CookieStore store, CookiePolicy cookiePolicy)

Create a new cookie manager with specified cookie store and cookie policy.

Parameters
store:CookieStore

a CookieStore to be used by cookie manager. if null, cookie manager will use a default one, which is an in-memory CookieStore implementation.

cookiePolicy:CookiePolicy

a CookiePolicy instance to be used by cookie manager as policy callback. if null, ACCEPT_ORIGINAL_SERVER will be used.

Method Detail

getback to summary
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException

Implements abstract java.net.CookieHandler.get.

Doc from java.net.CookieHandler.get.

Gets all the applicable cookies from a cookie cache for the specified uri in the request header.

The URI passed as an argument specifies the intended use for the cookies. In particular the scheme should reflect whether the cookies will be sent over http, https or used in another context like javascript. The host part should reflect either the destination of the cookies or their origin in the case of javascript.

It is up to the implementation to take into account the URI and the cookies attributes and security settings to determine which ones should be returned.

HTTP protocol implementers should make sure that this method is called after all request headers related to choosing cookies are added, and before the request is sent.

Parameters
uri:URI

a URI representing the intended use for the cookies

requestHeaders:Map<String, List<String>>

a Map from request header field names to lists of field values representing the current request headers

Returns:Map<String, List<String>>

an immutable map from state management headers, with field names "Cookie" or "Cookie2" to a list of cookies containing state information

Exceptions
IOException:
if an I/O error occurs
getCookieStoreback to summary
public CookieStore getCookieStore()

To retrieve current cookie store.

Returns:CookieStore

the cookie store currently used by cookie manager.

isInPortListback to summary
private static boolean isInPortList(String lst, int port)
pathMatchesback to summary
private boolean pathMatches(String path, String pathToMatchWith)
putback to summary
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException

Implements abstract java.net.CookieHandler.put.

Doc from java.net.CookieHandler.put.

Sets all the applicable cookies, examples are response header fields that are named Set-Cookie2, present in the response headers into a cookie cache.

Parameters
uri:URI

a URI where the cookies come from

responseHeaders:Map<String, List<String>>

an immutable map from field names to lists of field values representing the response header fields returned

Exceptions
IOException:
if an I/O error occurs
setCookiePolicyback to summary
public void setCookiePolicy(CookiePolicy cookiePolicy)

To set the cookie policy of this cookie manager.

An instance of CookieManager will have cookie policy ACCEPT_ORIGINAL_SERVER by default. Users always can call this method to set another cookie policy.

Parameters
cookiePolicy:CookiePolicy

the cookie policy. Can be null, which has no effects on current cookie policy.

shouldAcceptInternalback to summary
private boolean shouldAcceptInternal(URI uri, HttpCookie cookie)
sortByPathAndAgeback to summary
pack-priv static List<String> sortByPathAndAge(List<HttpCookie> cookies)
java.net back to summary

pack-priv Class CookieManager.CookieComparator

extends Object
implements Comparator<HttpCookie>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
compare(HttpCookie
the first object to be compared.
c1
,
HttpCookie
the second object to be compared.
c2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CookieComparatorback to summary
pack-priv CookieComparator()

Method Detail

compareback to summary
public int compare(HttpCookie c1, HttpCookie c2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
c1:HttpCookie

the first object to be compared.

c2:HttpCookie

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.