- java.lang.Object
-
- com.aoapps.servlet.http.Cookies
-
public final class Cookies extends Object
Helper utility to set, get, remove, encode, and decode cookies.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String comment, int maxAge, boolean secure, boolean contextOnlyPath)Adds a new cookie to the response.static StringdecodeComment(String comment)Gets the comment of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static StringdecodeName(String name)Gets the name of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static StringdecodePath(String path)Gets the path of a cookie, decoded viaURIDecoder.decodeURI(java.lang.String).static StringdecodeValue(String value)Gets the value of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static StringencodeComment(String comment)Encodes the comment of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).static StringencodeName(String name)Encodes the name of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).static StringencodePath(String path)Encodes the path of a cookie viaURIEncoder.encodeURI(java.lang.String).static StringencodeValue(String value)Encodes the value of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).static StringgetComment(Cookie cookie)Gets the comment of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static StringgetCookie(HttpServletRequest request, String name)Gets a cookie value given its name ornullif not found.static StringgetName(Cookie cookie)Gets the name of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static StringgetPath(Cookie cookie)Gets the path of a cookie, decoded viaURIDecoder.decodeURI(java.lang.String).static StringgetValue(Cookie cookie)Gets the value of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).static CookienewCookie(String name, String value)Creates a new cookie, but does not add it to any response.static CookienewCookie(HttpServletRequest request, String name, String value, String comment, int maxAge, boolean secure, boolean contextOnlyPath)Creates a new cookie, but does not add it to any response.static voidremoveCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean secure, boolean contextOnlyPath)Removes a cookie by adding it with maxAge of zero.static StringsetComment(Cookie cookie, String comment)Sets the comment of a cookie, encoded viaURIEncoder.encodeURIComponent(java.lang.String).static StringsetPath(Cookie cookie, String path)Sets the path of a cookie, encoded viaURIEncoder.encodeURI(java.lang.String).
-
-
-
Method Detail
-
encodeName
public static String encodeName(String name)
Encodes the name of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).- Returns:
- The encoded name
- See Also:
Cookie(java.lang.String, java.lang.String)
-
encodeValue
public static String encodeValue(String value)
Encodes the value of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).- Returns:
- The encoded value
- See Also:
Cookie(java.lang.String, java.lang.String)
-
encodeComment
public static String encodeComment(String comment)
Encodes the comment of a cookie viaURIEncoder.encodeURIComponent(java.lang.String).- Returns:
- The encoded comment
- See Also:
Cookie.setComment(java.lang.String)
-
encodePath
public static String encodePath(String path)
Encodes the path of a cookie viaURIEncoder.encodeURI(java.lang.String).- Returns:
- The encoded path
- See Also:
Cookie.setPath(java.lang.String)
-
decodeName
public static String decodeName(String name)
Gets the name of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded name
- See Also:
Cookie.getName()
-
decodeValue
public static String decodeValue(String value)
Gets the value of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded value
- See Also:
Cookie.getValue()
-
decodeComment
public static String decodeComment(String comment)
Gets the comment of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded comment
- See Also:
Cookie.getComment()
-
decodePath
public static String decodePath(String path)
Gets the path of a cookie, decoded viaURIDecoder.decodeURI(java.lang.String).- Returns:
- The decoded path
- See Also:
Cookie.getPath()
-
newCookie
public static Cookie newCookie(String name, String value)
Creates a new cookie, but does not add it to any response. Encodes name and value viaURIEncoder.encodeURIComponent(java.lang.String).- Returns:
- The new cookie
- See Also:
Cookie(java.lang.String, java.lang.String)
-
setComment
public static String setComment(Cookie cookie, String comment)
Sets the comment of a cookie, encoded viaURIEncoder.encodeURIComponent(java.lang.String).- Returns:
- The encoded comment
- See Also:
Cookie.setComment(java.lang.String)
-
setPath
public static String setPath(Cookie cookie, String path)
Sets the path of a cookie, encoded viaURIEncoder.encodeURI(java.lang.String).- Returns:
- The encoded path
- See Also:
Cookie.setPath(java.lang.String)
-
getName
public static String getName(Cookie cookie)
Gets the name of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded name
- See Also:
Cookie.getName()
-
getValue
public static String getValue(Cookie cookie)
Gets the value of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded value
- See Also:
Cookie.getValue()
-
getComment
public static String getComment(Cookie cookie)
Gets the comment of a cookie, decoded viaURIDecoder.decodeURIComponent(java.lang.String).- Returns:
- The decoded comment
- See Also:
Cookie.getComment()
-
getPath
public static String getPath(Cookie cookie)
Gets the path of a cookie, decoded viaURIDecoder.decodeURI(java.lang.String).- Returns:
- The decoded path
- See Also:
Cookie.getPath()
-
newCookie
public static Cookie newCookie(HttpServletRequest request, String name, String value, String comment, int maxAge, boolean secure, boolean contextOnlyPath)
Creates a new cookie, but does not add it to any response. Encodes name, value, and comment viaURIEncoder.encodeURIComponent(java.lang.String). Encodes path viaURIEncoder.encodeURI(java.lang.String).
-
addCookie
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String comment, int maxAge, boolean secure, boolean contextOnlyPath)
Adds a new cookie to the response. Encodes name, value, and comment viaURIEncoder.encodeURIComponent(java.lang.String). Encodes path viaURIEncoder.encodeURI(java.lang.String).
-
getCookie
public static String getCookie(HttpServletRequest request, String name)
Gets a cookie value given its name ornullif not found. Encodes name and value viaURIEncoder.encodeURIComponent(java.lang.String).
-
removeCookie
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean secure, boolean contextOnlyPath)
Removes a cookie by adding it with maxAge of zero. Encodes name viaURIEncoder.encodeURIComponent(java.lang.String).
-
-