Here is simple utility class:
CookieUtils.java
-----------------------------------
package org.apache.struts2.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
/**
* Cookie helper for Struts 2 actions.
*/
public abstract class CookieUtils
{
private static HttpServletRequest getRequest()
{
return ServletActionContext.getRequest();
}
private static HttpServletResponse getResponse()
{
return ServletActionContext.getResponse();
}
/**
* @return map of all the <code>Cookie</code>s included with this
request,
* or empty map if the request has no cookies.
*/
public static Map<String, Cookie> getCookies()
{
Map<String, Cookie> cookies = new HashMap<String, Cookie>();
Cookie[] cookieArray = getRequest().getCookies();
if (cookieArray != null)
{
for (Cookie cookie : cookieArray)
{
cookies.put(cookie.getName(), cookie);
}
}
return cookies;
}
public static Cookie getCookie(String cookieName)
{
Cookie foundCookie = null;
for (Cookie cookie : getRequest().getCookies())
{
if (cookie.getName().equals(cookieName))
{
foundCookie = cookie;
break;
}
}
return foundCookie;
}
public static String getCookieValue(String cookieName)
{
Cookie foundCookie = getCookie(cookieName);
return foundCookie == null ? null : foundCookie.getValue();
}
public static boolean hasCookie(String cookieName)
{
return getCookie(cookieName) != null;
}
public static void addCookie(Cookie cookie)
{
getResponse().addCookie(cookie);
}
public static void addCookie(String cookieName, String cookieValue)
{
Cookie cookie = new Cookie(cookieName, cookieValue);
addCookie(cookie);
}
public static void removeCookie(String cookieName)
{
Cookie cookie = new Cookie(cookieName, "");
// A zero value causes the cookie to be deleted.
cookie.setMaxAge(0);
addCookie(cookie);
}
}
Musachy Barroso wrote:
>
> a patch would be welcomed.
>
> musachy
>
> On Tue, Aug 25, 2009 at 7:51 PM, Xyzr<[email protected]> wrote:
>>
>> Struts 2 must have full featured cookie utils out of the box. I saw the
>> current CookieInterceptor, but it's insufficient.
>>
>> Needed methods (Cookie is javax.servlet.http.Cookie):
>> 1) Map<String, Cookie> getCookies(); // where key - cookie name
>> 2) String getCookie(String cookieName);
>> 3) setCookie(String cookieName, Cookie cookie); // or "addCookie(...)"
>> 4) removeCookie(String cookieName);
>>
>> I suppose this feature can be implemented either as Interceptor or as
>> Util
>> class.
>>
>> //-----------------------------------------------------------------------------
>> 1) Implementing as Interceptor:
>> interface CookiesAware {
>> void setCookies(Map<String, Cookie> cookies);
>>
>> // Helpers - methods that return Sets of cookies to be set/remove
>> // to/from response. Or these methods can be replaced by custom
>> // CookieHashMap that overrides "put", "putAll", "remove" and
>> "clear"
>> // to set/remove cookies to/from response.
>> Set<Cookie> getCookiesToSet();
>> Set<String> getCookiesToRemove();
>> }
>>
>> class CookiesInterceptor {
>> public String intercept(ActionInvocation actionInvocation) {
>> Object action = actionInvocation.getAction();
>> CookiesAware cookiesAware = null;
>> if (action instanceof CookiesAware)
>> {
>> HttpServletRequest request =
>> ServletActionContext.getRequest();
>> Map<String, Cookie> cookies = new HashMap<String,
>> Cookie>();
>> for (Cookie cookie : request.getCookies()) {
>> cookies.put(cookie.getName(), cookie);
>> }
>> cookiesAware = (CookiesAware) action;
>> cookiesAware.setCookies(cookies);
>> }
>>
>> String actionResult = actionInvocation.invoke();
>>
>> if (cookiesAware != null) {
>> HttpServletResponse response =
>> ServletActionContext.getResponse();
>>
>> Set<Cookie> cookiesToSet =
>> cookiesAware.getCookiesToSet();
>> if (cookiesToSet != null)
>> for (Cookie cookie : cookiesToSet) {
>> response.addCookie(cookie);
>> }
>> }
>>
>> Set<String> cookiesToRemove =
>> cookiesAware.getCookiesToRemove();
>> if (cookiesToRemove != null) {
>> for (String cookieName : cookiesToRemove)
>> {
>> Cookie cookie = new
>> Cookie(cookieName, "");
>> cookie.setMaxAge(0);
>> response.addCookie(cookie);
>> }
>> }
>> }
>>
>> return actionResult;
>> }
>> }
>>
>> //-----------------------------------------------------------------------------
>> 2) Implementing as Utility class:
>> // Use code from CookiesInterceptor
>> class abstract CookieUtils {
>> public static Map<String, Cookie> getCookies() {...}
>> public static String getCookie(String cookieName) {...}
>> public static void setCookie(String cookieName, Cookie cookie)
>> {...}
>> public static void removeCookie(String cookieName) {...}
>> }
>> --
>> View this message in context:
>> http://www.nabble.com/Full-featured-cookie-utils-out-of-the-box-tp25145716p25145716.html
>> Sent from the Struts - Dev mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
>
>
> --
> "Hey you! Would you help me to carry the stone?" Pink Floyd
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
--
View this message in context:
http://www.nabble.com/Full-featured-cookie-utils-out-of-the-box-tp25145716p25205770.html
Sent from the Struts - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]