Browse Source

Refactored out safeGetHttpSession method to remove multiple try/catch IllegalArgumentException blocks round request.getSession() calls.

Luke Taylor 17 years ago
parent
commit
d0ae8e072d

+ 13 - 21
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 {