KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > ServerInterceptor


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ServerInterceptor.java
20  *
21  * This object is responsible for authenticating the incoming thread using the
22  * credential information supplied.
23  */

24
25 // package path
26
package com.rift.coad.lib.interceptor;
27
28 // java imports
29
import java.lang.reflect.Constructor JavaDoc;
30 import java.util.concurrent.ConcurrentHashMap JavaDoc;
31 import java.util.concurrent.ConcurrentMap JavaDoc;
32
33 // logging import
34
import org.apache.log4j.Logger;
35
36 // coadunation imports
37
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 /**
51  * This object is responsible for authenticating the incoming thread using the
52  * credential information supplied.
53  *
54  * @author Brett Chaldecott
55  */

56 public class ServerInterceptor {
57     
58     // class constants
59
public static String JavaDoc CREDENTIAL_AUTHENTICATOR = "credential_authenticator";
60     
61     // the class log variable
62
protected Logger log =
63             Logger.getLogger(ServerInterceptor.class.getName());
64     
65     // the class private member variables
66
private ConcurrentMap JavaDoc authenticators = new ConcurrentHashMap JavaDoc();
67     private InterceptorPermissionStack permissionStack = null;
68     private UserSessionManager userSessionManager = null;
69     private UserStoreManager userStoreManger = null;
70     
71     /**
72      * Creates a new instance of ServerInterceptor
73      *
74      * @param threadPermissionContainer The container responsible for
75      * controlling the threading permissions.
76      */

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     /**
88      * This method creates a new session using the credentials passed in.
89      *
90      * @param credential The credentials to create a new session for.
91      * @exception AuthorizationException
92      * @exception InterceptorException
93      */

94     public void createSession(Credential credential) throws
95             AuthorizationException, InterceptorException {
96         UserSession userSession = authenticate(credential);
97         permissionStack.push(userSession);
98     }
99     
100     
101     /**
102      * This method releases this thread, removing all user information from it.
103      *
104      * @exception InterceptorException
105      */

106     public void release() throws InterceptorException {
107         permissionStack.pop();
108     }
109     
110     
111     /**
112      * This method authenticates a thread using the credential object passed in.
113      *
114      * @return The reference to the user session object.
115      * @param credentials The credential to authenticate this call.
116      * @exception AuthorizationException
117      * @exception InterceptorException
118      */

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 JavaDoc 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 JavaDoc 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