KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > ex1 > LoginServices


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: LoginServices.java,v 1.4 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.examples.ex1;
21
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.enhydra.barracuda.core.event.*;
27 import org.enhydra.barracuda.plankton.http.*;
28
29 /**
30  * This class provides basic Login services, specifically:
31  * - validateUserPwd (either from HttpReq, HttpSession,
32  * or based on a specific user/pwd combo)
33  */

34 public class LoginServices {
35
36     //session constants used throughout the app
37
public static final String JavaDoc NUMBER_OF_VISITS = LoginServices.class.getName()+".NumberOfVisits"; //int
38
public static final String JavaDoc AUTO_LOGIN = LoginServices.class.getName()+".AutoLogin"; //boolean
39
public static final String JavaDoc USER = "User"; //String
40
public static final String JavaDoc PASSWORD = "Password"; //String
41

42     //validateUserPwd results
43
public static final int VALID = 0;
44     public static final int INVALID_USER = 1;
45     public static final int INVALID_PASSWORD = 2;
46     public static final int INVALID_DATA_MISSING = 3;
47     public static final int INVALID_SESSION_EXPIRED = 4;
48     public static final int INVALID_OTHER = 99;
49
50     /**
51      * Convert validation results into text
52      */

53     public static String JavaDoc xrefValidity(int valid) {
54         if (valid==VALID) return "VALID";
55         else if (valid==INVALID_USER) return "INVALID_USER";
56         else if (valid==INVALID_PASSWORD) return "INVALID_PASSWORD";
57         else if (valid==INVALID_DATA_MISSING) return "INVALID_DATA_MISSING";
58         else if (valid==INVALID_SESSION_EXPIRED) return "INVALID_SESSION_EXPIRED";
59         else if (valid==INVALID_OTHER) return "INVALID_OTHER";
60         else return "???";
61     }
62
63     /**
64      * Validate user/pwd, getting the information from the
65      * HttpServletRequest object
66      */

67     public static int validateUserPwd(HttpServletRequest req, boolean updateSession) throws EventException {
68         //get the login information from the request
69
String JavaDoc user = req.getParameter(USER);
70         String JavaDoc password = req.getParameter(PASSWORD);
71
72         //update this info in the session if requested
73
if (updateSession) {
74             HttpSession session = SessionServices.getSession(req);
75             session.setAttribute(USER, ""+user);
76             session.setAttribute(PASSWORD, ""+password);
77         }
78
79         //this is kind of a hokey little way to catch our exception (which is just
80
//for demo purposes anyway) and clean up after ourselves by removing the
81
//user info fromt he session, so the error does not continue indefinitely)
82
int result = INVALID_OTHER;
83         try {result = validateUserPwd(user, password);}
84         catch (WhoYouCallingKlausException e) {
85             if (updateSession) {
86                 HttpSession session = SessionServices.getSession(req);
87                 session.setAttribute(USER, null);
88                 session.setAttribute(PASSWORD, null);
89             }
90             throw e;
91         }
92         return result;
93     }
94
95     /**
96      * Validate user/pwd, getting the information from the
97      * HttpSession object
98      */

99     public static int validateUserPwd(HttpSession session) throws EventException {
100         //make sure the session hasn't expired
101

102
103         //get the login information from the request
104
String JavaDoc user = (String JavaDoc) session.getAttribute(USER);
105         String JavaDoc password = (String JavaDoc) session.getAttribute(PASSWORD);
106         return validateUserPwd(user, password);
107     }
108
109     /**
110      * Validate user/pwd
111      */

112     public static int validateUserPwd(String JavaDoc user, String JavaDoc password) throws EventException {
113         //make sure the data is there
114
if (user==null || password==null) return INVALID_DATA_MISSING;
115
116         //validate the login information
117
if (user.equals("santa")) return INVALID_OTHER;
118         if (user.equals("klaus")) throw new WhoYouCallingKlausException();
119         if (!user.equals("foo")) return INVALID_USER;
120         if (!password.equals("bar")) return INVALID_PASSWORD;
121
122         //it's ok!
123
return VALID;
124
125     }
126
127     static class WhoYouCallingKlausException extends EventException {
128         public WhoYouCallingKlausException() {
129             super ("Hey, who are you calling Klaus!!!");
130         }
131     };
132
133 }
134
Popular Tags