2010/1/9 smallufo <[email protected]>
> Hi all :
>
> I'm trying to inject something to the Wicket Session by a Generic Servlet
> Filter :
>
> public class UserFilter implements Filter
> {
> private UserDao UserDao;
>
> @Override
> public void init(FilterConfig config) throws ServletException
> {
> WebApplicationContext wac =
> WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
> UserDao = (UserDao) wac.getBean("userDao");
> }
>
> @Override
> public void doFilter(ServletRequest req, ServletResponse res, FilterChain
> chain) throws IOException, ServletException
> {
>
> System.out.println(getClass().getName() + " : UserDao = " + UserDao);
> // ... blah...
> // do something , get user from cookie , authenticate cookie , and set
> it to Session
> // ... blah...
> MySession s = MySession.get(); // error occurs !
> chain.doFilter(req, res);
> }
>
>
> It seems I cannot get Wicket's session outside wicket's request cycle.
> I searched and found WicketSessionFilter , but it seems it is for
> "Retrieving" objects from wicket's session , not for Filter to inject
> something to wicket's session...
>
>
>
I've almost solve this problem by looking into
org.apache.wicket.protocol.http.servlet.WicketSessionFilter , and
implements my Filter :
public class UserFilter implements Filter
{
private String sessionKey;
private UserDao userDao;
@Override
public void init(FilterConfig config) throws ServletException
{
WebApplicationContext wac =
WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
userDao = (UserDao) wac.getBean("userDao");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain
chain) throws IOException, ServletException
{
HttpServletRequest httpServletRequest = ((HttpServletRequest)req);
// create a httpSession if there is no session
HttpSession httpSession = httpServletRequest.getSession(true);
WebApplication application = (WebApplication)Application.get("MyApp");
if (application != null)
{
sessionKey = application.getSessionAttributePrefix(null, "MyApp") +
Session.SESSION_ATTRIBUTE_NAME;
//MySession extends WebSession
MySession mySession = (MySession)httpSession.getAttribute(sessionKey);
if (mySession != null)
mySession.setUser(getUserFromCookies(httpServletRequest));
else
{
//No mySession , need to create a MySession and inject to
httpSession
MySession newSession = new MySession(application , ????????????); //
<----------------- How to get Request ?
newSession.setUser(getUserFromCookies(httpServletRequest));
httpSession.setAttribute(sessionKey, newSession);
}
}
chain.doFilter(req, res);
}
}
The problem is : when there is no Wicket's session , how do I create a
Wicket's session outside wicket's request cycle ?
WebSession needs a "org.apache.wicket.Request" to construct , how do I get
this Request in the filter ?
Thanks a lot.