001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.presentation.xmlc.login;
022:
023: import java.io.*;
024: import java.net.*;
025: import java.util.*;
026: import com.lutris.http.*;
027: import com.lutris.appserver.server.httpPresentation.*;
028: import com.lutris.appserver.server.user.*;
029: import com.lutris.util.*;
030: import golfShop.GolfShop;
031: import golfShop.spec.user.*;
032: import com.lutris.appserver.server.session.*;
033: import golfShop.spec.LoginException;
034:
035: /**
036: * Presentation Object that processes the login request and redirects
037: * to the appropriate page.
038: *
039: * @author Shawn McMurdo
040: * @version $Revision: 1.1 $
041: */
042: public class LoginProcessor implements HttpPresentation {
043: /**
044: * State object in session.
045: */
046: private LoginState loginState;
047:
048: private static final String nousername = "You must specify a username and password to login.";
049: private static final String badlogin = "Incorrect username or password.";
050: private static final String unknownhost = "Login failed: Unknown host name.";
051: private static final String noremotehost = "Login failed: No remote host header.";
052: private static final String loginfailed = "Login failed for an unknown reason!";
053: private static final String toomanylogins = "Login failed: You are already logged in! "
054: + "Logout or wait for the session to expire.";
055: private static final String permdenied = "Login failed: Permission denied.";
056: private static final String disabled = "Login failed: Account is disabled.";
057: private static final String nopassword = "Login failed: You must enter a valid password to login.";
058: private static final String badpassword = "Login failed: You must enter a valid password to login.";
059: private static final String badusername = "Login failed: Invalid user name.";
060:
061: /**
062: * Entry point for presentation.
063: */
064: public void run(HttpPresentationComms comms) throws IOException,
065: PageRedirectException, Exception {
066:
067: GolfShop application = (GolfShop) comms.application;
068: String fail_url = comms.request
069: .getAppFileURIPath("login/Login.po");
070: String success_url = comms.request
071: .getAppFileURIPath("main/Main.po");
072:
073: loginState = LoginState.get(comms.session);
074:
075: //
076: // Get the username and password from the html form.
077: // These are mandatory fields. Redirect if nothing was entered,
078: // or no data was sent, or the data is bad.
079: //
080: String username = comms.request.getParameter("username");
081: if (username == null) {
082: myRedirect(fail_url, true, nousername, null, comms);
083: }
084: if (username.length() == 0) {
085: myRedirect(fail_url, true, badusername, null, comms);
086: }
087: String password = comms.request.getParameter("password");
088: if (password == null) {
089: myRedirect(fail_url, true, nopassword, username, comms);
090: }
091: if (password.length() == 0) {
092: myRedirect(fail_url, true, badpassword, username, comms);
093: }
094:
095: try {
096: //
097: // Do the actual login. The LBS will have created a session
098: // object and assigned a cookie already. If the login is
099: // sucessfull, the user manager will locate the user data
100: // object that represents the user and set it into the session
101: // object.
102: //
103: GolfShopUserManager userManager = application
104: .getUserManager();
105:
106: try {
107: userManager.login(username, password, comms.session);
108: } catch (NullPointerException e) {
109: }
110: // We have successfully logged in!
111: myRedirect(success_url, false, null, username, comms);
112:
113: } catch (LoginException le) {
114: switch (le.reason) {
115: case GolfShopUserManager.UNKNOWN_ERROR:
116: myRedirect(fail_url, true, loginfailed, username, comms);
117: break;
118: case GolfShopUserManager.IO_ERROR:
119: myRedirect(fail_url, true, badlogin, username, comms);
120: break;
121: case GolfShopUserManager.AUTH_FAILED:
122: myRedirect(fail_url, true, badlogin, username, comms);
123: break;
124: case GolfShopUserManager.MULTIPLE_LOGIN:
125: myRedirect(fail_url, true, toomanylogins, username,
126: comms);
127: break;
128: case GolfShopUserManager.PERMISSION_DENIED:
129: myRedirect(fail_url, true, permdenied, username, comms);
130: break;
131: case GolfShopUserManager.ACCOUNT_DISABLED:
132: myRedirect(fail_url, true, disabled, username, comms);
133: break;
134: default:
135: myRedirect(fail_url, true, loginfailed, username, comms);
136: break;
137: }
138: }
139: }
140:
141: /*
142: * Redirect to a new page. If deny is true, then the denied message and
143: * username is set in the state object.
144: */
145: private void myRedirect(String url, boolean deny, String msg,
146: String username, HttpPresentationComms comms)
147: throws HttpPresentationException {
148: ClientPageRedirectException e = new ClientPageRedirectException(
149: url);
150: if (deny) {
151: loginState.lastError = msg;
152: if ((username != null) && (username.length() > 0)) {
153: loginState.userName = username;
154: }
155: }
156: throw e;
157: }
158:
159: }
|