From d0ae8e072d68bfa76101b207a1175673dbe7d647 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sat, 12 Apr 2008 15:01:52 +0000 Subject: [PATCH] Refactored out safeGetHttpSession method to remove multiple try/catch IllegalArgumentException blocks round request.getSession() calls. --- .../HttpSessionContextIntegrationFilter.java | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/springframework/security/context/HttpSessionContextIntegrationFilter.java b/core/src/main/java/org/springframework/security/context/HttpSessionContextIntegrationFilter.java index 3af1e239f6..78bc9b9a06 100644 --- a/core/src/main/java/org/springframework/security/context/HttpSessionContextIntegrationFilter.java +++ b/core/src/main/java/org/springframework/security/context/HttpSessionContextIntegrationFilter.java @@ -188,16 +188,8 @@ public class HttpSessionContextIntegrationFilter extends SpringSecurityFilter im return; } - HttpSession httpSession = null; - - try { - httpSession = request.getSession(forceEagerSessionCreation); - } - catch (IllegalStateException ignored) { - } - + HttpSession httpSession = safeGetSession(request, forceEagerSessionCreation); boolean httpSessionExistedAtStartOfRequest = httpSession != null; - SecurityContext contextBeforeChainExecution = readSecurityContextFromSession(httpSession); // Make the HttpSession null, as we don't want to keep a reference to it lying @@ -346,13 +338,7 @@ public class HttpSessionContextIntegrationFilter extends SpringSecurityFilter im HttpServletRequest request, boolean httpSessionExistedAtStartOfRequest, int contextHashBeforeChainExecution) { - HttpSession httpSession = null; - - try { - httpSession = request.getSession(false); - } - catch (IllegalStateException ignored) { - } + HttpSession httpSession = safeGetSession(request, false); if (httpSession == null) { if (httpSessionExistedAtStartOfRequest) { @@ -375,11 +361,8 @@ public class HttpSessionContextIntegrationFilter extends SpringSecurityFilter im logger.debug("HttpSession being created as SecurityContextHolder contents are non-default"); } - try { - httpSession = request.getSession(true); - } - catch (IllegalStateException ignored) { - } + httpSession = safeGetSession(request, true); + } else { if (logger.isDebugEnabled()) { logger.debug("HttpSession is null, but SecurityContextHolder has not changed from default: ' " @@ -400,6 +383,15 @@ public class HttpSessionContextIntegrationFilter extends SpringSecurityFilter im } } } + + private HttpSession safeGetSession(HttpServletRequest request, boolean allowCreate) { + try { + return request.getSession(allowCreate); + } + catch (IllegalStateException ignored) { + return null; + } + } public SecurityContext generateNewContext() throws ServletException { try {