Michael Pasternak has uploaded a new change for review.

Change subject: utils: [WIP] HttpSession sharing between contexts
......................................................................

utils: [WIP] HttpSession sharing between contexts

         *** DO NOT SUBMIT ***

 This patch is for educational purposes only

         *** DO NOT SUBMIT ***

Change-Id: I5cbd81f296b9595a3e6c1c6e4a577485ccdd05c8
Signed-off-by: Michael pasternak <mpast...@redhat.com>
---
M 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/Challenger.java
M 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/SessionUtils.java
2 files changed, 62 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/59/21559/1

diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/Challenger.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/Challenger.java
index 119ef03..73b8807 100644
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/Challenger.java
+++ 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/Challenger.java
@@ -192,6 +192,7 @@
                     httpSession = getCurrentSession(true);
                 }
                 SessionUtils.setEngineSessionId(httpSession, engineSessionId);
+                SessionUtils.persistSessionInTheServletContext(httpSession);
                 updateAuthenticationProperties(preferPersistentAuth, 
principal);
             }
         }
diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/SessionUtils.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/SessionUtils.java
index cdbafd0..d022a7a 100644
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/SessionUtils.java
+++ 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/security/auth/SessionUtils.java
@@ -1,9 +1,11 @@
 package org.ovirt.engine.api.common.security.auth;
 
+import java.util.Hashtable;
 import java.util.List;
 import java.util.UUID;
 
 import javax.security.jacc.PolicyContextException;
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import javax.ws.rs.core.HttpHeaders;
@@ -55,6 +57,9 @@
         if (request != null) {
             retVal = request.getSession(create);
         }
+        if (retVal == null && !create) {
+            retVal = SessionUtils.fetchSessionFromTheServletContext();
+        }
         return retVal;
     }
 
@@ -81,4 +86,60 @@
     public static String generateEngineSessionId() {
         return UUID.randomUUID().toString();
     }
+
+    /**
+     * Persists session in ServletContext for cross-context access.
+     *
+     * @param httpSession
+     *            session to persist
+     */
+    public static synchronized void 
persistSessionInTheServletContext(HttpSession httpSession) {
+        HttpServletRequest request = getCurrentHttpServletRequest();
+        ServletContext context = request.getServletContext();
+
+        // load the shared_context from the servlet context
+        @SuppressWarnings("unchecked")
+        Hashtable<String, Object> shareddata =
+                (Hashtable<String, Object>) 
context.getAttribute("shared_context");
+
+        // if not yet available, create a new cahce
+        if (shareddata == null) {
+            shareddata = new Hashtable<String, Object>();
+        }
+
+        // store the httpSession in cache
+        shareddata.put("HttpSession", httpSession);
+
+        // store the shared_context back into the servlet context
+        context.setAttribute("shared_context", shareddata);
+    }
+
+    /**
+     * Fetches session from ServletContext based on cross-context access.
+     *
+     * @return {@link HttpSession}
+     */
+    public static HttpSession fetchSessionFromTheServletContext() {
+
+        HttpSession session = null;
+
+        HttpServletRequest request = getCurrentHttpServletRequest();
+        ServletContext context = request.getServletContext();
+
+        // get the context containing the shared_context data
+        ServletContext sharedContext = context.getContext("/api");
+
+        if (sharedContext != null) {
+            // read the shared_context
+            @SuppressWarnings("unchecked")
+            Hashtable<String, Object> shareddata =
+                    (Hashtable<String, Object>) 
sharedContext.getAttribute("shared_context");
+
+            if (shareddata != null) {
+                // get the HttpSession from the shared_context
+                session = (HttpSession) shareddata.get("HttpSession");
+            }
+        }
+        return session;
+    }
 }


-- 
To view, visit http://gerrit.ovirt.org/21559
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5cbd81f296b9595a3e6c1c6e4a577485ccdd05c8
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Michael Pasternak <mpast...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to