views:

58

answers:

1

I need to grab a certain custom HTTP header value from every request and put it in WebSession so that it will be available on any WebPage later on. (I believe the Wicket way to do this is to have a custom class extending WebSession that has appropriate accessors.)

My question is, what kind of Filter (or other mechanism) I need to be able to both intercept the header and access the WebSession for storing the value?

I tried to do this with a normal JEE Filter, using

CustomSession session = (CustomSession) AuthenticatedWebSession.get();

But (perhaps not surprisingly), that yields:

java.lang.IllegalStateException: 
    you can only locate or create sessions in the context of a request cycle

Should I perhaps extend WicketFilter and do it there (can I access the session at that point?), or is something even more complicated required?

Of course, please point it out if I'm doing something completely wrong; I'm new to Wicket.

+2  A: 

I'd guess you need to implement a custom WebRequestCycle:

public class CustomRequestCycle extends WebRequestCycle{

    public CustomRequestCycle(WebApplication application,
        WebRequest request,
        Response response){
        super(application, request, response);
        String headerValue = request.getHttpServletRequest().getHeader("foo");
        ((MyCustomSession)Session.get()).setFoo(headerValue);
    }

}

And in your WebApplication class you register the custom RequestCycle like this:

public class MyApp extends WebApplication{

    @Override
    public RequestCycle newRequestCycle(Request request, Response response){
        return new CustomRequestCycle(this, (WebRequest) request, response);
    }

}

Reference:

seanizer
Thanks, I got it working with this approach! (Just took me a while to verify that with Tamper Data...) Using custom WebRequestCycle is actually pretty simple and clean, nothing as complicated as I thought.
Jonik
wicket has a simple solution for most problems, that's the nice thing about it
seanizer