You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by JOSE L MARTINEZ-AVIAL <jl...@gmail.com> on 2010/12/24 13:04:48 UTC

How to avoid PreResultListener called twice in action chaining

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

Re: How to avoid PreResultListener called twice in action chaining

Posted by Chris Pratt <th...@gmail.com>.
Two ideas, provide your own wrapper around the HttpServletRequest that does
allow getting back the cookies (probably more work that it's worth), or add
the cookies as a request attribute that you can query to see which cookies
were added during this request.  If it comes back null, you're the first (or
only) in the chain, if not, then you have the list of cookies that were
added.  I'm not sure if you'll be able to overwrite an existing cookie, but
you could, institute a "first-come, first-served" rule.
  (*Chris*)

On Fri, Dec 24, 2010 at 10:22 AM, <jl...@gmail.com> wrote:

> How can I do that when I chain actions?. I mean, the second action is
> chained after the first one, but there are situations in which there is no
> chaining at all(both actions can be executed indepently) or the chaining can
> be the other way. In all cases I need to keep only the last cookie
> generated. I can keep a set of cookies in the request, but how I can put
> them into the request after all the processing has been done? Since if I
> chain the actions, there is no way to know programatically which is the last
> one, right? Or is there?
>
> Thanks
> Jose
> Sent via BlackBerry from T-Mobile
>
> -----Original Message-----
> From: Dave Newton <da...@gmail.com>
> Date: Fri, 24 Dec 2010 12:57:53
> To: Struts Users Mailing List<us...@struts.apache.org>; <jl...@gmail.com>
> Subject: Re: How to avoid PreResultListener called twice in action chaining
>
> Save cookies outside the request then, and only add the cookie at the last
> minute.
>
> Dave
> On Dec 24, 2010 8:49 AM, <jl...@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 <da...@gmail.com>
> > Date: Fri, 24 Dec 2010 07:47:59
> > To: Struts Users Mailing List<us...@struts.apache.org>
> > Reply-To: "Struts Users Mailing List" <us...@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 <jlmagc@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
> >>
> >
>
>

Re: How to avoid PreResultListener called twice in action chaining

Posted by Dave Newton <da...@gmail.com>.
Yet another reason to avoid action chaining, too.
On Dec 24, 2010 2:29 PM, "Maurizio Cucchiara" <ma...@gmail.com>
wrote:
> There should be at least a way to check if request live along a chain
> request, it could be possible by asking for size at stack.getRoot()
>
> Maurizio Cucchiara
>
> On Dec 24, 2010 7:22 PM, <jl...@gmail.com> wrote:
> How can I do that when I chain actions?. I mean, the second action is
> chained after the first one, but there are situations in which there is no
> chaining at all(both actions can be executed indepently) or the chaining
can
> be the other way. In all cases I need to keep only the last cookie
> generated. I can keep a set of cookies in the request, but how I can put
> them into the request after all the processing has been done? Since if I
> chain the actions, there is no way to know programatically which is the
last
> one, right? Or is there?
>
> Thanks
> Jose
>
> Sent via BlackBerry from T-Mobile
>
> -----Original Message-----
> From: Dave Newton <davelnewton@gmail.c...
>
> Date: Fri, 24 Dec 2010 12:57:53
> To: Struts Users Mailing List<us...@struts.apache.org>; <jlmagc@gmai...

Re: How to avoid PreResultListener called twice in action chaining

Posted by Maurizio Cucchiara <ma...@gmail.com>.
There should be at least a way to check if request live along a chain
request, it could be possible by asking for size at stack.getRoot()

Maurizio Cucchiara

On Dec 24, 2010 7:22 PM, <jl...@gmail.com> wrote:
How can I do that when I chain actions?. I mean, the second action is
chained after the first one, but there are situations in which there is no
chaining at all(both actions can be executed indepently) or the chaining can
be the other way. In all cases I need to keep only the last cookie
generated. I can keep a set of cookies in the request, but how I can put
them into the request after all the processing has been done? Since if I
chain the actions, there is no way to know programatically which is the last
one, right? Or is there?

Thanks
Jose

Sent via BlackBerry from T-Mobile

-----Original Message-----
From: Dave Newton <davelnewton@gmail.c...

Date: Fri, 24 Dec 2010 12:57:53
To: Struts Users Mailing List<us...@struts.apache.org>; <jlmagc@gmai...

Re: How to avoid PreResultListener called twice in action chaining

Posted by jl...@gmail.com.
How can I do that when I chain actions?. I mean, the second action is chained after the first one, but there are situations in which there is no chaining at all(both actions can be executed indepently) or the chaining can be the other way. In all cases I need to keep only the last cookie generated. I can keep a set of cookies in the request, but how I can put them into the request after all the processing has been done? Since if I chain the actions, there is no way to know programatically which is the last one, right? Or is there?

Thanks
Jose
Sent via BlackBerry from T-Mobile

-----Original Message-----
From: Dave Newton <da...@gmail.com>
Date: Fri, 24 Dec 2010 12:57:53 
To: Struts Users Mailing List<us...@struts.apache.org>; <jl...@gmail.com>
Subject: Re: How to avoid PreResultListener called twice in action chaining

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

Dave
On Dec 24, 2010 8:49 AM, <jl...@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 <da...@gmail.com>
> Date: Fri, 24 Dec 2010 07:47:59
> To: Struts Users Mailing List<us...@struts.apache.org>
> Reply-To: "Struts Users Mailing List" <us...@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 <jlmagc@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
>>
>


Re: How to avoid PreResultListener called twice in action chaining

Posted by Dave Newton <da...@gmail.com>.
Save cookies outside the request then, and only add the cookie at the last
minute.

Dave
On Dec 24, 2010 8:49 AM, <jl...@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 <da...@gmail.com>
> Date: Fri, 24 Dec 2010 07:47:59
> To: Struts Users Mailing List<us...@struts.apache.org>
> Reply-To: "Struts Users Mailing List" <us...@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 <jlmagc@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
>>
>

Re: How to avoid PreResultListener called twice in action chaining

Posted by jl...@gmail.com.
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 <da...@gmail.com>
Date: Fri, 24 Dec 2010 07:47:59 
To: Struts Users Mailing List<us...@struts.apache.org>
Reply-To: "Struts Users Mailing List" <us...@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 <jl...@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
>


Re: How to avoid PreResultListener called twice in action chaining

Posted by Dave Newton <da...@gmail.com>.
Check to see if it's already there?

On Fri, Dec 24, 2010 at 7:04 AM, JOSE L MARTINEZ-AVIAL <jl...@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
>