1 23 package org.enhydra.barracuda.discRack.pres.screens; 24 25 import java.io.*; 26 import java.util.*; 27 import java.net.*; 28 import javax.servlet.*; 29 import javax.servlet.http.*; 30 31 import org.w3c.dom.*; 32 import org.w3c.dom.html.*; 33 34 import com.lutris.appserver.server.Enhydra; 35 import com.lutris.logging.LogChannel; 36 37 import org.enhydra.xml.xmlc.*; 38 39 import org.enhydra.barracuda.core.comp.*; 40 import org.enhydra.barracuda.core.event.*; 41 import org.enhydra.barracuda.core.event.helper.*; 42 import org.enhydra.barracuda.core.forms.*; 43 import org.enhydra.barracuda.core.forms.validators.*; 44 import org.enhydra.barracuda.plankton.data.*; 45 import org.enhydra.barracuda.core.util.dom.*; 46 import org.enhydra.barracuda.core.util.http.*; 47 import org.enhydra.barracuda.plankton.http.ServletUtil; 48 import org.apache.log4j.*; 49 50 import org.enhydra.barracuda.discRack.biz.*; 51 import org.enhydra.barracuda.discRack.biz.person.*; 52 import org.enhydra.barracuda.discRack.pres.events.*; 53 import org.enhydra.barracuda.discRack.pres.services.*; 54 import org.enhydra.barracuda.discRack.pres.xmlc.*; 55 56 57 98 public class LoginScreen extends DefaultEventGateway { 99 100 protected static Logger logger = Logger.getLogger(LoginScreen.class.getName()); 103 104 private static final String LOGIN_FORM = LoginScreen.class.getName()+".LoginForm"; private static final String LOGIN_ERR = LoginScreen.class.getName()+".LoginErr"; 108 private ListenerFactory getLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new GetLoginHandler();} public String getListenerID() {return getID(GetLoginHandler.class);}}; 110 private ListenerFactory doLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new DoLoginHandler();} public String getListenerID() {return getID(DoLoginHandler.class);}}; 111 private ListenerFactory doLogoutFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new DoLogoutHandler();} public String getListenerID() {return getID(DoLogoutHandler.class);}}; 112 private ListenerFactory validateLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new ValidateLoginHandler();} public String getListenerID() {return getID(ValidateLoginHandler.class);}}; 113 private ListenerFactory renderLoginFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderLoginHandler();} public String getListenerID() {return getID(RenderLoginHandler.class);}}; 114 private ListenerFactory renderLogoutFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderLogoutHandler();} public String getListenerID() {return getID(RenderLogoutHandler.class);}}; 115 private ListenerFactory renderSampleErrorFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderSampleErrorHandler();} public String getListenerID() {return getID(RenderSampleErrorHandler.class);}}; 116 117 119 120 124 public LoginScreen() { 125 specifyLocalEventInterests(getLoginFactory, GetLogin.class); 127 specifyLocalEventInterests(doLoginFactory, DoLogin.class); 128 specifyLocalEventInterests(doLogoutFactory, DoLogout.class); 129 specifyLocalEventInterests(validateLoginFactory, ValidatedEvent.class); 130 specifyLocalEventInterests(renderLoginFactory, RenderLogin.class); 131 specifyLocalEventInterests(renderLogoutFactory, RenderLogout.class); 132 specifyLocalEventInterests(renderSampleErrorFactory, SampleError.class); 133 } 134 135 136 143 class GetLoginHandler extends DefaultBaseEventListener { 144 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 145 DispatchQueue queue = context.getQueue(); 147 148 if (LoginServices.isLoggedIn(SessionServices.getSession(context.getRequest()))) { 151 if (logger.isDebugEnabled()) logger.debug("Adding DoLogin to Queue (Auto-login)"); 152 queue.addEvent(new DoLogin(context.getEvent())); 153 } else { 154 if (logger.isDebugEnabled()) logger.debug("Adding RenderLogin to Queue"); 155 queue.addEvent(new RenderLogin(context.getEvent())); 156 } 157 } 158 } 159 160 164 class DoLoginHandler extends DefaultBaseEventListener { 165 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 166 HttpServletRequest req = context.getRequest(); 168 169 ValidationException ve = null; 171 if (logger.isDebugEnabled()) logger.debug("Creating login form"); 172 LoginForm lf = new LoginForm(SessionServices.getSession(req)); 173 try { 174 if (logger.isDebugEnabled()) logger.debug("Mapping/Validating login form"); 176 lf.map(req).validate(true); 177 } catch (ValidationException e) { 178 LoginServices.logOut(SessionServices.getSession(req)); 179 if (logger.isDebugEnabled()) logger.debug("Validation Exception: "+e); 180 if (logger.isDebugEnabled()) CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 181 ve = e; 182 } 183 184 if (logger.isDebugEnabled()) logger.debug("Saving form, errors"); 186 context.putState(LOGIN_FORM, lf); 187 context.putState(LOGIN_ERR, ve); 188 189 if (ve==null) { 191 if (logger.isDebugEnabled()) logger.debug("Redirecting to GetCatalog"); 192 throw new ClientSideRedirectException(new GetCatalog()); 193 } else { 194 if (logger.isDebugEnabled()) logger.debug("InterruptDispatch to GetLogin"); 195 throw new InterruptDispatchException(new GetLogin()); 196 } 197 } 198 } 199 200 205 class ValidateLoginHandler extends DefaultBaseEventListener { 206 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 207 if (!LoginServices.isLoggedIn(SessionServices.getSession(context.getRequest()))) { 210 if (logger.isDebugEnabled()) logger.debug("InterruptDispatch to GetLogin"); 211 throw new ClientSideRedirectException(new GetLogin(context.getEvent())); 212 } 213 } 214 } 215 216 219 class DoLogoutHandler extends DefaultBaseEventListener { 220 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 221 DispatchQueue queue = context.getQueue(); 223 224 if (logger.isDebugEnabled()) logger.debug("Logging out"); 226 LoginServices.logOut(SessionServices.getSession(context.getRequest())); 227 queue.addEvent(new RenderLogout(context.getEvent())); 228 } 229 } 230 231 237 class RenderLoginHandler extends DefaultBaseEventListener { 238 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 239 HttpServletRequest req = context.getRequest(); 241 ServletUtil.showParams(req, logger); 242 243 if (logger.isDebugEnabled()) logger.debug("Handling RenderLogin Request"); 245 if (new Boolean (req.getParameter("generateSampleError")).booleanValue()) { 246 throw new InterruptDispatchException(new SampleError(new Exception ("Look, here's an error!"))); 247 } 248 249 LoginHTML page = (LoginHTML) DefaultDOMLoader.getGlobalInstance().getDOM(LoginHTML.class); 253 254 258 284 285 if (logger.isDebugEnabled()) logger.debug("Creating template component"); 287 Node node = page.getDocument().getElementById("LoginForm"); 288 BTemplate templateComp = new BTemplate(new LoginModel(context)); 289 templateComp.setView(new DefaultTemplateView(node)); 290 try {templateComp.render(new DefaultViewContext(context));} 291 catch (RenderException re) {logger.warn("Render err:"+re);} 292 293 if (logger.isDebugEnabled()) logger.debug("Rendering document"); 296 new DefaultDOMWriter().write(page, context.getResponse()); 297 } 298 } 299 300 303 318 class RenderLogoutHandler extends BTemplateViewHandler { 319 public Object getTemplateModels() {return new LogoutModel();} 321 public Class getTemplateClass() {return ExitHTML.class;} 322 } 323 324 327 class RenderSampleErrorHandler extends DefaultBaseEventListener { 328 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 329 BaseEvent event = context.getEvent(); 331 332 ErrorHTML page = (ErrorHTML) DefaultDOMLoader.getGlobalInstance().getDOM(ErrorHTML.class); 336 Object source = event.getSource(); 337 if (source instanceof Throwable ) { 338 Throwable th = (Throwable ) source; 340 StringWriter writer = new StringWriter(); 341 th.printStackTrace(new PrintWriter(writer)); 342 String err = writer.toString(); 343 344 LogChannel logChannel = Enhydra.getLogChannel(); 346 int level = logChannel.getLevel("DEBUG"); 347 logChannel.write(level, this.getClass().getName()+" stack trace = "); 348 logChannel.write(level, err); 349 350 page.setTextStackTrace(err); 352 page.setTextErrorMessage(th.getMessage()); 353 } 354 355 if (logger.isDebugEnabled()) logger.debug("Rendering document"); 358 new DefaultDOMWriter().write(page, context.getResponse()); 359 } 360 } 361 362 368 class LoginModel extends AbstractTemplateModel { 369 370 LoginForm fm = null; 371 ValidationException ve = null; 372 373 public LoginModel(EventContext ec) { 375 fm = (LoginForm) ec.getState(LOGIN_FORM); 376 ve = (ValidationException) ec.getState(LOGIN_ERR); 377 } 378 379 public String getName() {return "Login";} 381 382 public Object getItem(String key) { 384 ViewContext vc = getViewContext(); 385 if (key.equals("Username")) { 386 return (fm!=null ? fm.getStringVal(LoginForm.USER, "") : ""); 387 } else if (key.equals("Password")) { 388 return (fm!=null ? fm.getStringVal(LoginForm.PASSWORD, "") : ""); 389 } else if (key.equals("LoginButton")) { 390 BAction baComp = new BAction(new DoLogin()); 391 baComp.setDisableBackButton(true); 392 return baComp; 393 } else if (key.equals("RegisterButton")) { 394 BAction baComp = new BAction(new GetRegister()); 395 baComp.setDisableBackButton(true); 396 return baComp; 397 } else if (key.equals("ExceptionButton")) { 398 BAction baComp = new BAction(new GetLogin()); 399 baComp.setParam("generateSampleError","true"); 400 baComp.setDisableBackButton(true); 401 return baComp; 402 } else if (key.equals("Errors")) { 403 if (ve==null) return null; 405 List errlist = ve.getExceptionList(); 406 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 407 Iterator it = errlist.iterator(); 408 while (it.hasNext()) { 409 sb.append(" "+((ValidationException) it.next()).getMessage()); 410 } 411 return sb.toString(); 412 } else { 413 return super.getItem(key); 414 } 415 } 416 } 417 418 421 class LogoutModel extends AbstractTemplateModel { 422 423 public String getName() {return "Logout";} 425 426 public Object getItem(String key) { 428 ViewContext vc = getViewContext(); 429 if (key.equals("LoginButton")) { 430 BAction baComp = new BAction(new GetLogin()); 431 baComp.setDisableBackButton(true); 432 return baComp; 433 } else { 434 return super.getItem(key); 435 } 436 } 437 } 438 439 440 446 class LoginForm extends DefaultFormMap { 447 static final String USER = "login"; 449 static final String PASSWORD = "password"; 450 451 HttpSession session = null; 452 453 public LoginForm(HttpSession isession) { 454 session = isession; 455 456 if (logger.isDebugEnabled()) logger.debug("Defining Login form elements"); 458 this.defineElement(new DefaultFormElement(USER, FormType.STRING, null, new NotNullValidator("You must enter a Login."))); 459 this.defineElement(new DefaultFormElement(PASSWORD, FormType.STRING, null, new NotNullValidator("You must enter a Password."))); 460 461 if (logger.isDebugEnabled()) logger.debug("Defining Registration form validator"); 463 this.defineValidator(new LoginValidator(session)); 464 } 465 466 471 public FormMap validate(boolean deferExceptions) throws ValidationException { 472 if (logger.isDebugEnabled()) logger.debug("Checking to see if already logged in (prevalidated)"); 474 if (LoginServices.isLoggedIn(session)) { 475 try { 476 Person p = LoginServices.getCurrentUser(session); 477 String fuser = this.getStringVal(LoginForm.USER); 478 String fpassword = this.getStringVal(LoginForm.PASSWORD); 479 if (fuser==null && fpassword==null) return this; if (p.getLogin().equals(fuser) && 481 p.getPassword().equals(fpassword)) return this; } catch (DiscRackBusinessException e) {} 483 } 484 485 return super.validate(deferExceptions); 487 } 488 } 489 490 493 class LoginValidator extends DefaultFormValidator { 494 495 HttpSession session = null; 496 497 public LoginValidator(HttpSession isession) { 498 session = isession; 499 } 500 501 public void validateForm(FormMap imap, boolean deferExceptions) throws ValidationException { 502 if (logger.isDebugEnabled()) logger.debug("Validating login form"); 503 504 if (logger.isDebugEnabled()) logger.debug("Validate the user/pwd info"); 506 LoginForm map = (LoginForm) imap; 507 DeferredValidationException defValException = new DeferredValidationException(); 508 String user = map.getStringVal(LoginForm.USER); 509 String password = map.getStringVal(LoginForm.PASSWORD); 510 String errMsg = "Invalid username or password. Please check your information and try again."; 511 if (user != null && password != null) { 512 try { 513 Person person = PersonFactory.findPerson(user); 515 if (person==null) throw new ValidationException(map.getElement(LoginForm.USER), errMsg); 518 if (!person.getPassword().equals(password)) throw new ValidationException(map.getElement(LoginForm.PASSWORD), errMsg); 519 520 LoginServices.logIn(session, person); 522 } catch (DiscRackBusinessException e) { 523 LoginServices.logOut(session); defValException.addSubException(new DeferredValidationException(map.getElement(LoginForm.USER), "System error finding user: " + e.getMessage()+". Please try again. If the problem persists, contact your system administrator.", e)); 525 } 526 } 527 if (defValException.hasSubExceptions()) { 528 throw defValException; 529 } 530 } 531 } 532 } 533 534 | Popular Tags |