public final class Session
extends java.lang.Object
Session
is used to manage cookies and user credentials across
multiple AbstractRestOperation
s. Only one shared Session
exists at the same time and will be used for all
AbstractRestOperation
s.
Use the shared Session
to add Credentials
to be
automatically applied to AbstractRestOperation
if the security
restrictions set for the Credentials
apply.
The Session
also internally manages Cookies sent by the HTTP
backend services and automatically adds valid Cookies to
AbstractRestOperation
if domain, path and security settings of the
Cookie match. The internal management of Cookie
s can be controlled
using policies by using
To get (or create a new) Session
use:
Session.get();To delete the shared
Session
use:
Session.destroy();Destroying the
Session
removes all managed Cookie
and
Credentials
objects and will recreate the Session
with
the next call.
This class is not meant to be extended.Modifier and Type | Field and Description |
---|---|
static int |
COOKIE_ACCEPT_ALL
Accept all Cookies sent from HTTP backend services.
|
static int |
COOKIE_ACCEPT_NONE
Accept no Cookies sent from HTTP backend services at all.
|
static int |
COOKIE_ACCEPT_SERVER
Accept Cookies only if
Cookie.getDomain() matches the URL it is
coming from (default) |
static int |
COOKIE_MULTIVALUE_ADD
Add Cookie values for existing names
|
static int |
COOKIE_MULTIVALUE_MAX
The max number of values store for one Cookie name
|
static int |
COOKIE_MULTIVALUE_OVERWRITE
Overwrite Cookie values for existing names.
|
Constructor and Description |
---|
Session()
Creates a new Session.
|
Modifier and Type | Method and Description |
---|---|
void |
addCookie(Cookie cookie,
java.lang.String url)
Adds a
Cookie for a given URL to the Session . |
void |
addCookies(java.util.Vector cookies,
java.lang.String url)
Adds a set of Cookies to the
Session for a given URL. |
void |
addCredentials(Credentials cred)
Adds new credentials to the set of credentials managed by the
Session |
static Session |
create()
Creates a new shared
Session if no exists. |
static void |
destroy()
Deletes the current
Session . |
static Session |
get()
Returns the current shared
Session . |
int |
getCookieAcceptPolicy()
Returns the Cookie management accept policy set for the current
Session . |
java.lang.String |
getCookieHeaderString(java.lang.String url)
Returns a String representation of the Cookies matching the given URL.
|
int |
getCookieMultiValuePolicy()
Returns the Cookie management multi value policy set for the current
Session
|
java.util.Vector |
getCookies()
Returns all Cookies managed by the
Session . |
java.util.Vector |
getCookies(java.lang.String url)
Returns all cookies matching the given URL.
|
java.util.Vector |
getCredentials()
Returns all credentials managed by the
Session . |
Credentials |
getCredentialsForRealm(java.lang.String realm)
Returns all credentials matching the given realm.
|
Credentials |
getCredentialsForURL(java.lang.String url)
Returns all credentials matching the given URL.
|
Credentials |
getCredentialsForURL(java.lang.String url,
java.lang.String realm)
Returns all credentials matching the given URL and realm.
|
void |
removeCookies()
Removes all Cookies from the current
Session . |
void |
removeCredentials()
Removes all credentials from the
Session . |
void |
setCookieAcceptPolicy(int cookieAcceptPolicy)
Sets the new accept policy for internal Cookie management.
|
void |
setCookieMultiValuePolicy(int cookieMultiValuePolicy)
Sets the new multi value policy for internal Cookie management.
|
public static int COOKIE_ACCEPT_NONE
public static int COOKIE_ACCEPT_ALL
public static int COOKIE_ACCEPT_SERVER
Cookie.getDomain()
matches the URL it is
coming from (default)public static int COOKIE_MULTIVALUE_OVERWRITE
public static int COOKIE_MULTIVALUE_ADD
public static int COOKIE_MULTIVALUE_MAX
public Credentials getCredentialsForURL(java.lang.String url)
Returns all credentials matching the given URL. Use
addCredentials(Credentials)
to register Credentials
to
be managed by the Session
url
- the URL as Stringpublic Credentials getCredentialsForRealm(java.lang.String realm)
Returns all credentials matching the given realm. Use
addCredentials(Credentials)
to register Credentials
to
be managed by the Session
realm
- the realm as Stringpublic Credentials getCredentialsForURL(java.lang.String url, java.lang.String realm)
Returns all credentials matching the given URL and realm. Use
addCredentials(Credentials)
to register Credentials
to
be managed by the Session
url
- the URL as Stringrealm
- the realm as Stringpublic void addCredentials(Credentials cred)
Adds new credentials to the set of credentials managed by the
Session
cred
- the Credentials
to be added to the Sessionpublic java.util.Vector getCredentials()
Session
.Session
.public void removeCredentials()
Session
.public void addCookies(java.util.Vector cookies, java.lang.String url)
Adds a set of Cookies to the Session
for a given URL. Each
Cookie
will be added to the Session
using
addCookie(Cookie, String)
.
cookies
- the Cookies to be added to the Session
.url
- the URL the Cookies will be registered for.addCookie(Cookie, String)
public void addCookie(Cookie cookie, java.lang.String url)
Adds a Cookie
for a given URL to the Session
.
Depending on the accept policy of the Session
, the
url
parameter might be used to validate the Cookie before
adding it to the Session. If a Cookie does not match the given URL or the
accept policy is set to COOKIE_ACCEPT_NONE
, the Cookie
will be silently be ignored.
If the Cookie does not have a domain or path set up, the url
parameter will be used to add domain and path to the Cookie for security
reasons.
If a Cookie is added that already exists, this means that a Cookie exists
in the current Session
with the same name, domain and path,
the multi value policy setting controls if the Cookie is overwritten or
added. Use setCookieMultiValuePolicy(int)
to change the multi
value policy for the current Session
.
cookie
- the Cookie to be added to the Session
.url
- the URL the Cookies will be registered for.public java.util.Vector getCookies()
Returns all Cookies managed by the Session
.
public java.util.Vector getCookies(java.lang.String url)
Returns all cookies matching the given URL.
url
- the URL as Stringpublic java.lang.String getCookieHeaderString(java.lang.String url)
Returns a String representation of the Cookies matching the given URL. If
no Cookie matches the given URL, null
will be returned.
The String returned can be used within a HTTP cookie header, but does not contain the leading HTTP header field.
url
- the URL as Stringpublic void removeCookies()
Session
.public int getCookieAcceptPolicy()
Returns the Cookie management accept policy set for the current
Session
.
COOKIE_ACCEPT_ALL
,
COOKIE_ACCEPT_NONE
or
COOKIE_ACCEPT_SERVER
public void setCookieAcceptPolicy(int cookieAcceptPolicy)
Sets the new accept policy for internal Cookie management.
cookieAcceptPolicy
- one of COOKIE_ACCEPT_ALL
,
COOKIE_ACCEPT_NONE
or
COOKIE_ACCEPT_SERVER
public int getCookieMultiValuePolicy()
Returns the Cookie management multi value policy set for the current
Session
COOKIE_MULTIVALUE_ADD
or
COOKIE_MULTIVALUE_OVERWRITE
public void setCookieMultiValuePolicy(int cookieMultiValuePolicy)
Sets the new multi value policy for internal Cookie management.
cookieMultiValuePolicy
- one of COOKIE_MULTIVALUE_ADD
or
COOKIE_MULTIVALUE_OVERWRITE
public static Session get()
Returns the current shared Session
.
public static Session create()
Creates a new shared Session
if no exists.
get()
public static void destroy()