001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ServerInterceptor.java
020: *
021: * This object is responsible for authenticating the incoming thread using the
022: * credential information supplied.
023: */
024:
025: // package path
026: package com.rift.coad.lib.interceptor;
027:
028: // java imports
029: import java.lang.reflect.Constructor;
030: import java.util.concurrent.ConcurrentHashMap;
031: import java.util.concurrent.ConcurrentMap;
032:
033: // logging import
034: import org.apache.log4j.Logger;
035:
036: // coadunation imports
037: import com.rift.coad.lib.configuration.Configuration;
038: import com.rift.coad.lib.configuration.ConfigurationFactory;
039: import com.rift.coad.lib.interceptor.credentials.Credential;
040: import com.rift.coad.lib.interceptor.credentials.Login;
041: import com.rift.coad.lib.interceptor.credentials.Session;
042: import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator;
043: import com.rift.coad.lib.security.AuthorizationException;
044: import com.rift.coad.lib.security.ThreadPermissionSession;
045: import com.rift.coad.lib.security.ThreadsPermissionContainer;
046: import com.rift.coad.lib.security.UserSession;
047: import com.rift.coad.lib.security.user.UserSessionManager;
048: import com.rift.coad.lib.security.user.UserStoreManager;
049:
050: /**
051: * This object is responsible for authenticating the incoming thread using the
052: * credential information supplied.
053: *
054: * @author Brett Chaldecott
055: */
056: public class ServerInterceptor {
057:
058: // class constants
059: public static String CREDENTIAL_AUTHENTICATOR = "credential_authenticator";
060:
061: // the class log variable
062: protected Logger log = Logger.getLogger(ServerInterceptor.class
063: .getName());
064:
065: // the class private member variables
066: private ConcurrentMap authenticators = new ConcurrentHashMap();
067: private ThreadsPermissionContainer permissionContainer = null;
068: private InterceptorPermissionStack permissionStack = null;
069: private UserSessionManager userSessionManager = null;
070: private UserStoreManager userStoreManger = null;
071:
072: /**
073: * Creates a new instance of ServerInterceptor
074: *
075: * @param threadPermissionContainer The container responsible for
076: * controlling the threading permissions.
077: */
078: protected ServerInterceptor(
079: ThreadsPermissionContainer threadPermissionContainer,
080: UserSessionManager userSessionManager,
081: UserStoreManager userStoreManger) {
082: permissionStack = new InterceptorPermissionStack(
083: threadPermissionContainer);
084: this .permissionContainer = threadPermissionContainer;
085: this .userSessionManager = userSessionManager;
086: this .userStoreManger = userStoreManger;
087: }
088:
089: /**
090: * This method overwrites the session attached to this thread.
091: *
092: * @return The user session of the authenticated user.
093: * @param credential The credentials to create a new session for.
094: * @exception AuthorizationException
095: * @exception InterceptorException
096: */
097: public UserSession setSession(Credential credential)
098: throws AuthorizationException, InterceptorException {
099: UserSession userSession = authenticate(credential);
100: permissionContainer.putSession(Thread.currentThread().getId(),
101: new ThreadPermissionSession(new Long(Thread
102: .currentThread().getId()), userSession));
103: return userSession;
104: }
105:
106: /**
107: * This method creates a new session using the credentials passed in.
108: *
109: * @return The user session of the authenticated user.
110: * @param credential The credentials to create a new session for.
111: * @exception AuthorizationException
112: * @exception InterceptorException
113: */
114: public UserSession createSession(Credential credential)
115: throws AuthorizationException, InterceptorException {
116: UserSession userSession = authenticate(credential);
117: permissionStack.push(userSession);
118: return userSession;
119: }
120:
121: /**
122: * This method releases this thread, removing all user information from it.
123: *
124: * @exception InterceptorException
125: */
126: public void release() throws InterceptorException {
127: permissionStack.pop();
128: }
129:
130: /**
131: * This method authenticates a thread using the credential object passed in.
132: *
133: * @return The reference to the user session object.
134: * @param credentials The credential to authenticate this call.
135: * @exception AuthorizationException
136: * @exception InterceptorException
137: */
138: private UserSession authenticate(Credential credential)
139: throws AuthorizationException, InterceptorException {
140: try {
141: InterceptorAuthenticator authenticator = null;
142: if (!authenticators.containsKey(credential.getClass())) {
143: Configuration config = ConfigurationFactory
144: .getInstance().getConfig(credential.getClass());
145: String className = config.getString(
146: CREDENTIAL_AUTHENTICATOR,
147: SessionAuthenticator.class.getName());
148: if (className.equals(SessionAuthenticator.class
149: .getName())) {
150: authenticator = new SessionAuthenticator(
151: userSessionManager, userStoreManger);
152: } else {
153: authenticator = (InterceptorAuthenticator) Class
154: .forName(className).newInstance();
155: }
156: authenticators
157: .put(credential.getClass(), authenticator);
158: } else {
159: authenticator = (InterceptorAuthenticator) authenticators
160: .get(credential.getClass());
161: }
162:
163: return authenticator.authenticate(credential);
164: } catch (AuthorizationException ex) {
165: throw ex;
166: } catch (InterceptorException ex) {
167: throw ex;
168: } catch (Exception ex) {
169: log.error("Failed to authenticate :" + ex.getMessage(), ex);
170: throw new InterceptorException("Failed to authenticate : "
171: + ex.getMessage(), ex);
172: }
173: }
174:
175: }
|