You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by bh...@actrix.gen.nz on 2011/09/29 00:01:32 UTC

How to set Cookie on Redirect to URL

Hi,

How do I add a cookie to a URL redirect response? I started with the
following class:

public class RedirectToUrlWithHandlerException extends
ReplaceHandlerException {
...
    
    public RedirectToUrlWithHandlerException(final String redirectUrl,
final int statusCode, final IRequestHandler handler) {
        super(new RedirectRequestHandler(redirectUrl, statusCode){
            @Override
            public void detach(IRequestCycle requestCycle) {
                super.detach(requestCycle);
                handler.detach(requestCycle);
            }
            @Override
            public void respond(IRequestCycle requestCycle) {
                super.respond(requestCycle);
                handler.respond(requestCycle);
            }
        }, true);
    }
}


and in the page:

        throw new RedirectToUrlWithHandlerException(targetUrl, new
IRequestHandler(){
            @Override
            public void respond(IRequestCycle requestCycle) {
		// Set cookie
                cookieUtils.save(name, value);
            }

            @Override
            public void detach(IRequestCycle requestCycle) { }
        });

with cookieUtils:

        instance.cookieUtils = new CookieUtils(cookieDefaults){
            @Override
            protected void initializeCookie(Cookie cookie) {
                super.initializeCookie(cookie);
                cookie.setPath("/");
            }
        };


Still no cookie in the response.

I know this is a tricky issue where older browsers don't always
cooperate, but the page being redirected to can't set the cookie
because the cookie is used to transfer information to it.

Many thanks,

Bernard

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to set Cookie on Redirect to URL

Posted by Martin Grigorov <mg...@apache.org>.
Here is what I use to keep the cookies (headers):

public class NonResettingRestartException extends ReplaceHandlerException {

    public NonResettingRestartException(final Class<? extends Page<?>>
pageClass, final PageParameters params, final RequestCycle cycle) {
        super(createRequestHandler(pageClass, params), true);

        Response response = cycle.getResponse();
        if (response instanceof IMetaDataBufferingWebResponse) {
            IMetaDataBufferingWebResponse bufferingWebResponse =
(IMetaDataBufferingWebResponse) response;
            bufferingWebResponse.writeMetaData((WebResponse)
cycle.getOriginalResponse());
        }
    }

    private static IRequestHandler createRequestHandler(Class<?
extends Page<?>> pageClass, PageParameters params) {
        return new RenderPageRequestHandler(new
PageProvider(pageClass, params));
    }

}

On Thu, Sep 29, 2011 at 12:01 AM,  <bh...@actrix.gen.nz> wrote:
> Hi,
>
> How do I add a cookie to a URL redirect response? I started with the
> following class:
>
> public class RedirectToUrlWithHandlerException extends
> ReplaceHandlerException {
> ...
>
>    public RedirectToUrlWithHandlerException(final String redirectUrl,
> final int statusCode, final IRequestHandler handler) {
>        super(new RedirectRequestHandler(redirectUrl, statusCode){
>            @Override
>            public void detach(IRequestCycle requestCycle) {
>                super.detach(requestCycle);
>                handler.detach(requestCycle);
>            }
>            @Override
>            public void respond(IRequestCycle requestCycle) {
>                super.respond(requestCycle);
>                handler.respond(requestCycle);
>            }
>        }, true);
>    }
> }
>
>
> and in the page:
>
>        throw new RedirectToUrlWithHandlerException(targetUrl, new
> IRequestHandler(){
>            @Override
>            public void respond(IRequestCycle requestCycle) {
>                // Set cookie
>                cookieUtils.save(name, value);
>            }
>
>            @Override
>            public void detach(IRequestCycle requestCycle) { }
>        });
>
> with cookieUtils:
>
>        instance.cookieUtils = new CookieUtils(cookieDefaults){
>            @Override
>            protected void initializeCookie(Cookie cookie) {
>                super.initializeCookie(cookie);
>                cookie.setPath("/");
>            }
>        };
>
>
> Still no cookie in the response.
>
> I know this is a tricky issue where older browsers don't always
> cooperate, but the page being redirected to can't set the cookie
> because the cookie is used to transfer information to it.
>
> Many thanks,
>
> Bernard
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org