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 com.lutris.util.*;
026: import com.lutris.http.*;
027: import golfShop.GolfShop;
028: import com.lutris.appserver.server.httpPresentation.*;
029: import com.lutris.appserver.server.user.*;
030: import golfShop.spec.user.*;
031: import golfShop.spec.LoginException;
032:
033: /**
034: * Presentation Object that processes the new account requests and redirects
035: * to the appropriate page. It redirects to either the new account page
036: * or the main page (if sucessfull).
037: *
038: * @author Andrew John
039: * @version $Revision: 1.1 $
040: */
041: public class AccountProcessor implements HttpPresentation {
042: /**
043: * State object in session.
044: */
045: private LoginState loginState;
046:
047: private static final String nousername = "You must enter a username for the new account.";
048: private static final String nopassword = "You must enter the new password twice.";
049: private static final String badpassword = "Error entering password! Enter the new password twice.";
050: private static final String badlogin = "Incorrect username or password.";
051: private static final String unknownhost = "Login failed: Unknown host name.";
052: private static final String noremotehost = "Login failed: No remote host header.";
053: private static final String loginfailed = "Account creation failed: Unknown reason!";
054: private static final String userexists = "Account creation failed: Username is already in use.";
055: private static final String badusername = "That is not a valid user name.";
056: private static final String toomanylogins = "Account creation failed: Exceeded maximum number of sessions.";
057: private static final String permdenied = "Login failed: Permission denied.";
058: private static final String disabled = "Login failed: Account is disabled.";
059:
060: /**
061: * If a string is null, make it into an empty string.
062: */
063: private String fixStr(String str) {
064: if (str == null) {
065: str = "";
066: }
067: return str;
068: }
069:
070: /**
071: * Entry point for presentation.
072: */
073: public void run(HttpPresentationComms comms) throws IOException,
074: PageRedirectException, Exception {
075: loginState = LoginState.get(comms.session);
076:
077: GolfShop application = (GolfShop) comms.application;
078: String fail_url = comms.request
079: .getAppFileURIPath("login/NewAccount.po");
080: String success_url = comms.request
081: .getAppFileURIPath("main/Main.po");
082: InetAddress[] inet = new InetAddress[1];
083:
084: // Get the username and passwords from the html form.
085: // These are mandatory fields. Redirect if nothing was entered,
086: // or no data was sent, or the data is bad.
087: String username = comms.request.getParameter("username");
088: if (username == null) {
089: myRedirect(fail_url, true, nousername, null, comms);
090: }
091: if (username.length() == 0) {
092: myRedirect(fail_url, true, badusername, null, comms);
093: }
094: String password = comms.request.getParameter("password");
095: String password2 = comms.request.getParameter("password2");
096: if ((password == null) || (password2 == null)) {
097: myRedirect(fail_url, true, nopassword, username, comms);
098: }
099: if (!password.equals(password2)) {
100: myRedirect(fail_url, true, badpassword, username, comms);
101: }
102: if (password.length() == 0) {
103: myRedirect(fail_url, true, badpassword, username, comms);
104: }
105:
106: // Now get the optional fields. Most users will leave these
107: // blank for now, and fill them out at checkout time.
108: // getParameter() willr return null if there was no html form
109: // data passed back, so be paranoid.
110: String address1 = fixStr(comms.request.getParameter("address1"));
111: if (address1 == null) {
112: address1 = "";
113: }
114: String address2 = fixStr(comms.request.getParameter("address2"));
115: if (address2 == null) {
116: address2 = "";
117: }
118: String city = fixStr(comms.request.getParameter("city"));
119: String state = fixStr(comms.request.getParameter("state"));
120: String zip = fixStr(comms.request.getParameter("zip"));
121: String creditCard = fixStr(comms.request
122: .getParameter("creditCard"));
123: String email = fixStr(comms.request.getParameter("email"));
124:
125: //
126: // For this application, future requests are only accepted from
127: // the same ip address they logged in from.
128: //
129: String remoteHost = comms.request.getRemoteHost();
130: // Be paranoid
131: if (remoteHost == null) {
132: myRedirect(fail_url, true, noremotehost, username, comms);
133: }
134: inet[0] = null;
135: try {
136: inet[0] = InetAddress.getByName(remoteHost);
137: } catch (UnknownHostException uh) {
138: // Will deal with below.
139: }
140: if (inet[0] == null) {
141: myRedirect(fail_url, true, unknownhost, username, comms);
142: }
143:
144: try {
145: //
146: // Actually create the account (and log in). If it works, a new
147: // user will be created, then the user manager will create a new
148: // session and add it to the session manager.
149: // It returns the session key, which is used to refer to the
150: // session. We need to send the key back to the browser in a
151: // cookie, so it will be passed back to this application with
152: // each future request.
153: //
154: GolfShopUserManager userManager = application
155: .getUserManager();
156: userManager.createAccount(username, password, address1,
157: address2, city, state, zip, creditCard, email,
158: comms.session);
159: // We have successfully logged in!
160: myRedirect(success_url, false, null, username, comms);
161: /*
162: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres ) so
163: * we cannot create a new user
164: * We need to allow GolfShop_pres to be functional , response
165: * will be default HTML page with message
166: */
167:
168: } catch (NullPointerException ex) {
169:
170: myRedirect(
171: fail_url,
172: true,
173: "You cannot register user while runing GolfShop_pres",
174: username, comms);
175: } catch (LoginException le) {
176: switch (le.reason) {
177: case GolfShopUserManager.UNKNOWN_ERROR:
178: myRedirect(fail_url, true, loginfailed, username, comms);
179: break;
180: case GolfShopUserManager.IO_ERROR:
181: myRedirect(fail_url, true, badlogin, username, comms);
182: break;
183: case GolfShopUserManager.AUTH_FAILED:
184: myRedirect(fail_url, true, badlogin, username, comms);
185: break;
186: case GolfShopUserManager.MULTIPLE_LOGIN:
187: myRedirect(fail_url, true, toomanylogins, username,
188: comms);
189: break;
190: case GolfShopUserManager.PERMISSION_DENIED:
191: myRedirect(fail_url, true, permdenied, username, comms);
192: break;
193: case GolfShopUserManager.ACCOUNT_DISABLED:
194: myRedirect(fail_url, true, disabled, username, comms);
195: break;
196: case GolfShopUserManager.USERNAME_ALREADY_EXISTS:
197: myRedirect(fail_url, true, userexists, username, comms);
198: break;
199: default:
200: myRedirect(fail_url, true, loginfailed, username, comms);
201: break;
202: }
203: }
204: }
205:
206: /*
207: * Redirect to a new page. If deny is true, then the denied message and
208: * username is set in the state object.
209: */
210: private void myRedirect(String url, boolean deny, String msg,
211: String username, HttpPresentationComms comms)
212: throws HttpPresentationException {
213:
214: ClientPageRedirectException e = new ClientPageRedirectException(
215: url);
216: if (deny) {
217: loginState.lastError = msg;
218: if ((username != null) && (username.length() > 0)) {
219: loginState.userName = username;
220: }
221: }
222: throw e;
223: }
224: }
|