KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > acting > LoginAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.authentication.acting;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.acting.ServiceableAction;
24 import org.apache.cocoon.environment.Redirector;
25 import org.apache.cocoon.environment.SourceResolver;
26 import org.apache.cocoon.webapps.authentication.AuthenticationManager;
27 import org.apache.cocoon.webapps.authentication.user.UserHandler;
28 import org.apache.excalibur.source.SourceParameters;
29
30 /**
31  * This action logs the current user into a given handler. If the
32  * authentication is successful, a map is returned with the authentication
33  * information and a session is created (if it not already exists).
34  * If the authentication is not successful, the error information is stored
35  * into the temporary context.
36  *
37  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
38  * @version CVS $Id: LoginAction.java 53738 2004-10-04 19:03:53Z vgritsenko $
39 */

40 public final class LoginAction extends ServiceableAction
41                                implements ThreadSafe {
42
43     public Map JavaDoc act(Redirector redirector,
44                    SourceResolver resolver,
45                    Map JavaDoc objectModel,
46                    String JavaDoc source,
47                    Parameters par)
48     throws Exception JavaDoc {
49         if (getLogger().isDebugEnabled()) {
50             getLogger().debug("BEGIN act resolver=" + resolver +
51                               ", objectModel=" + objectModel +
52                               ", source=" + source +
53                               ", par=" + par);
54         }
55
56         final String JavaDoc handlerName = par.getParameter("handler", null);
57         if (handlerName == null) {
58             throw new ProcessingException("LoginAction requires at least the handler parameter.");
59         }
60
61         // Build authentication parameters
62
SourceParameters authenticationParameters = new SourceParameters();
63         String JavaDoc[] keys = par.getNames();
64         if (keys != null) {
65             for (int i = 0; i < keys.length; i++) {
66                 final String JavaDoc key = keys[i];
67                 if (key.startsWith("parameter_")) {
68                     authenticationParameters.setParameter(key.substring("parameter_".length()),
69                                                           par.getParameter(key));
70                 }
71             }
72         }
73
74         Map JavaDoc map = null;
75
76         // authenticate
77
AuthenticationManager authManager = null;
78         try {
79             authManager = (AuthenticationManager) this.manager.lookup(AuthenticationManager.ROLE);
80             UserHandler handler = authManager.login(handlerName,
81                                                     par.getParameter("application", null),
82                                                     authenticationParameters);
83             if (handler != null) {
84                 // success
85
map = handler.getContext().getContextInfo();
86             }
87         } finally {
88             this.manager.release(authManager);
89         }
90
91         if (getLogger().isDebugEnabled()) {
92             getLogger().debug("END act map=" + map);
93         }
94
95         return map;
96     }
97 }
98
Popular Tags