1 23 24 package barracudaDiscRack.presentation.personMgmt; 25 26 import barracudaDiscRack.business.person.*; 27 import barracudaDiscRack.presentation.BasePO; 28 import com.lutris.appserver.server.httpPresentation.*; 29 import com.lutris.appserver.server.session.*; 30 import com.lutris.util.*; 31 import org.enhydra.xml.xmlc.*; 32 import org.enhydra.xml.xmlc.html.*; 33 import org.w3c.dom.*; 34 import org.w3c.dom.html.*; 35 import org.enhydra.xml.xmlc.XMLObject; 36 import barracudaDiscRack.business.DiscRackBusinessException; 37 import barracudaDiscRack.presentation.DiscRackPresentationException; 38 39 import org.enhydra.barracuda.core.comp.*; 41 import org.enhydra.barracuda.core.forms.*; 42 import org.enhydra.barracuda.core.forms.validators.*; 43 import org.enhydra.barracuda.plankton.data.*; 44 import org.enhydra.barracuda.core.view.ViewCapabilities; 45 import org.enhydra.barracuda.core.event.*; 46 import org.enhydra.barracuda.core.comp.model.*; 47 import org.enhydra.barracuda.plankton.data.CollectionsUtil; 48 49 import javax.servlet.http.*; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 53 import java.io.*; 54 import java.util.List ; 55 import java.util.Iterator ; 56 import java.util.Collection ; 57 58 import org.apache.log4j.*; 59 60 66 public class Login extends BasePO { 67 68 71 private static String SUBMIT_NAME = "submit"; 72 private static String LOGIN_NAME = "login"; 73 private static String PASSWORD_NAME = "password"; 74 private static String ERROR_NAME = "ERROR_NAME"; 75 76 79 public boolean loggedInUserRequired() { 80 return false; 81 } 82 83 86 public XMLObject handleDefault() throws HttpPresentationException { 87 return showPage(null); 88 } 89 90 96 public XMLObject handleLogin() throws HttpPresentationException { 97 String login = this.getComms().request.getParameter(LOGIN_NAME); 98 String password = this.getComms().request.getParameter(PASSWORD_NAME); 99 Person user = null; 100 101 try { 102 user = PersonFactory.findPerson(login); 103 if (null == user || !user.getPassword().equals(password)) { 104 return showPage("Invalid username or password"); 105 } 107 else { 108 this.setUser(user); 109 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 110 } 111 } 112 catch (DiscRackBusinessException ex) { 113 this.writeDebugMsg("System error finding user: " + ex.getMessage()); 114 throw new DiscRackPresentationException("System error finding user", ex); 115 } 116 } 117 118 124 public XMLObject handleLogout() throws HttpPresentationException { 125 this.removeUserFromSession(); 126 return new ExitHTML(); 127 } 128 129 135 public XMLObject handleThrowException() throws Exception { 136 throw new Exception ( 137 "This is a test exception thrown from Login.java handleThrowException()"); 138 } 139 140 146 public XMLObject showPage(String errorMsg) { 147 148 LoginHTML page = (LoginHTML) myComms.xmlcFactory.create(LoginHTML.class); 149 150 if (null != errorMsg || 151 null != (errorMsg = this.getSessionData().getAndClearUserMessage())) { 152 page.setTextErrorText(errorMsg); 153 } 154 else { 155 page.getElementErrorText().getParentNode().removeChild(page. 156 getElementErrorText()); 157 } 158 159 return page; 160 } 161 162 166 169 public XMLObject handleShowUsingBarracuda() throws HttpPresentationException { 170 return this.showPageUsingBarracuda(new DefaultStateMap()); 171 } 172 173 176 public XMLObject handleLoginUsingBarracuda() throws HttpPresentationException { 177 178 ValidationException ve = null; 180 if (logger.isDebugEnabled()) 181 logger.debug("Creating login form"); 182 LoginForm lf = new LoginForm(getComms().session.getHttpSession()); 183 try { 184 if (logger.isDebugEnabled()) 186 logger.debug("Mapping/Validating login form"); 187 lf.map(this.getComms().request.getHttpServletRequest()).validate(true); 188 } 189 catch (ValidationException e) { 190 removeUserFromSession(); 191 if (logger.isDebugEnabled()) 192 logger.debug("Validation Exception: " + e); 193 194 if (logger.isDebugEnabled()){ 195 CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 196 } 197 198 ve = e; 199 } 200 201 if (ve == null) { 203 if (logger.isDebugEnabled()) 204 logger.debug("Redirecting to " + BasePO.DISC_CATALOG_PAGE); 205 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+DISC_CATALOG_PAGE); 206 } 207 208 if (logger.isDebugEnabled()) 209 logger.debug("Displaying login page"); 210 211 StateMap context = new DefaultStateMap(); 213 214 if (logger.isDebugEnabled()) 216 logger.debug("Saving form, errors"); 217 context.putState(LOGIN_FORM, lf); 218 context.putState(LOGIN_ERR, ve); 219 220 return this.showPageUsingBarracuda(context); 221 } 222 223 229 public XMLObject showPageUsingBarracuda(StateMap context) { 230 231 LoginHTML page = (LoginHTML) myComms.xmlcFactory.create(LoginHTML.class); 232 233 Node node = page.getDocument().getElementById("LoginForm"); 235 236 TemplateView tv = new DefaultTemplateView(node); 237 TemplateModel tm = new LoginModel(context); 238 BTemplate templateComp = new BTemplate(tm); 239 templateComp.setView(tv); 240 241 try { 242 HttpServletRequest ireq = this.getComms().request.getHttpServletRequest(); 243 HttpServletResponse ires = this.getComms().response.getHttpServletResponse(); 244 245 ViewContext vc = new DefaultViewContext(new ViewCapabilities(), 246 ireq, 247 ires); 248 templateComp.render(vc); 249 250 } 251 catch (RenderException re) { 252 this.writeDebugMsg("Render err:" + re); 253 } 254 255 return page; 256 } 257 258 265 protected static Logger logger = Logger.getLogger(Login.class.getName()); 267 268 private static final String LOGIN_FORM = LoginEventGateway.class.getName() + 270 ".LoginForm"; private static final String LOGIN_ERR = LoginEventGateway.class.getName() + 272 ".LoginErr"; 274 280 class LoginModel extends AbstractTemplateModel { 281 282 LoginForm fm = null; 283 ValidationException ve = null; 284 285 public LoginModel(StateMap context) { 287 fm = (LoginForm) context.getState(LOGIN_FORM); 288 ve = (ValidationException) context.getState(LOGIN_ERR); 289 } 290 291 public String getName() { 293 return "Login"; 294 } 295 296 public Object getItem(String key) { 297 ViewContext vc = getViewContext(); 298 if (key.equals("Username")) { 299 return (fm != null ? fm.getStringVal(LoginForm.USER, "") : ""); 300 } 301 else if (key.equals("Password")) { 302 return (fm != null ? fm.getStringVal(LoginForm.PASSWORD, "") : ""); 303 } 304 else if (key.equals("Errors")) { 305 if (ve == null) return ""; 306 List errlist = ve.getExceptionList(); 307 StringBuffer sb = new StringBuffer (errlist.size() > 1 ? 308 "There were several errors:" : ""); 309 Iterator it = errlist.iterator(); 310 while (it.hasNext()) { 311 sb.append(" " + ( (ValidationException) it.next()).getMessage()); 312 } 313 return sb.toString(); 314 } 315 else { 316 return super.getItem(key); 317 } 318 319 } 320 } 321 322 326 329 class LoginForm extends DefaultFormMap { 330 static final String USER = "login"; 332 static final String PASSWORD = "password"; 333 334 HttpSession session = null; 335 336 public LoginForm(HttpSession isession) { 337 session = isession; 338 339 if (logger.isDebugEnabled()) 341 logger.debug("Defining Login form elements"); 342 this.defineElement(new DefaultFormElement(USER, FormType.STRING, null, 343 new 344 NotNullValidator( 345 "You must enter a Login."))); 346 this.defineElement(new DefaultFormElement(PASSWORD, FormType.STRING, null, 347 new 348 NotNullValidator( 349 "You must enter a Password."))); 350 351 if (logger.isDebugEnabled()) 353 logger.debug("Defining Registration form validator"); 354 this.defineValidator(new LoginValidator(session)); 355 } 356 357 362 public FormMap validate(boolean deferExceptions) throws ValidationException { 363 if (logger.isDebugEnabled()) 365 logger.debug("Checking to see if already logged in (prevalidated)"); 366 Person p = getUser(); 367 if (null != p) { 368 try { 369 String fuser = this.getStringVal(LoginForm.USER); 370 String fpassword = this.getStringVal(LoginForm.PASSWORD); 371 if (fuser == null && fpassword == null) 372 return this; if (p.getLogin().equals(fuser) && 374 p.getPassword().equals(fpassword)) 375 return this; } 377 catch (DiscRackBusinessException e) { 378 } 379 } 380 381 return super.validate(deferExceptions); 383 } 384 } 385 386 389 class LoginValidator extends DefaultFormValidator { 390 391 HttpSession session = null; 392 393 public LoginValidator(HttpSession isession) { 394 session = isession; 395 } 396 397 public void validateForm(FormMap imap, boolean deferExceptions) throws 398 ValidationException { 399 if (logger.isDebugEnabled()) 400 logger.debug("Validating login form"); 401 402 if (logger.isDebugEnabled()) 404 logger.debug("Validate the user/pwd info"); 405 LoginForm map = (LoginForm) imap; 406 String user = map.getStringVal(LoginForm.USER); 407 String password = map.getStringVal(LoginForm.PASSWORD); 408 String errMsg = "Invalid username or password from LoginValidator. Please check your information and try again."; 409 try { 410 Person person = PersonFactory.findPerson(user); 412 if (person == null) 413 throw new ValidationException(map.getElement(LoginForm.USER), errMsg); 414 if (!person.getPassword().equals(password)) 415 throw new ValidationException(map.getElement(LoginForm.PASSWORD), 416 errMsg); 417 418 setUser(person); 420 } 421 catch (DiscRackBusinessException e) { 422 removeUserFromSession(); throw new ValidationException(map.getElement(LoginForm.USER), 424 "System error finding user: " + 425 e.getMessage() + ". Please try again. If the problem persists, contact your system administrator.", 426 e); 427 } 428 catch (DiscRackPresentationException e) { 429 throw new ValidationException(map.getElement(LoginForm.USER), 430 "System error setting user: " + 431 e.getMessage() + ". Please try again. If the problem persists, contact your system administrator.", 432 e); 433 } 434 } 435 } 436 } | Popular Tags |