Save cookies outside the request then, and only add the cookie at the last
minute.

Dave
On Dec 24, 2010 8:49 AM, <jlm...@gmail.com> wrote:
> Can't be done. The HttpServletResponse does not provide a method to check
if there is already a cookie on the request. And I need to put the cookie
generated by the last action, not the first one.
> Sent via BlackBerry from T-Mobile
>
> -----Original Message-----
> From: Dave Newton <davelnew...@gmail.com>
> Date: Fri, 24 Dec 2010 07:47:59
> To: Struts Users Mailing List<user@struts.apache.org>
> Reply-To: "Struts Users Mailing List" <user@struts.apache.org>
> Subject: Re: How to avoid PreResultListener called twice in action
chaining
>
> Check to see if it's already there?
>
> On Fri, Dec 24, 2010 at 7:04 AM, JOSE L MARTINEZ-AVIAL <jlm...@gmail.com
>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
>>
>

Reply via email to