KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > presentation > xmlc > login > AccountProcessor


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.presentation.xmlc.login;
22
23 import java.io.*;
24 import java.net.*;
25 import com.lutris.util.*;
26 import com.lutris.http.*;
27 import golfShop.GolfShop;
28 import com.lutris.appserver.server.httpPresentation.*;
29 import com.lutris.appserver.server.user.*;
30 import golfShop.spec.user.*;
31 import golfShop.spec.LoginException;
32 /**
33  * Presentation Object that processes the new account requests and redirects
34  * to the appropriate page. It redirects to either the new account page
35  * or the main page (if sucessfull).
36  *
37  * @author Andrew John
38  * @version $Revision: 1.1 $
39  */

40 public class AccountProcessor implements HttpPresentation {
41     /**
42      * State object in session.
43      */

44     private LoginState loginState;
45
46     private static final String JavaDoc nousername =
47     "You must enter a username for the new account.";
48     private static final String JavaDoc nopassword =
49     "You must enter the new password twice.";
50     private static final String JavaDoc badpassword =
51     "Error entering password! Enter the new password twice.";
52     private static final String JavaDoc badlogin =
53     "Incorrect username or password.";
54     private static final String JavaDoc unknownhost =
55     "Login failed: Unknown host name.";
56     private static final String JavaDoc noremotehost =
57     "Login failed: No remote host header.";
58     private static final String JavaDoc loginfailed =
59     "Account creation failed: Unknown reason!";
60     private static final String JavaDoc userexists =
61     "Account creation failed: Username is already in use.";
62     private static final String JavaDoc badusername =
63     "That is not a valid user name.";
64     private static final String JavaDoc toomanylogins =
65     "Account creation failed: Exceeded maximum number of sessions.";
66     private static final String JavaDoc permdenied =
67     "Login failed: Permission denied.";
68     private static final String JavaDoc disabled =
69     "Login failed: Account is disabled.";
70
71
72     /**
73      * If a string is null, make it into an empty string.
74      */

75     private String JavaDoc fixStr(String JavaDoc str) {
76         if (str == null) {
77             str = "";
78         }
79         return str;
80     }
81
82
83     /**
84      * Entry point for presentation.
85      */

86     public void run(HttpPresentationComms comms)
87             throws IOException, PageRedirectException, Exception JavaDoc {
88         loginState = LoginState.get(comms.session);
89
90     GolfShop application = (GolfShop) comms.application;
91     String JavaDoc fail_url = comms.request.getAppFileURIPath("login/NewAccount.po");
92     String JavaDoc success_url = comms.request.getAppFileURIPath("main/Main.po");
93     InetAddress[] inet = new InetAddress[1];
94
95     // Get the username and passwords from the html form.
96
// These are mandatory fields. Redirect if nothing was entered,
97
// or no data was sent, or the data is bad.
98
String JavaDoc username = comms.request.getParameter("username");
99     if (username == null) {
100         myRedirect(fail_url, true, nousername, null, comms);
101         }
102     if (username.length() == 0) {
103         myRedirect(fail_url, true, badusername, null, comms);
104         }
105         String JavaDoc password = comms.request.getParameter("password");
106         String JavaDoc password2 = comms.request.getParameter("password2");
107     if ((password == null) || (password2 == null)) {
108         myRedirect(fail_url, true, nopassword, username, comms);
109         }
110     if (!password.equals(password2)) {
111         myRedirect(fail_url, true, badpassword, username, comms);
112         }
113     if (password.length() == 0) {
114         myRedirect(fail_url, true, badpassword, username, comms);
115         }
116
117     // Now get the optional fields. Most users will leave these
118
// blank for now, and fill them out at checkout time.
119
// getParameter() willr return null if there was no html form
120
// data passed back, so be paranoid.
121
String JavaDoc address1 = fixStr(comms.request.getParameter("address1"));
122     if (address1 == null) {
123             address1 = "";
124         }
125     String JavaDoc address2 = fixStr(comms.request.getParameter("address2"));
126     if (address2 == null) {
127             address2 = "";
128         }
129     String JavaDoc city = fixStr(comms.request.getParameter("city"));
130     String JavaDoc state = fixStr(comms.request.getParameter("state"));
131     String JavaDoc zip = fixStr(comms.request.getParameter("zip"));
132     String JavaDoc creditCard = fixStr(comms.request.getParameter("creditCard"));
133     String JavaDoc email = fixStr(comms.request.getParameter("email"));
134     
135     //
136
// For this application, future requests are only accepted from
137
// the same ip address they logged in from.
138
//
139
String JavaDoc remoteHost = comms.request.getRemoteHost();
140     // Be paranoid
141
if (remoteHost == null) {
142         myRedirect(fail_url, true, noremotehost, username, comms);
143         }
144     inet[0] = null;
145     try {
146         inet[0] = InetAddress.getByName(remoteHost);
147     } catch (UnknownHostException uh) {
148             // Will deal with below.
149
}
150     if (inet[0] == null) {
151         myRedirect(fail_url, true, unknownhost, username, comms);
152         }
153
154     try {
155         //
156
// Actually create the account (and log in). If it works, a new
157
// user will be created, then the user manager will create a new
158
// session and add it to the session manager.
159
// It returns the session key, which is used to refer to the
160
// session. We need to send the key back to the browser in a
161
// cookie, so it will be passed back to this application with
162
// each future request.
163
//
164
GolfShopUserManager userManager = application.getUserManager();
165         userManager.createAccount(username, password, address1,
166                           address2, city, state, zip, creditCard,
167                                       email, comms.session);
168         // We have successfully logged in!
169
myRedirect(success_url, false, null, username, comms);
170 /*
171  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres ) so
172  * we cannot create a new user
173  * We need to allow GolfShop_pres to be functional , response
174  * will be default HTML page with message
175  */

176  
177         } catch(NullPointerException JavaDoc ex) {
178         
179         myRedirect(fail_url, true, "You cannot register user while runing GolfShop_pres", username, comms);
180     }catch (LoginException le) {
181         switch (le.reason) {
182         case GolfShopUserManager.UNKNOWN_ERROR:
183         myRedirect(fail_url, true, loginfailed, username, comms);
184         break;
185         case GolfShopUserManager.IO_ERROR:
186         myRedirect(fail_url, true, badlogin, username, comms);
187         break;
188         case GolfShopUserManager.AUTH_FAILED:
189         myRedirect(fail_url, true, badlogin, username, comms);
190         break;
191         case GolfShopUserManager.MULTIPLE_LOGIN:
192         myRedirect(fail_url, true, toomanylogins, username, comms);
193         break;
194         case GolfShopUserManager.PERMISSION_DENIED:
195         myRedirect(fail_url, true, permdenied, username, comms);
196         break;
197         case GolfShopUserManager.ACCOUNT_DISABLED:
198         myRedirect(fail_url, true, disabled, username, comms);
199         break;
200         case GolfShopUserManager.USERNAME_ALREADY_EXISTS:
201         myRedirect(fail_url, true, userexists, username, comms);
202         break;
203         default:
204         myRedirect(fail_url, true, loginfailed, username, comms);
205         break;
206         }
207     }
208     }
209
210     /*
211      * Redirect to a new page. If deny is true, then the denied message and
212      * username is set in the state object.
213      */

214     private void myRedirect(String JavaDoc url, boolean deny, String JavaDoc msg,
215         String JavaDoc username, HttpPresentationComms comms)
216         throws HttpPresentationException {
217
218     ClientPageRedirectException e = new ClientPageRedirectException(url);
219     if (deny) {
220             loginState.lastError = msg;
221         if ((username != null) && (username.length() > 0)) {
222         loginState.userName = username;
223             }
224     }
225         throw e;
226     }
227 }
228
Popular Tags