Java tutorial
/* * $Id: WicketSessionFilter.java 4710 2006-03-02 08:46:15 +0000 (Thu, 02 Mar * 2006) eelco12 $ $Revision: 6202 $ $Date: 2006-03-02 08:46:15 +0000 (Thu, 02 * Mar 2006) $ * * ============================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package wicket.protocol.http.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import wicket.Session; /** * <p> * This filter can be used to make the Wicket * {@link wicket.protocol.http.WebSession} instances available to non-wicket * servlets. * </p> * <p> * The following example displays how you can make the Wicket session object of * application SessionApplication, mapped on <code>/sessiontest/*</code> * available for servlet WicketSessionServlet, mapped under * <code>/servlet/sessiontest</code>: * * <pre> * <filter> * <filter-name>WicketSessionFilter</filter-name> * <filter-class>wicket.protocol.http.servlet.WicketSessionFilter</filter-class> * <init-param> * <param-name>servletPath</param-name> * <param-value>sessiontest</param-value> * </init-param> * </filter> * * <filter-mapping> * <filter-name>WicketSessionFilter</filter-name> * <url-pattern>/servlet/sessiontest</url-pattern> * </filter-mapping> * * <servlet> * <servlet-name>SessionApplication</servlet-name> * <servlet-class>wicket.protocol.http.WicketServlet</servlet-class> * <init-param> * <param-name>applicationClassName</param-name> * <param-value>session.SessionApplication</param-value> * </init-param> * <load-on-startup>1</load-on-startup> * </servlet> * * <servlet> * <servlet-name>WicketSessionServlet</servlet-name> * <servlet-class>session.WicketSessionServlet</servlet-class> * <load-on-startup>1</load-on-startup> * </servlet> * * <servlet-mapping> * <servlet-name>SessionApplication</servlet-name> * <url-pattern>/sessiontest/*</url-pattern> * </servlet-mapping> * * <servlet-mapping> * <servlet-name>WicketSessionServlet</servlet-name> * <url-pattern>/servlet/sessiontest</url-pattern> * </servlet-mapping> * </pre> * * After that, you can get to the Wicket session in the usual fashion: * * <pre> * wicket.Session wicketSession = wicket.Session.get(); * </pre> * * </p> * * @author Eelco Hillenius */ public class WicketSessionFilter implements Filter { /** log. */ private static final Log log = LogFactory.getLog(WicketSessionFilter.class); /** the servlet path. */ private String servletPath; /** the session key where the Wicket session should be stored. */ private String sessionKey; /** * Construct. */ public WicketSessionFilter() { } /** * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig filterConfig) throws ServletException { servletPath = filterConfig.getInitParameter("servletPath"); if (servletPath == null) { throw new ServletException( "you must provide init parameter servlet-path if you want to use " + getClass().getName()); } if (servletPath.charAt(0) != '/') { servletPath = '/' + servletPath; } if (log.isDebugEnabled()) { log.debug("servlet path set to " + servletPath); } sessionKey = "wicket:" + servletPath + ":" + Session.SESSION_ATTRIBUTE_NAME; if (log.isDebugEnabled()) { log.debug("will use " + sessionKey + " as the session key to get the Wicket session"); } } /** * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = ((HttpServletRequest) request); HttpSession httpSession = httpServletRequest.getSession(false); if (httpSession != null) { Session session = (Session) httpSession.getAttribute(sessionKey); if (session != null) { // set the session's threadlocal Session.set(session); if (log.isDebugEnabled()) { log.debug("session " + session + " set as current for " + httpServletRequest.getContextPath() + "," + httpServletRequest.getServerName()); } } else { if (log.isDebugEnabled()) { log.debug("could not set Wicket session: key " + sessionKey + " not found in http session for " + httpServletRequest.getContextPath() + "," + httpServletRequest.getServerName()); } } } else { if (log.isDebugEnabled()) { log.debug("could not set Wicket session: no http session was created yet for " + httpServletRequest.getContextPath() + "," + httpServletRequest.getServerName()); } } // go on with processing chain.doFilter(request, response); // clean up Session.unset(); } /** * @see javax.servlet.Filter#destroy() */ public void destroy() { } }