KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > ex1 > LoginScreen


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: LoginScreen.java,v 1.16 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.examples.ex1;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.w3c.dom.*;
29 import org.w3c.dom.html.*;
30
31 import org.enhydra.barracuda.core.comp.*;
32 import org.enhydra.barracuda.core.event.*;
33 import org.enhydra.barracuda.core.event.helper.*;
34 import org.enhydra.barracuda.core.util.dom.*;
35 import org.enhydra.barracuda.core.util.http.*;
36 import org.apache.log4j.*;
37 import org.enhydra.barracuda.examples.ex1.events.*;
38 import org.enhydra.barracuda.examples.ex1.xmlc.*;
39
40
41 /**
42  * Event handlers (both Controller and View) for the
43  * Login screen
44  */

45 public class LoginScreen extends DefaultEventGateway {
46
47     //public constants
48
protected static final Logger logger = Logger.getLogger(LoginScreen.class.getName());
49
50     //this defines the various event handlers
51
private ListenerFactory getLoginFactory = new EventForwardingFactory(new RenderLoginScreen());
52     private ListenerFactory attemptLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new AttemptLoginHandler();} public String JavaDoc getListenerID() {return getID(AttemptLoginHandler.class);}};
53     private ListenerFactory renderLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderLoginScreenHandler();} public String JavaDoc getListenerID() {return getID(RenderLoginScreenHandler.class);}};
54     private ListenerFactory renderBadUsernameFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderBadUsernameHandler();} public String JavaDoc getListenerID() {return getID(RenderBadUsernameHandler.class);}};
55     private ListenerFactory renderBadPasswordFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderBadPasswordHandler();} public String JavaDoc getListenerID() {return getID(RenderBadPasswordHandler.class);}};
56     private ListenerFactory renderErrorFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderErrorHandler();} public String JavaDoc getListenerID() {return getID(RenderErrorHandler.class);}};
57
58     //private vars
59
private AttemptLogin lnkAttemptLogin; //added by TJ to show association
60
private GetLoginScreen lnkGetLoginScreen; //added by TJ to show association
61
private RenderLoginScreen lnkRenderLoginScreen; //added by TJ to show association
62
private BadUsername lnkBadUsername; //added by TJ to show association
63
private BadPassword lnkBadPassword; //added by TJ to show association
64
private ErrorEvent lnkError; //added by TJ to show association
65

66     /**
67      * Public constructor
68      */

69     public LoginScreen() {
70         //specify who's interested in what
71
specifyLocalEventInterests(getLoginFactory, GetLoginScreen.class);
72         specifyLocalEventInterests(attemptLoginFactory, AttemptLogin.class);
73         specifyLocalEventInterests(renderLoginFactory, RenderLoginScreen.class);
74         specifyLocalEventInterests(renderBadUsernameFactory, BadUsername.class);
75         specifyLocalEventInterests(renderBadPasswordFactory, BadPassword.class);
76         specifyLocalEventInterests(renderErrorFactory, ErrorEvent.class);
77     }
78
79     //------------------------------------------------------------
80
// Model 2 - Controller Event Handlers
81
//------------------------------------------------------------
82
/**
83      * AttemptLoginHandler - this is where we handle any AttemptLogin
84      * events. Validate and then redirect accordingly.
85      */

86     class AttemptLoginHandler extends DefaultBaseEventListener {
87         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
88             //unpack the necessary entities from the context
89
BaseEvent event = context.getEvent();
90             HttpServletRequest req = context.getRequest();
91             logger.debug("Attempting login...");
92
93             try {
94                 //validate the login information
95
int valid = LoginServices.validateUserPwd(req, true);
96                 logger.debug("Validity:"+LoginServices.xrefValidity(valid));
97
98                 //if it's valid, redirect to the Main screen
99
if (valid==LoginServices.VALID) {
100                     logger.debug("...Redirecting to Main Screen");
101                     throw new ClientSideRedirectException(new GetMainScreen());
102                 //invalid user
103
} else if (valid==LoginServices.INVALID_USER) {
104                     logger.debug("...Invalid User!");
105                     throw new InterruptDispatchException(new BadUsername());
106                 //invalid pwd
107
} else if (valid==LoginServices.INVALID_PASSWORD) {
108                     logger.debug("...Invalid Password!");
109                     throw new InterruptDispatchException(new BadPassword());
110                 //anything else
111
} else {
112                     logger.debug("...Unknown Error!");
113                     throw new InterruptDispatchException(new UnknownLoginError());
114                 }
115             } finally {
116                 event.setHandled(true);
117             }
118         }
119     }
120
121
122
123     //------------------------------------------------------------
124
// Model 2 - View Event Handlers
125
//------------------------------------------------------------
126
/**
127      * RenderLoginScreenHandler - this is where we handle any RenderLoginScreen
128      * event and actually generate the view. We repoulate the form if there
129      * is user/pwd information in the session.
130      */

