Author: ivaynberg
Date: Tue May 11 17:38:04 2010
New Revision: 943201
URL: http://svn.apache.org/viewvc?rev=943201&view=rev
Log:
WICKET-2858 Fix bug in WicketSessionFilter
Issue: WICKET-2858
Modified:
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java
Modified:
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java
URL:
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java?rev=943201&r1=943200&r2=943201&view=diff
==============================================================================
---
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java
(original)
+++
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java
Tue May 11 17:38:04 2010
@@ -104,7 +104,7 @@ import org.slf4j.LoggerFactory;
public class WicketSessionFilter implements Filter
{
/** log. */
- private static final Logger log =
LoggerFactory.getLogger(WicketSessionFilter.class);
+ private static final Logger logger =
LoggerFactory.getLogger(WicketSessionFilter.class);
/** the filter name/ application key. */
private String filterName;
@@ -133,8 +133,7 @@ public class WicketSessionFilter impleme
getClass().getName());
}
- log.debug("filterName/ application key set to {}", filterName);
-
+ logger.debug("filterName/application key set to {}",
filterName);
}
/**
@@ -144,80 +143,83 @@ public class WicketSessionFilter impleme
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
- HttpServletRequest httpServletRequest =
((HttpServletRequest)request);
- HttpSession httpSession = httpServletRequest.getSession(false);
- WebApplication application = null;
- Session session = null;
- if (httpSession != null)
+ try
{
- if (sessionKey == null)
- {
- application =
(WebApplication)Application.get(filterName);
- if (application == null)
- {
- throw new IllegalStateException(
- "Could not find wicket
application mapped to filter: " +
- filterName +
- ". Make sure you set
filterName attribute to the name of the wicket filter " +
- "for the wicket
application whose session you want to access.");
- }
-
- sessionKey =
application.getSessionAttributePrefix(null, filterName) +
- Session.SESSION_ATTRIBUTE_NAME;
-
- log.debug("will use {} as the session key to
get the Wicket session", sessionKey);
- }
+ WebApplication application = bindApplication();
+ bindSession(request, application);
+ chain.doFilter(request, response);
+ }
+ finally
+ {
+ cleanupBoundApplicationAndSession();
+ }
+ }
- session = (Session)httpSession.getAttribute(sessionKey);
+ private void cleanupBoundApplicationAndSession()
+ {
+ try
+ {
+ Session.unset();
}
- else
+ finally
{
- log.debug("could not set Wicket session: no http
session was created yet for {},{}",
- httpServletRequest.getContextPath(),
httpServletRequest.getServerName());
+ Application.unset();
}
+ }
+
+ private void bindSession(ServletRequest request, WebApplication
application)
+ {
+ // find wicket session and bind it to thread
+ HttpSession httpSession =
((HttpServletRequest)request).getSession(false);
+ Session session = getSession(httpSession, application);
if (session == null)
{
- // no session found
-
- if (log.isDebugEnabled())
+ if (logger.isDebugEnabled())
{
- log.debug("could not set Wicket session: key "
+ sessionKey +
- " not found in http session for " +
httpServletRequest.getContextPath() + "," +
- httpServletRequest.getServerName());
+ logger.debug("could not set Wicket session: key
" + sessionKey +
+ " not found in http session for " +
+
((HttpServletRequest)request).getContextPath() + "," + request.getServerName() +
+ ", or http session does not exist");
}
-
- // go on with processing
- chain.doFilter(request, response);
}
else
{
- // session found
+ session.bind();
+ }
+ }
- Application.set(application);
- try
- {
- Session.set(session);
- try
- {
- log.debug("session " + session + " set
as current for " +
-
httpServletRequest.getContextPath() + "," +
-
httpServletRequest.getServerName());
-
- // go on with processing
- chain.doFilter(request, response);
- }
- finally
- {
- Session.unset();
- }
+ private WebApplication bindApplication()
+ {
+ // find wicket application and bind it to thread
- }
- finally
+ WebApplication application =
(WebApplication)Application.get(filterName);
+ if (application == null)
+ {
+ throw new IllegalStateException("Could not find wicket
application mapped to filter: " +
+ filterName +
+ ". Make sure you set filterName attribute to
the name of the wicket filter " +
+ "for the wicket application whose session you
want to access.");
+ }
+ Application.set(application);
+ return application;
+ }
+
+ private Session getSession(HttpSession session, WebApplication
application)
+ {
+ if (session != null)
+ {
+ if (sessionKey == null)
{
- Application.unset();
+ sessionKey =
application.getSessionAttributePrefix(null, filterName) +
+ Session.SESSION_ATTRIBUTE_NAME;
+
+ logger.debug("will use {} as the session key to
get the Wicket session", sessionKey);
}
+
+ return (Session)session.getAttribute(sessionKey);
}
+ return null;
}
/**