Package net.shibboleth.shared.net
Class CookieManager
java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
net.shibboleth.shared.net.CookieManager
- All Implemented Interfaces:
Component,DestructableComponent,InitializableComponent
A helper class for managing one or more cookies on behalf of a component.
This bean centralizes settings related to cookie creation and access, and is parameterized by name so that multiple cookies may be managed with common properties.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringDomain of cookie.private StringPath of cookie.private booleanIs cookie marked HttpOnly?private NonnullSupplier<HttpServletRequest>Supplier for the servlet request to read from.private NonnullSupplier<HttpServletResponse>Supplier for the servlet response to write to.private intMaximum age in seconds, or -1 for session.private booleanIs cookie secure? -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a cookie with the specified name and value.protected voidAdd a cookie with the specified name and value.private StringTurn the servlet context path into an appropriate cookie path.booleancookieHasValue(String name, String expectedValue) Check whether a cookie has a certain value.protected voidPerforms the initialization of the component.Get the cookie domain to use.Get the cookie path to use.getCookieValue(String name, String defValue) Return the first matching cookie's value.protected HttpServletRequestGet the current HTTP request if available.protected HttpServletResponseGet the current HTTP response if available.intGet maximum age of cookies in seconds.voidsetCookieDomain(String domain) Set the cookie domain to use.voidsetCookiePath(String path) Set the cookie path to use.voidsetHttpOnly(boolean flag) Set the HttpOnly flag.voidsetHttpServletRequestSupplier(NonnullSupplier<HttpServletRequest> requestSupplier) Set the Supplier for the servlet request to read from.voidsetHttpServletResponseSupplier(NonnullSupplier<HttpServletResponse> responseSupplier) Set the supplier for the servlet response to write to.voidsetMaxAge(int age) Maximum age in seconds, or -1 for per-session.voidMaximum age expressed as aDuration.voidsetSecure(boolean flag) Set the SSL-only flag.voidunsetCookie(String name) Unsets a cookie with the specified name.Methods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, doDestroy, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, initialize, isDestroyed, isInitialized
-
Field Details
-
cookiePath
Path of cookie. -
cookieDomain
Domain of cookie. -
httpRequestSupplier
Supplier for the servlet request to read from. -
httpResponseSupplier
Supplier for the servlet response to write to. -
secure
private boolean secureIs cookie secure? -
httpOnly
private boolean httpOnlyIs cookie marked HttpOnly? -
maxAge
private int maxAgeMaximum age in seconds, or -1 for session.
-
-
Constructor Details
-
CookieManager
public CookieManager()Constructor.
-
-
Method Details
-
getCookiePath
Get the cookie path to use.- Returns:
- cookie path
- Since:
- 9.1.0
-
setCookiePath
Set the cookie path to use.Defaults to the servlet context path.
- Parameters:
path- cookie path to use, or null for the default
-
getCookieDomain
Get the cookie domain to use.- Returns:
- cookie domain
- Since:
- 9.1.0
-
setCookieDomain
Set the cookie domain to use.- Parameters:
domain- the cookie domain to use, or null for the default
-
getHttpServletRequest
Get the current HTTP request if available.- Returns:
- current HTTP request
-
getHttpServletResponse
Get the current HTTP response if available.- Returns:
- current HTTP response or null
-
setSecure
public void setSecure(boolean flag) Set the SSL-only flag.- Parameters:
flag- flag to set
-
setHttpOnly
public void setHttpOnly(boolean flag) Set the HttpOnly flag.- Parameters:
flag- flag to set
-
getMaxAge
public int getMaxAge()Get maximum age of cookies in seconds.- Returns:
- max age in seconds
- Since:
- 9.1.0
-
setMaxAge
public void setMaxAge(int age) Maximum age in seconds, or -1 for per-session.- Parameters:
age- max age to set
-
setMaxAgeDuration
Maximum age expressed as aDuration.- Parameters:
age- max age of cookie- Since:
- 9.0.0
-
doInitialize
Performs the initialization of the component. This method is executed within the lock on the object being initialized. The default implementation of this method is a no-op.- Overrides:
doInitializein classAbstractInitializableComponent- Throws:
ComponentInitializationException- thrown if there is a problem initializing the component
-
addCookie
Add a cookie with the specified name and value.- Parameters:
name- name of cookievalue- value of cookie
-
addCookie
protected void addCookie(@Nonnull @NotEmpty String name, @Nonnull @NotEmpty String value, int overrideMaxAge) Add a cookie with the specified name and value.- Parameters:
name- name of cookievalue- value of cookieoverrideMaxAge- max-age value to use- Since:
- 9.1.0
-
unsetCookie
Unsets a cookie with the specified name.- Parameters:
name- name of cookie
-
cookieHasValue
public boolean cookieHasValue(@Nonnull @NotEmpty String name, @Nonnull @NotEmpty String expectedValue) Check whether a cookie has a certain value.- Parameters:
name- name of cookieexpectedValue- expected value of cookie- Returns:
- true iff the cookie exists and has the expected value
-
getCookieValue
Return the first matching cookie's value.- Parameters:
name- cookie namedefValue- default value to return if the cookie isn't found- Returns:
- cookie value
-
contextPathToCookiePath
Turn the servlet context path into an appropriate cookie path.- Returns:
- the cookie path
-