1 24 25 package com.rift.coad.lib.interceptor; 27 28 import java.lang.reflect.Constructor ; 30 import java.util.concurrent.ConcurrentHashMap ; 31 import java.util.concurrent.ConcurrentMap ; 32 33 import org.apache.log4j.Logger; 35 36 import com.rift.coad.lib.configuration.Configuration; 38 import com.rift.coad.lib.configuration.ConfigurationFactory; 39 import com.rift.coad.lib.interceptor.credentials.Credential; 40 import com.rift.coad.lib.interceptor.credentials.Login; 41 import com.rift.coad.lib.interceptor.credentials.Session; 42 import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator; 43 import com.rift.coad.lib.security.AuthorizationException; 44 import com.rift.coad.lib.security.ThreadsPermissionContainer; 45 import com.rift.coad.lib.security.UserSession; 46 import com.rift.coad.lib.security.user.UserSessionManager; 47 import com.rift.coad.lib.security.user.UserStoreManager; 48 49 50 56 public class ServerInterceptor { 57 58 public static String CREDENTIAL_AUTHENTICATOR = "credential_authenticator"; 60 61 protected Logger log = 63 Logger.getLogger(ServerInterceptor.class.getName()); 64 65 private ConcurrentMap authenticators = new ConcurrentHashMap (); 67 private InterceptorPermissionStack permissionStack = null; 68 private UserSessionManager userSessionManager = null; 69 private UserStoreManager userStoreManger = null; 70 71 77 protected ServerInterceptor(ThreadsPermissionContainer 78 threadPermissionContainer,UserSessionManager userSessionManager, 79 UserStoreManager userStoreManger) { 80 permissionStack = new InterceptorPermissionStack( 81 threadPermissionContainer); 82 this.userSessionManager = userSessionManager; 83 this.userStoreManger = userStoreManger; 84 } 85 86 87 94 public void createSession(Credential credential) throws 95 AuthorizationException, InterceptorException { 96 UserSession userSession = authenticate(credential); 97 permissionStack.push(userSession); 98 } 99 100 101 106 public void release() throws InterceptorException { 107 permissionStack.pop(); 108 } 109 110 111 119 private UserSession authenticate(Credential credential) throws 120 AuthorizationException, InterceptorException { 121 try { 122 InterceptorAuthenticator authenticator = null; 123 if (!authenticators.containsKey(credential.getClass())) { 124 Configuration config = ConfigurationFactory.getInstance(). 125 getConfig(credential.getClass()); 126 String className = config.getString(CREDENTIAL_AUTHENTICATOR, 127 SessionAuthenticator.class.getName()); 128 if (className.equals( 129 SessionAuthenticator.class.getName())) 130 { 131 authenticator = new SessionAuthenticator( 132 userSessionManager, userStoreManger); 133 } 134 else 135 { 136 authenticator = (InterceptorAuthenticator)Class. 137 forName(className).newInstance(); 138 } 139 authenticators.put(credential.getClass(),authenticator); 140 } else { 141 authenticator = (InterceptorAuthenticator)authenticators.get( 142 credential.getClass()); 143 } 144 145 return authenticator.authenticate(credential); 146 } catch (AuthorizationException ex) { 147 throw ex; 148 } catch (InterceptorException ex) { 149 throw ex; 150 } catch (Exception ex) { 151 log.error("Failed to authenticate :" + ex.getMessage(),ex); 152 throw new InterceptorException("Failed to authenticate : " + 153 ex.getMessage(),ex); 154 } 155 } 156 157 158 159 } 160 | Popular Tags |