KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > authenticator > SessionAuthenticator


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  * SessionAuthenticator.java
20  *
21  * This class acts as the standard interceptor authenticator.
22  */

23
24 // package path
25
package com.rift.coad.lib.interceptor.authenticator;
26
27 // logging import
28
import org.apache.log4j.Logger;
29
30 // coadunation imports
31
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 /**
42  * This class acts as the standard interceptor authenticator.
43  *
44  * @author Brett Chaldecott
45  */

46 public class SessionAuthenticator implements
47         InterceptorAuthenticator {
48     
49     // the class log variable
50
protected Logger log =
51             Logger.getLogger(SessionAuthenticator.class.getName());
52     
53     // private member variables
54
private UserSessionManager userSessionManager = null;
55     private UserStoreManager userStoreManager = null;
56     
57     /**
58      * The private default constructor private so it cannot not be called
59      * by anyone
60      */

61     private SessionAuthenticator() {
62         System.out.println("The call to the default constructor");
63     }
64     
65     /**
66      * Creates a new instance of SessionAuthenticator
67      *
68      *
69      * @param userSessionManager The reference to the user session manager.
70      * @param userStoreManager Reference to the user store manager
71      */

72     public SessionAuthenticator(UserSessionManager
73             userSessionManager, UserStoreManager userStoreManager) {
74         this.userSessionManager = userSessionManager;
75         this.userStoreManager = userStoreManager;
76     }
77     
78     
79     /**
80      * This method returns a valid user session object for the supplied
81      * credentials
82      *
83      * @return UserSession The new user session for the given credentials.
84      * @param credentials The credentials for the user session.
85      * @exception InterceptorException
86      * @exception AuthorizationException
87      */

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