1 19 20 package com.sslexplorer.policyframework; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import com.sslexplorer.security.Constants; 32 import com.sslexplorer.security.SessionInfo; 33 import com.sslexplorer.security.SessionInfoListener; 34 import com.sslexplorer.security.User; 35 import com.sslexplorer.util.TicketGenerator; 36 37 38 43 public class LaunchSessionManager { 44 45 46 48 private Map <User, List <LaunchSession>> userSessionsList = new HashMap <User, List <LaunchSession>>(); 49 private Map <String , LaunchSession> launchSessions = new HashMap <String , LaunchSession>(); 50 51 57 public Collection <LaunchSession> getLaunchSessions(User user) { 58 return userSessionsList.get(user); 59 } 60 61 66 public Collection <LaunchSession> getLaunchSession() { 67 return launchSessions.values(); 68 } 69 70 80 public LaunchSession getLaunchSession(String launchId) { 81 return (LaunchSession)launchSessions.get(launchId); 82 } 83 84 94 public LaunchSession getLaunchSession(SessionInfo session, String launchId) { 95 SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS); 96 return launchSessions == null ? null : launchSessions.get(launchId); 97 } 98 99 104 public void removeLaunchSession(LaunchSession launchSession) { 105 synchronized (launchSessions) { 106 launchSessions.remove(launchSession.getId()); 107 SessionsLaunchSessions sessionLaunchSessions = (SessionsLaunchSessions)launchSession.getSession().getAttribute(Constants.LAUNCH_SESSIONS); 108 if(sessionLaunchSessions != null) { 109 sessionLaunchSessions.remove(launchSession.getId()); 110 if(sessionLaunchSessions.size() == 0) { 111 launchSession.getSession().removeAttribute(Constants.LAUNCH_SESSIONS); 112 } 113 } 114 115 } 116 117 synchronized(userSessionsList) { 118 List <LaunchSession> us = userSessionsList.get(launchSession.getSession().getUser()); 119 if(us != null) { 120 us.remove(launchSession); 121 if(us.size() == 0) { 122 userSessionsList.remove(launchSession.getSession().getUser()); 123 } 124 } 125 } 126 } 127 128 136 public LaunchSession createLaunchSession(SessionInfo session, Resource resource, Policy policy) { 137 138 String launchId = null; 140 LaunchSession launchSession = null; 141 synchronized(launchSessions) { 142 143 while(true) { 144 launchId = TicketGenerator.getInstance().generateUniqueTicket("l", 7); 145 if(!launchSessions.containsKey(launchId)) { 146 launchSession = new LaunchSession(launchId, session, resource, policy); 147 launchSessions.put(launchId, launchSession); 148 break; 149 } 150 } 151 } 152 153 SessionsLaunchSessions sessionLaunchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS); 155 if (sessionLaunchSessions == null) { 156 sessionLaunchSessions = new SessionsLaunchSessions(launchSession); 157 session.setAttribute(Constants.LAUNCH_SESSIONS, sessionLaunchSessions); 158 } 159 sessionLaunchSessions.put(launchId, launchSession); 160 161 162 synchronized(userSessionsList) { 164 List <LaunchSession> us = userSessionsList.get(session.getUser()); 165 if(us == null) { 166 us = new ArrayList <LaunchSession>(); 167 userSessionsList.put(session.getUser(), us); 168 } 169 us.add(launchSession); 170 } 171 172 return launchSession; 173 } 174 175 182 public Collection <LaunchSession> getLaunchSessionsForType(SessionInfo session, ResourceType resourceType) { 183 SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS); 184 List <LaunchSession> l = new ArrayList <LaunchSession>(); 185 if (launchSessions != null) { 186 for (LaunchSession launchSession : launchSessions.values()) { 187 if (launchSession.getResource() != null && launchSession.getResource().getResourceType() == resourceType) { 188 l.add(launchSession); 189 } 190 } 191 } 192 return l; 193 } 194 195 196 203 public LaunchSession getFirstLaunchSessionForResource(SessionInfo session, Resource resource) { 204 LaunchSession launchSession = null; 205 Iterator <LaunchSession> i = getLaunchSessionsForResource(session, resource).iterator(); 206 if(i.hasNext()) { 207 launchSession = i.next(); 208 } 209 return launchSession; 210 } 211 212 213 220 public Collection <LaunchSession> getLaunchSessionsForResource(SessionInfo session, Resource resource) { 221 SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS); 222 List <LaunchSession> l = new ArrayList <LaunchSession>(); 223 if (launchSessions != null) { 224 for (LaunchSession launchSession : launchSessions.values()) { 225 if (launchSession.getResource() != null && launchSession.getResource().equals(resource)) { 226 l.add(launchSession); 227 } 228 } 229 } 230 return l; 231 } 232 233 248 public static Policy getLaunchRequestPolicy(HttpServletRequest request, SessionInfo session, Resource resource) 249 throws IllegalArgumentException , Exception { 250 try { 251 253 257 int policyId = request==null ? -1 : Integer.parseInt(String.valueOf(request.getParameter("policy"))); 258 Policy policy = policyId == -1 ? PolicyDatabaseFactory.getInstance().getGrantingPolicyForUser(session.getUser(), resource) 259 : PolicyDatabaseFactory.getInstance().getPolicy(policyId); 260 if (policy == null) { 261 if(policyId == -1) { 262 List l = PolicyDatabaseFactory.getInstance().getPoliciesAttachedToResource(resource, 263 session.getUser().getRealm()); 264 if(l.size() == 0) 265 throw new Exception ("Resource is not attached to any policies."); 266 return (Policy)l.get(0); 267 } 268 throw new IllegalArgumentException ("No policy with ID of " + policy); 269 } 270 return policy; 271 } catch (NumberFormatException nfe) { 272 throw new IllegalArgumentException ("No policy ID provided."); 273 } 274 } 275 276 class SessionsLaunchSessions extends HashMap <String , LaunchSession> implements SessionInfoListener { 277 278 private LaunchSession launchSession; 279 280 SessionsLaunchSessions(LaunchSession launchSession) { 281 this.launchSession = launchSession; 282 } 283 284 public void invalidated() { 285 synchronized(userSessionsList) { 286 List <LaunchSession> l = userSessionsList.get(launchSession.getSession().getUser()); 287 for(LaunchSession r : values()) { 288 l.remove(r); 289 launchSessions.remove(r.getId()); 290 } 291 if(l.size() == 0) { 292 userSessionsList.remove(launchSession.getSession().getUser()); 293 } 294 } 295 } 296 } 297 } 298 | Popular Tags |