开发者

How to intercept custom HTTP header value and store it in Wicket's WebSession?

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 Java EE 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开发者_JAVA技巧 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.


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:

  • Request cycle and request cycle processor
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