1 23 24 package com.rift.coad.lib.interceptor.authenticator; 26 27 import org.apache.log4j.Logger; 29 30 import com.rift.coad.lib.interceptor.InterceptorAuthenticator; 32 import com.rift.coad.lib.interceptor.InterceptorException; 33 import com.rift.coad.lib.interceptor.credentials.Credential; 34 import com.rift.coad.lib.interceptor.credentials.Session; 35 import com.rift.coad.lib.security.AuthorizationException; 36 import com.rift.coad.lib.security.UserSession; 37 import com.rift.coad.lib.security.user.UserException; 38 import com.rift.coad.lib.security.user.UserSessionManager; 39 import com.rift.coad.lib.security.user.UserStoreManager; 40 41 46 public class SessionAuthenticator implements 47 InterceptorAuthenticator { 48 49 protected Logger log = 51 Logger.getLogger(SessionAuthenticator.class.getName()); 52 53 private UserSessionManager userSessionManager = null; 55 private UserStoreManager userStoreManager = null; 56 57 61 private SessionAuthenticator() { 62 System.out.println("The call to the default constructor"); 63 } 64 65 72 public SessionAuthenticator(UserSessionManager 73 userSessionManager, UserStoreManager userStoreManager) { 74 this.userSessionManager = userSessionManager; 75 this.userStoreManager = userStoreManager; 76 } 77 78 79 88 public UserSession authenticate(Credential credential) throws 89 InterceptorException, AuthorizationException { 90 if (!(credential instanceof Session)) { 91 throw new InterceptorException( 92 "The session authenticator is only capable of dealling " + 93 "with session credentials."); 94 } 95 Session sessionCredential = (Session)credential; 96 UserSession userSession = null; 97 try { 98 userSession = (UserSession)(userSessionManager.getSessionById( 99 sessionCredential.getSessionId()).clone()); 100 } catch (UserException ex) { 101 try { 102 userSession = userStoreManager.getUserInfo( 103 sessionCredential.getUsername()); 104 userSession.setSessionId(sessionCredential.getSessionId()); 105 userSessionManager.addUserSession(userSession); 106 userSession = (UserSession)userSession.clone(); 107 } catch (Exception ex2) { 108 log.error("Failed to retrieve the session information : " + 109 ex2.getMessage(),ex2); 110 throw new InterceptorException( 111 "Failed to retrieve the session information : " + 112 ex2.getMessage(),ex2); 113 } 114 } catch (Exception ex) { 115 throw new InterceptorException( 116 "Failed to retrieve the session information : " + 117 ex.getMessage(),ex); 118 } 119 userSession.setPrincipals(sessionCredential.getPrincipals()); 120 return userSession; 121 } 122 } 123 | Popular Tags |