KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wrappers > catalina > SlideRealm


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/wrappers/wrappers/catalina/SlideRealm.java,v 1.3.2.1 2004/08/31 15:33:00 unico Exp $
3  * $Revision: 1.3.2.1 $
4  * $Date: 2004/08/31 15:33:00 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package wrappers.catalina;
25
26 import java.security.Principal JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32 import org.apache.catalina.Container;
33 import org.apache.catalina.LifecycleException;
34 import org.apache.catalina.Logger;
35 import org.apache.catalina.Realm;
36 import org.apache.catalina.realm.RealmBase;
37 import org.apache.catalina.util.LifecycleSupport;
38 import org.apache.catalina.util.StringManager;
39
40 import org.apache.slide.common.NamespaceAccessToken;
41 import org.apache.slide.common.Domain;
42 import org.apache.slide.common.SlideToken;
43 import org.apache.slide.common.SlideTokenImpl;
44 import org.apache.slide.common.SlideException;
45 import org.apache.slide.content.Content;
46 import org.apache.slide.content.NodeRevisionDescriptors;
47 import org.apache.slide.content.NodeRevisionDescriptor;
48 import org.apache.slide.content.NodeProperty;
49 import org.apache.slide.structure.ObjectNode;
50 import org.apache.slide.authenticate.CredentialsToken;
51 import org.apache.slide.authenticate.SecurityToken;
52 import org.apache.slide.security.Security;
53
54
55 /**
56  * Implemetation of a Catalina realm which authenticates users defined
57  * in a Slide namespace.
58  * <p>
59  * The namespace used will have the same name as the container to which the
60  * realm is associated. If such a namespace doesn't exist, it falls back
61  * to tomcat, webdav or default.
62  *
63  * @version $Revision: 1.3.2.1 $ $Date: 2004/08/31 15:33:00 $
64  */

65
66 public final class SlideRealm
67     extends RealmBase {
68
69
70     // ----------------------------------------------------- Instance Variables
71

72
73     /**
74      * Descriptive information about this Realm implementation.
75      */

76     private static final String JavaDoc info =
77     "wrappers.catalina.SlideRealm/1.2";
78
79
80     /**
81      * Set of access tokens.
82      */

83     private NamespaceAccessToken accessToken;
84
85
86     /**
87      * Content helper.
88      */

89     private Content contentHelper;
90
91
92     /**
93      * Security halper.
94      */

95     private Security securityHelper;
96
97
98     /**
99      * Users path.
100      */

101     private String JavaDoc usersPath;
102
103
104     /**
105      * Root credentials token.
106      */

107     private CredentialsToken rootCredentials;
108
109
110     /**
111      * Namepsace to which this realm will connect.
112      */

113     private String JavaDoc namespace;
114
115
116     // ------------------------------------------------------------- Properties
117

118
119     /**
120      * Set the namespace name to which this realm will connect.
121      */

122     public void setNamespace(String JavaDoc namespace) {
123         this.namespace = namespace;
124     }
125
126
127     /**
128      * Set the namespace access token used by this realm.
129      */

130     public void setAccessToken(NamespaceAccessToken accessToken) {
131         this.accessToken = accessToken;
132     }
133
134
135     /**
136      * Get name.
137      */

138     public String JavaDoc getName() {
139         return "Slide realm";
140     }
141
142
143     // --------------------------------------------------------- Public Methods
144

145
146     /**
147      * Return <code>true</code> if the specified Principal has the specified
148      * security role, within the context of this Realm; otherwise return
149      * <code>false</code>.
150      *
151      * @param principal Principal for whom the role is to be checked
152      * @param role Security role to be checked
153      */

154     public boolean hasRole(Principal JavaDoc principal, String JavaDoc role) {
155
156         CredentialsToken credToken = new CredentialsToken(principal);
157         SlideToken slideToken = new SlideTokenImpl(credToken);
158         try {
159             return securityHelper.hasRole(slideToken, role);
160         } catch (SlideException e) {
161             return (false);
162         }
163
164     }
165
166
167     /**
168      * Start the realm.
169      */

170     public void start() throws LifecycleException {
171
172         super.start();
173
174         if (namespace == null)
175             namespace = container.getName();
176
177         if (accessToken == null)
178             accessToken = Domain.accessNamespace
179                 (new SecurityToken(container), namespace);
180
181         if (accessToken == null)
182             throw new IllegalStateException JavaDoc
183                 ("Invalid Slide Realm configuration : "
184                  + "Couldn't access namespace " + namespace);
185
186         contentHelper = accessToken.getContentHelper();
187         securityHelper = accessToken.getSecurityHelper();
188
189         usersPath = accessToken.getNamespaceConfig().getUsersPath();
190
191     }
192
193
194     // ------------------------------------------------------ Protected Methods
195

196
197     /**
198      * Return the password associated with the given principal's user name.
199      */

200     protected String JavaDoc getPassword(String JavaDoc username) {
201         
202         Principal JavaDoc userPrincipal = getPrincipal(username);
203         CredentialsToken credToken = new CredentialsToken(userPrincipal);
204         SlideToken slideToken = new SlideTokenImpl(credToken);
205         
206         // Fetch the Slide object representing the user.
207
try {
208             
209             ObjectNode user = securityHelper.getPrincipal(slideToken);
210             
211         } catch (SlideException e) {
212             return null;
213         }
214         
215         String JavaDoc passwordValue = null;
216         
217         try {
218             
219             NodeRevisionDescriptors revisionDescriptors =
220                 contentHelper.retrieve(slideToken, usersPath + "/" + username);
221             NodeRevisionDescriptor revisionDescriptor =
222                 contentHelper.retrieve(slideToken, revisionDescriptors);
223             NodeProperty password =
224                 revisionDescriptor.getProperty
225                 ("password", NodeProperty.SLIDE_NAMESPACE);
226             if (password != null) {
227                 passwordValue = (String JavaDoc) password.getValue();
228             }
229             
230         } catch (SlideException e) {
231             // Whatever happens doesn't really matter
232
// The stack trace is displayed for now for debug purposes
233
}
234         
235         if (passwordValue == null) {
236             log("User " + username
237                 + " doesn't have his password property set : "
238                 + "can't authenticate");
239         }
240         
241         return passwordValue;
242         
243     }
244
245
246     /**
247      * Return the Principal associated with the given user name.
248      */

249     protected Principal JavaDoc getPrincipal(String JavaDoc username) {
250     return new SlideRealmPrincipal(username);
251     }
252
253
254 }
255
256
257 /**
258  * Private class representing an individual user's Principal object.
259  */

260
261 final class SlideRealmPrincipal implements Principal JavaDoc, Serializable JavaDoc {
262
263     /**
264      * The username for this Principal.
265      */

266     private String JavaDoc username = null;
267
268
269     /**
270      * Construct a new MemoryRealmPrincipal instance.
271      *
272      * @param username The username for this Principal
273      */

274     public SlideRealmPrincipal(String JavaDoc username) {
275
276     this.username = username;
277
278     }
279
280
281     /**
282      * Return the name of this Principal.
283      */

284     public String JavaDoc getName() {
285
286     return (username);
287
288     }
289
290
291 }
292
293
294
Popular Tags