1 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 34 public class LoginServices { 35 36 public static final String NUMBER_OF_VISITS = LoginServices.class.getName()+".NumberOfVisits"; public static final String AUTO_LOGIN = LoginServices.class.getName()+".AutoLogin"; public static final String USER = "User"; public static final String PASSWORD = "Password"; 42 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 53 public static String 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 67 public static int validateUserPwd(HttpServletRequest req, boolean updateSession) throws EventException { 68 String user = req.getParameter(USER); 70 String password = req.getParameter(PASSWORD); 71 72 if (updateSession) { 74 HttpSession session = SessionServices.getSession(req); 75 session.setAttribute(USER, ""+user); 76 session.setAttribute(PASSWORD, ""+password); 77 } 78 79 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 99 public static int validateUserPwd(HttpSession session) throws EventException { 100 102 103 String user = (String ) session.getAttribute(USER); 105 String password = (String ) session.getAttribute(PASSWORD); 106 return validateUserPwd(user, password); 107 } 108 109 112 public static int validateUserPwd(String user, String password) throws EventException { 113 if (user==null || password==null) return INVALID_DATA_MISSING; 115 116 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 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 |