From 143b983f196ef84b52211afecc146fb76520e5a9 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 20 Jun 2013 00:10:38 +0200 Subject: [PATCH] Improved Cookie management. --- src/fr/devinsy/kiss4web/CookieHelper.java | 176 ++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 src/fr/devinsy/kiss4web/CookieHelper.java diff --git a/src/fr/devinsy/kiss4web/CookieHelper.java b/src/fr/devinsy/kiss4web/CookieHelper.java new file mode 100644 index 0000000..49328bb --- /dev/null +++ b/src/fr/devinsy/kiss4web/CookieHelper.java @@ -0,0 +1,176 @@ +package fr.devinsy.kiss4web; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +/** + * + */ +public class CookieHelper +{ + static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (CookieHelper.class); + + /** + * + */ + static public Cookie buildCookie (String name, String value, int duration, boolean isSecure) + { + Cookie result; + + result = new Cookie (name, value); + result.setMaxAge (duration); + result.setPath ("/"); + result.setSecure (isSecure); + + // + return (result); + } + + + /** + * + */ + static public Cookie buildCookie (String name, String value, int duration) + { + Cookie result; + + result = buildCookie(name, value, duration, false); + + // + return (result); + } + + + /** + * + */ + static public Cookie getCookie (Cookie[] cookies, String key) + { + Cookie result = null; + + if (cookies == null) + { + result = null; + } + else + { + boolean ended = false; + int cookieCounter = 0; + while (!ended) + { + if (cookieCounter < cookies.length) + { + if (key.equals (cookies[cookieCounter].getName ())) + { + ended = true; + result = cookies[cookieCounter]; + } + else + { + cookieCounter += 1; + } + } + else + { + ended = true; + result = null; + } + } + } + + // + return (result); + } + + + /** + * + */ + static public Cookie getCookie (HttpServletRequest request, String key) + { + Cookie result = null; + + result = getCookie(request.getCookies (), key); + + // + return(result); + } + + + /** + * + */ + static public Object getCookieValue (Cookie[] cookies, String key) + { + Object result; + + Cookie cookie = getCookie(cookies, key); + + if (cookie == null) + { + result = null; + } + else + { + result = cookie.getValue(); + } + + // + return(result); + } + + + /** + * + */ + static public Object getCookieValue (HttpServletRequest request, String key) + { + Object result; + + result = getCookieValue (request.getCookies (), key); + + // + return(result); + } + + + /** + * + */ + static public boolean exists(HttpServletRequest request, String key) + { + boolean result; + + if (getCookieValue(request, key) == null) + { + result = false; + } + else + { + result = true; + } + + // + return(result); + } + + + /** + * + */ + static public void set (HttpServletResponse response, String name, String value, int duration) + { + response.addCookie (buildCookie(name, value, duration)); + } + + + /** + * + */ + static public void reset (HttpServletResponse response, String key) + { + response.addCookie(buildCookie(key, "", 0)); + } +} \ No newline at end of file