KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > interceptors > PersistentLoginInterceptor


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.interceptors;
21
22 import org.apache.commons.httpclient.Cookie;
23
24 import za.org.coefficient.core.CoefficientInterceptor;
25 import za.org.coefficient.interfaces.CoefficientContext;
26 import za.org.coefficient.util.common.InvokerFactory;
27
28 /**
29  *
30  * @version $Revision: 1.6 $ $Date: 2004/10/27 10:26:18 $
31  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
32  */

33 public class PersistentLoginInterceptor extends CoefficientInterceptor {
34     //~ Static fields/initializers =============================================
35

36     private static final String JavaDoc CHECKED_FOR_COOKIES = "__checked_for_cookies";
37
38     //~ Methods ================================================================
39

40     protected int handleInvoke(CoefficientContext ctx) throws Exception JavaDoc {
41         int retVal = INVOKE_PROCESS_CHILD;
42         String JavaDoc mod = ctx.getParameter("module", "");
43         String JavaDoc op = ctx.getParameter("op", "");
44         if ((ctx.getCurrentUser() == null)
45             && (ctx.getSessionAttribute(CHECKED_FOR_COOKIES) == null)
46             && (!mod.equalsIgnoreCase("security") && !op.equals("login"))) {
47             // check for cookies and login if found
48
Cookie[] cookies = ctx.getCookies();
49             if (cookies != null) {
50                 String JavaDoc username = null;
51                 String JavaDoc password = null;
52                 for (int i = 0; i < cookies.length; i++) {
53                     Cookie cookie = cookies[i];
54                     if ("coefficient_username".equals(cookie.getName())) {
55                         username = cookie.getValue();
56                         // Don't pass on cookies that are empty
57
if(username != null && username.trim().equals("")) {
58                             username = null;
59                         }
60                     }
61                     if ("coefficient_password".equals(cookie.getName())) {
62                         password = cookie.getValue();
63                         // Don't pass on cookies that are empty
64
if(password != null && password.trim().equals("")) {
65                             password = null;
66                         }
67                     }
68                 }
69                 if ((username != null) && (password != null)) {
70                     ctx.setRequestAttribute("password", password);
71                     ctx.setRequestAttribute("username", username);
72                     ctx.setRequestAttribute("hashedPassword", new Boolean JavaDoc(true));
73                     Object JavaDoc context = InvokerFactory.getRemoteInvoker()
74                         .invokeOpOnModule("Security", "login", ctx);
75                     if(context instanceof CoefficientContext) {
76                         ctx = (CoefficientContext)context;
77                     }
78                     ctx.removeRequestAttribute("password");
79                     ctx.removeRequestAttribute("username");
80                     ctx.removeRequestAttribute("hashedPassword");
81                 }
82             }
83             ctx.setSessionAttribute(CHECKED_FOR_COOKIES, new Boolean JavaDoc(true));
84         }
85
86         // else do nothing
87
return retVal;
88     }
89 }
90
Popular Tags