KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > Login


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.pages;
16
17 import java.rmi.RemoteException JavaDoc;
18
19 import javax.servlet.http.Cookie JavaDoc;
20
21 import org.apache.tapestry.IEngine;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.callback.ICallback;
24 import org.apache.tapestry.event.PageEvent;
25 import org.apache.tapestry.event.PageRenderListener;
26 import org.apache.tapestry.form.IFormComponent;
27 import org.apache.tapestry.html.BasePage;
28 import org.apache.tapestry.valid.IValidationDelegate;
29 import org.apache.tapestry.valid.ValidatorException;
30 import org.apache.tapestry.vlib.IErrorProperty;
31 import org.apache.tapestry.vlib.VirtualLibraryEngine;
32 import org.apache.tapestry.vlib.Visit;
33 import org.apache.tapestry.vlib.ejb.IOperations;
34 import org.apache.tapestry.vlib.ejb.LoginException;
35 import org.apache.tapestry.vlib.ejb.Person;
36
37 /**
38  * Allows the user to login, by providing email address and password.
39  * After succesfully logging in, a cookie is placed on the client browser
40  * that provides the default email address for future logins (the cookie
41  * persists for a week).
42  *
43  * @author Howard Lewis Ship
44  * @version $Id: Login.java,v 1.11 2004/02/19 17:37:48 hlship Exp $
45  *
46  **/

47
48 public abstract class Login extends BasePage implements IErrorProperty, PageRenderListener
49 {
50     /**
51      * The name of a cookie to store on the user's machine that will identify
52      * them next time they log in.
53      *
54      **/

55
56     private static final String JavaDoc COOKIE_NAME = "org.apache.tapestry.vlib.Login.email";
57
58     private final static int ONE_WEEK = 7 * 24 * 60 * 60;
59
60     public abstract void setEmail(String JavaDoc value);
61
62     public abstract String JavaDoc getEmail();
63
64     public abstract String JavaDoc getPassword();
65
66     public abstract void setPassword(String JavaDoc password);
67
68     protected IValidationDelegate getValidationDelegate()
69     {
70         return (IValidationDelegate) getBeans().getBean("delegate");
71     }
72
73     protected void setErrorField(String JavaDoc componentId, String JavaDoc message)
74     {
75         IFormComponent field = (IFormComponent) getComponent(componentId);
76
77         IValidationDelegate delegate = getValidationDelegate();
78         delegate.setFormComponent(field);
79         delegate.record(new ValidatorException(message));
80     }
81
82     public abstract void setCallback(ICallback value);
83
84     public abstract ICallback getCallback();
85
86     /**
87      * Attempts to login.
88      *
89      * <p>If the user name is not known, or the password is invalid, then an error
90      * message is displayed.
91      *
92      **/

93
94     public void attemptLogin(IRequestCycle cycle)
95     {
96         String JavaDoc password = getPassword();
97
98         // Do a little extra work to clear out the password.
99

100         setPassword(null);
101         IValidationDelegate delegate = getValidationDelegate();
102
103         delegate.setFormComponent((IFormComponent) getComponent("inputPassword"));
104         delegate.recordFieldInputValue(null);
105
106         // An error, from a validation field, may already have occured.
107

108         if (delegate.getHasErrors())
109             return;
110
111         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
112
113         int i = 0;
114         while (true)
115         {
116             try
117             {
118                 IOperations operations = vengine.getOperations();
119
120                 Person person = operations.login(getEmail(), password);
121
122                 loginUser(person, cycle);
123
124                 break;
125
126             }
127             catch (LoginException ex)
128             {
129                 String JavaDoc fieldName = ex.isPasswordError() ? "inputPassword" : "inputEmail";
130
131                 setErrorField(fieldName, ex.getMessage());
132                 return;
133             }
134             catch (RemoteException JavaDoc ex)
135             {
136                 vengine.rmiFailure("Remote exception validating user.", ex, i++);
137             }
138         }
139     }
140
141     /**
142      * Sets up the {@link Person} as the logged in user, creates
143      * a cookie for thier email address (for subsequent logins),
144      * and redirects to the appropriate page ({@link MyLibrary}, or
145      * a specified page).
146      *
147      **/

148
149     public void loginUser(Person person, IRequestCycle cycle) throws RemoteException JavaDoc
150     {
151         String JavaDoc email = person.getEmail();
152
153         // Get the visit object; this will likely force the
154
// creation of the visit object and an HttpSession.
155

156         Visit visit = (Visit) getVisit();
157         visit.setUser(person);
158
159         // After logging in, go to the MyLibrary page, unless otherwise
160
// specified.
161

162         ICallback callback = getCallback();
163
164         if (callback == null)
165             cycle.activate("Home");
166         else
167             callback.performCallback(cycle);
168
169         // I've found that failing to set a maximum age and a path means that
170
// the browser (IE 5.0 anyway) quietly drops the cookie.
171

172         IEngine engine = getEngine();
173         Cookie JavaDoc cookie = new Cookie JavaDoc(COOKIE_NAME, email);
174         cookie.setPath(engine.getServletPath());
175         cookie.setMaxAge(ONE_WEEK);
176
177         // Record the user's email address in a cookie
178

179         cycle.getRequestContext().addCookie(cookie);
180
181         engine.forgetPage(getPageName());
182     }
183
184     public void pageBeginRender(PageEvent event)
185     {
186         if (getEmail() == null)
187             setEmail(getRequestCycle().getRequestContext().getCookieValue(COOKIE_NAME));
188     }
189
190 }
Popular Tags