131     class RenderLoginScreenHandler extends BTemplateViewHandler {
132         public Object JavaDoc getTemplateModels() {return new LocalTemplateModel();}
133         public Class JavaDoc getTemplateClass() {return LoginHTML.class;}
134     }
135
136     /**
137      * RenderBadUsernameHandler - this is where we handle any RenderBadUsername
138      * event and actually generate the error view. We could just redisplay the
139      * Login screen if we wanted and add an error message there, but I wanted to
140      * demonstrate how we can throw InterruptDispatchExceptions to redirect
141      * flow here.
142      */

143     class RenderBadUsernameHandler extends BTemplateViewHandler {
144         public Object JavaDoc getTemplateModels() {return new LocalTemplateModel();}
145         public Class JavaDoc getTemplateClass() {return BadUsernameHTML.class;}
146     }
147
148     /**
149      * RenderBadPasswordHandler - this is where we handle any RenderBadUsername
150      * event and actually generate the error view. We could just redisplay the
151      * Login screen if we wanted and add an error message there, but I wanted to
152      * demonstrate how we can throw InterruptDispatchExceptions to redirect
153      * flow here.
154      */

155     class RenderBadPasswordHandler extends BTemplateViewHandler {
156         public Object JavaDoc getTemplateModels() {return new LocalTemplateModel();}
157         public Class JavaDoc getTemplateClass() {return BadPasswordHTML.class;}
158     }
159
160     /**
161      * RenderErrorHandler - this is where we handle any Error event and generate a
162      * default view. Typically, this would only happen when a specific error was
163      * not caught and handled. In that case, the event proprogates up the event
164      * chain, and we catch it here.
165      */

166     class RenderErrorHandler extends BTemplateViewHandler {
167         public Object JavaDoc getTemplateModels() {return new LocalTemplateModel();}
168         public Class JavaDoc getTemplateClass() {return ErrorHTML.class;}
169     }
170
171     //------------------------------------------------------------
172
// Components - TemplateModel
173
//------------------------------------------------------------
174
/**
175      * LocalTemplateModel
176      */

177     class LocalTemplateModel extends AbstractTemplateModel {
178     
179         //register the model by name
180
public String JavaDoc getName() {return "Ex1_Login";}
181         
182         //provide items by key
183
public Object JavaDoc getItem(String JavaDoc key) {
184             ViewContext vc = getViewContext();
185             if (key.equals("GetLoginLink")) {
186                 return new BAction(new GetLoginScreen());
187             } else if (key.equals("AttemptLoginLink")) {
188                 return new BAction(new AttemptLogin());
189             } else if (key.equals("User")) {
190                 HttpSession session = SessionServices.getSession(vc, false);
191                 String JavaDoc user = (String JavaDoc) session.getAttribute(LoginServices.USER);
192                 if (user==null) user = "";
193                 return new BInput(null, LoginServices.USER, user);
194             } else if (key.equals("Password")) {
195                 HttpSession session = SessionServices.getSession(vc, false);
196                 String JavaDoc password = (String JavaDoc) session.getAttribute(LoginServices.PASSWORD);
197                 if (password==null) password = "";
198                 return new BInput(null, LoginServices.PASSWORD, password);
199             } else {
200                 return super.getItem(key);
201             }
202         }
203     }
204 }
205
206
Popular Tags