KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26 import com.lutris.http.*;
27 import com.lutris.appserver.server.httpPresentation.*;
28 import com.lutris.appserver.server.user.*;
29 import com.lutris.util.*;
30 import golfShop.GolfShop;
31 import golfShop.spec.user.*;
32 import com.lutris.appserver.server.session.*;
33 import golfShop.spec.LoginException;
34 /**
35  * Presentation Object that processes the login request and redirects
36  * to the appropriate page.
37  *
38  * @author Shawn McMurdo
39  * @version $Revision: 1.1 $
40  */

41 public class LoginProcessor implements HttpPresentation {
42     /**
43      * State object in session.
44      */

45     private LoginState loginState;
46
47     private static final String JavaDoc nousername =
48     "You must specify a username and password to login.";
49     private static final String JavaDoc badlogin =
50     "Incorrect username or password.";
51     private static final String JavaDoc unknownhost =
52     "Login failed: Unknown host name.";
53     private static final String JavaDoc noremotehost =
54     "Login failed: No remote host header.";
55     private static final String JavaDoc loginfailed =
56     "Login failed for an unknown reason!";
57     private static final String JavaDoc toomanylogins =
58     "Login failed: You are already logged in! " +
59     "Logout or wait for the session to expire.";
60     private static final String JavaDoc permdenied =
61     "Login failed: Permission denied.";
62     private static final String JavaDoc disabled =
63     "Login failed: Account is disabled.";
64     private static final String JavaDoc nopassword =
65     "Login failed: You must enter a valid password to login.";
66     private static final String JavaDoc badpassword =
67     "Login failed: You must enter a valid password to login.";
68     private static final String JavaDoc badusername =
69     "Login failed: Invalid user name.";
70
71     /**
72      * Entry point for presentation.
73      */

74     public void run(HttpPresentationComms comms)
75             throws IOException, PageRedirectException,Exception JavaDoc {
76
77     GolfShop application = (GolfShop) comms.application;
78     String JavaDoc fail_url = comms.request.getAppFileURIPath("login/Login.po");
79     String JavaDoc success_url = comms.request.getAppFileURIPath("main/Main.po");
80
81         loginState = LoginState.get(comms.session);
82
83     //
84
// Get the username and password from the html form.
85
// These are mandatory fields. Redirect if nothing was entered,
86
// or no data was sent, or the data is bad.
87
//
88
String JavaDoc username = comms.request.getParameter("username");
89     if (username == null) {
90         myRedirect(fail_url, true, nousername, null, comms);
91         }
92     if (username.length() == 0) {
93         myRedirect(fail_url, true, badusername, null, comms);
94         }
95     String JavaDoc password = comms.request.getParameter("password");
96     if (password == null) {
97         myRedirect(fail_url, true, nopassword, username, comms);
98         }
99     if (password.length() == 0) {
100         myRedirect(fail_url, true, badpassword, username, comms);
101         }
102
103     try {
104         //
105
// Do the actual login. The LBS will have created a session
106
// object and assigned a cookie already. If the login is
107
// sucessfull, the user manager will locate the user data
108
// object that represents the user and set it into the session
109
// object.
110
//
111
GolfShopUserManager userManager = application.getUserManager();
112
113          
114    try{
115         userManager.login(username, password, comms.session);
116  }catch (NullPointerException JavaDoc e){
117 }
118         // We have successfully logged in!
119
myRedirect(success_url, false, null, username, comms);
120         
121     } catch (LoginException le) {
122         switch (le.reason) {
123         case GolfShopUserManager.UNKNOWN_ERROR:
124             myRedirect(fail_url, true, loginfailed, username, comms);
125             break;
126         case GolfShopUserManager.IO_ERROR:
127             myRedirect(fail_url, true, badlogin, username, comms);
128             break;
129         case GolfShopUserManager.AUTH_FAILED:
130             myRedirect(fail_url, true, badlogin, username, comms);
131             break;
132         case GolfShopUserManager.MULTIPLE_LOGIN:
133             myRedirect(fail_url, true, toomanylogins, username, comms);
134             break;
135         case GolfShopUserManager.PERMISSION_DENIED:
136             myRedirect(fail_url, true, permdenied, username, comms);
137             break;
138         case GolfShopUserManager.ACCOUNT_DISABLED:
139             myRedirect(fail_url, true, disabled, username, comms);
140             break;
141         default:
142             myRedirect(fail_url, true, loginfailed, username, comms);
143             break;
144          }
145         }
146     }
147
148     /*
149      * Redirect to a new page. If deny is true, then the denied message and
150      * username is set in the state object.
151      */

152     private void myRedirect(String JavaDoc url, boolean deny, String JavaDoc msg,
153         String JavaDoc username, HttpPresentationComms comms)
154         throws HttpPresentationException {
155     ClientPageRedirectException e = new ClientPageRedirectException(url);
156     if (deny) {
157             loginState.lastError = msg;
158         if ((username != null) && (username.length() > 0)) {
159         loginState.userName = username;
160             }
161     }
162     throw e;
163     }
164
165 }
166
Popular Tags