Coverage Report - org.webslinger.servlet.EmbeddedHttpSessionManager
 
Classes in this File Line Coverage Branch Coverage Complexity
EmbeddedHttpSessionManager
94%
29/31
100%
7/7
0
 
 1  
 package org.webslinger.servlet;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.Map;
 5  
 import javax.servlet.http.HttpServletRequest;
 6  
 import javax.servlet.http.HttpServletResponse;
 7  
 import javax.servlet.http.HttpSession;
 8  
 
 9  
 public class EmbeddedHttpSessionManager implements HttpSessionIdManager, HttpSessionStore {
 10  1
     public static final String SESSION_KEY = EmbeddedHttpSessionManager.class.getName() + ":sessions";
 11  
 
 12  
     protected final String id;
 13  
 
 14  26
     public EmbeddedHttpSessionManager(String id) {
 15  26
         this.id = id;
 16  26
     }
 17  
 
 18  
     public HttpSessionId extractHttpSessionId(HttpServletRequest[] request, HttpServletResponse[] response) {
 19  
         HttpSessionId.Location location;
 20  307
         if (request[0].isRequestedSessionIdFromCookie()) {
 21  176
             location = HttpSessionId.Location.COOKIE;
 22  131
         } else if (request[0].isRequestedSessionIdFromURL()) {
 23  0
             location = HttpSessionId.Location.URL;
 24  
         } else {
 25  131
             location = HttpSessionId.Location.OTHER;
 26  
         }
 27  307
         return new HttpSessionId(id, location);
 28  
     }
 29  
 
 30  
     public HttpSessionId createHttpSessionId() {
 31  0
         throw new UnsupportedOperationException();
 32  
     }
 33  
 
 34  
     public String encodeURL(String path, HttpSessionId id) {
 35  18
         return path;
 36  
     }
 37  
 
 38  
     public String encodeRedirectURL(String path, HttpSessionId id) {
 39  23
         return path;
 40  
     }
 41  
 
 42  
     public HttpSessionData getSessionData(HttpServletRequest request, HttpSessionId id, boolean create) {
 43  134
         HttpSession session = request.getSession(create);
 44  134
         if (session == null) return null;
 45  63
         return getSessionData(request, session, id, create);
 46  
     }
 47  
 
 48  
     public static HttpSessionData getSessionData(HttpServletRequest request, HttpSession session, HttpSessionId id, boolean create) {
 49  
         Map<String, HttpSessionData> nestedSessions;
 50  63
         synchronized (session) {
 51  63
             nestedSessions = (Map<String, HttpSessionData>) session.getAttribute(SESSION_KEY);
 52  63
             if (nestedSessions == null) {
 53  16
                 if (!create) return null;
 54  16
                 nestedSessions = new HashMap<String, HttpSessionData>();
 55  16
                 session.setAttribute(SESSION_KEY, nestedSessions);
 56  
             }
 57  63
         }
 58  
         HttpSessionData data;
 59  63
         synchronized (nestedSessions) {
 60  63
             data = nestedSessions.get(id.getId());
 61  63
             if (data == null) {
 62  16
                 if (!create) return null;
 63  16
                 data = new HttpSessionData(id.getId(), request.getRemoteAddr());
 64  16
                 nestedSessions.put(id.getId(), data);
 65  
             }
 66  63
         }
 67  63
         return data;
 68  
     }
 69  
 }