Fix bug with cookie value which need to be encoded/decoded.

This commit is contained in:
Christian P. MOMON 2013-12-03 17:02:11 +01:00
parent 3a2cdbad34
commit 524c5b1fa6

View file

@ -1,5 +1,7 @@
package fr.devinsy.kiss4web; package fr.devinsy.kiss4web;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -33,14 +35,16 @@ public class CookieHelper
} }
/** /**
* * Warning: value is UTF-8 URLEncoded!
*/ */
static public Cookie buildCookie(final String name, final String value, final int duration, final Scope secure) static public Cookie buildCookie(final String name, final String value, final int duration, final Scope secure)
{ {
Cookie result; Cookie result;
// //
result = new Cookie(name, value); try
{
result = new Cookie(name, java.net.URLEncoder.encode(value, "UTF-8"));
result.setMaxAge(duration); result.setMaxAge(duration);
result.setPath("/"); result.setPath("/");
@ -55,6 +59,12 @@ public class CookieHelper
secureValue = false; secureValue = false;
} }
result.setSecure(secureValue); result.setSecure(secureValue);
}
catch (UnsupportedEncodingException exception)
{
exception.printStackTrace();
throw new IllegalArgumentException("value is unsupported encoding.");
}
// //
return (result); return (result);
@ -135,12 +145,14 @@ public class CookieHelper
} }
/** /**
* * Note: value is UTF-8 decoded.
*/ */
static public Object getCookieValue(final Cookie[] cookies, final String key) static public Object getCookieValue(final Cookie[] cookies, final String key)
{ {
Object result; Object result;
try
{
Cookie cookie = getCookie(cookies, key); Cookie cookie = getCookie(cookies, key);
if (cookie == null) if (cookie == null)
@ -149,7 +161,13 @@ public class CookieHelper
} }
else else
{ {
result = cookie.getValue(); result = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
catch (UnsupportedEncodingException exception)
{
exception.printStackTrace();
throw new IllegalArgumentException();
} }
// //