Re: How to create Wicket's session and inject to Wicket's life cycle using Servlet Filter ?
Thank you , I've found the answer : httpSession = ((ServletWebRequest)request).getHttpServletRequest().getSession(); This is a better way ... thanks a lot. 2010/1/10 smallufo > > > 2010/1/10 Igor Vaynberg > > why not simply put whatever it is into the httpsession, and then write >> a getter in wicket's session that retrieves it from there? seems >> simpler. >> >> -igor > > > Thank you . > But how to get HttpSession inside Wicket's Session ? > thanks ... > >
Re: How to create Wicket's session and inject to Wicket's life cycle using Servlet Filter ?
2010/1/10 Igor Vaynberg > why not simply put whatever it is into the httpsession, and then write > a getter in wicket's session that retrieves it from there? seems > simpler. > > -igor Thank you . But how to get HttpSession inside Wicket's Session ? thanks ...
Re: How to create Wicket's session and inject to Wicket's life cycle using Servlet Filter ?
why not simply put whatever it is into the httpsession, and then write a getter in wicket's session that retrieves it from there? seems simpler. -igor On Sat, Jan 9, 2010 at 4:07 AM, smallufo wrote: > 2010/1/9 smallufo > >> 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. > - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
How to create Wicket's session and inject to Wicket's life cycle using Servlet Filter ?
2010/1/9 smallufo > 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.