Check to see if it's already there?
On Fri, Dec 24, 2010 at 7:04 AM, JOSE L MARTINEZ-AVIAL <[email protected]>wrote:
> Hello all,
> I've developed an interceptor to add cookies to the response. It works as
> follows:
> [..]
> public String intercept(ActionInvocation invocation) throws Exception {
> invocation.addPreResultListener(this);
> return invocation.invoke();
> }
>
> public void beforeResult(ActionInvocation invocation, String resultCode) {
> //log.debug("beforeResult start");
> ActionContext ac = invocation.getInvocationContext();
> HttpServletResponse response = (HttpServletResponse)
> ac.get(StrutsStatics.HTTP_RESPONSE);
> addCookiesToResponse(invocation.getAction(), response);
> //log.debug("beforeResult end");
> }
> private void addCookiesToResponse(Object action, HttpServletResponse
> response) {
> //log.info("CookieProviderInterceptor "+action);
> if (action instanceof CookieProvider) {
>
>
> Map<String,CookieBean> cookies = ((CookieProvider)
> action).getCookies();
> //log.info("CookieProviderInterceptor "+cookies);
>
> if (cookies != null) {
> Set<Entry<String,CookieBean>> cookieSet =
> cookies.entrySet();
> for (Entry<String, CookieBean> entry : cookieSet) {
> CookieBean cookiebean = entry.getValue();
> Cookie cookie = new Cookie(cookiebean.getCookieName(),
> cookiebean.getCookieValue());
> cookie.setMaxAge(cookiebean.getMaxAge());
> cookie.setComment(cookiebean.getComment());
> if (cookiebean.getDomain()!=null)
> cookie.setDomain(cookiebean.getDomain());
> cookie.setPath(cookiebean.getPath());
> cookie.setSecure(cookiebean.getSecure());
> cookie.setVersion(cookiebean.getVersion());
> //log.info("adding "+cookie);
> response.addCookie(cookie);
> }
> }
> }
> }
>
> So the idea is that the Action implements CookieProvider, returning a map
> of
> cookies, and put the cookies in the response. It works well, but I'm having
> some problems when I chain two actions, if both of them are CookieProvider,
> and both return a cookie with the same name. My idea was be that the first
> cookie would be overwritten by the second one, but in fact the response has
> both cookies, with the same name. After giving it a thought, it makes
> sense,
> so probably I'm doing something wrong. Any insight on how to do this?
>
> TIA
>
> Jose Luis
>