KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > servlets > Auth


1 package edu.rice.rubis.beans.servlets;
2
3 import javax.naming.Context JavaDoc;
4 import javax.rmi.PortableRemoteObject JavaDoc;
5 import javax.servlet.http.HttpServlet JavaDoc;
6
7 import edu.rice.rubis.beans.User;
8 import edu.rice.rubis.beans.UserHome;
9
10 /**
11  * This class is not a servlet but it provides user authentication services to servlets.
12  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
13  * @version 1.0
14  */

15 public class Auth extends HttpServlet JavaDoc
16 {
17
18   private Context JavaDoc servletContext;
19   private ServletPrinter sp;
20
21   /**
22    * Creates a new <code>Auth</code> instance.
23    *
24    * @param context a <code>Context</code> value
25    * @param printer a <code>ServletPrinter</code> value
26    */

27   public Auth(Context JavaDoc context, ServletPrinter printer)
28   {
29     servletContext = context;
30     sp = printer;
31   }
32
33   /**
34    * Describe <code>authenticate</code> method here.
35    *
36    * @param name user nick name
37    * @param password user password
38    * @return an <code>int</code> value corresponding to the user id or -1 on error
39    */

40   public int authenticate(String JavaDoc name, String JavaDoc password)
41   {
42     int userId = -1;
43
44     // Connecting to user Home interface thru JNDI
45
UserHome userHome = null;
46     try
47     {
48       userHome =
49         (UserHome) PortableRemoteObject.narrow(
50           servletContext.lookup("UserHome"),
51           UserHome.class);
52     }
53     catch (Exception JavaDoc e)
54     {
55       sp.printHTML("Cannot lookup User: " + e + "<br>");
56       return userId;
57     }
58     // get the User ID
59
try
60     {
61       User user = userHome.findByNickName(name);
62       String JavaDoc pwd = user.getPassword();
63       if (pwd.compareTo(password) == 0)
64       {
65         userId = user.getId().intValue();
66       }
67     }
68     catch (Exception JavaDoc e)
69     {
70       sp.printHTML(
71         " User "
72           + name
73           + " does not exist in the database!<br>(got exception: "
74           + e
75           + ")<br>");
76       return userId;
77     }
78     return userId;
79   }
80
81 }
82
Popular Tags