1 2 24 25 package barracudaDiscRack.presentation.personMgmt; 26 27 import barracudaDiscRack.presentation.*; 29 import barracudaDiscRack.presentation.personMgmt.events.*; 30 import barracudaDiscRack.business.person.*; 31 import barracudaDiscRack.business.DiscRackBusinessException; 32 33 import org.enhydra.barracuda.core.event.*; 35 import org.enhydra.barracuda.core.forms.*; 36 import org.enhydra.barracuda.core.forms.validators.*; 37 import org.enhydra.barracuda.core.comp.*; 38 import org.enhydra.barracuda.core.util.dom.*; 39 import org.enhydra.barracuda.plankton.data.CollectionsUtil; 40 41 import org.apache.log4j.*; 42 43 import org.w3c.dom.*; 45 import org.w3c.dom.html.*; 46 47 import javax.servlet.*; 49 import javax.servlet.http.*; 50 import java.io.*; 51 import java.util.List ; 52 import java.util.Iterator ; 53 54 60 public class LoginEventGateway extends BaseDiscRackEventGateway { 61 62 public LoginEventGateway() { 63 specifyLocalEventInterests(getLoginFactory, GetLogin.class); 65 specifyLocalEventInterests(renderLoginFactory, RenderLogin.class); 66 specifyLocalEventInterests(doLoginFactory, DoLogin.class); 67 } 68 69 private ListenerFactory getLoginFactory = new DefaultListenerFactory() { 71 public BaseEventListener getInstance() { 72 return new GetLoginHandler(); 73 } public String getListenerID() { 74 return getID(GetLoginHandler.class); 75 } 76 }; 77 78 private ListenerFactory renderLoginFactory = new DefaultListenerFactory() { 79 public BaseEventListener getInstance() { 80 return new RenderLoginHandler(); 81 } public String getListenerID() { 82 return getID(RenderLoginHandler.class); 83 } 84 }; 85 86 private ListenerFactory doLoginFactory = new DefaultListenerFactory() { 87 public BaseEventListener getInstance() { 88 return new DoLoginHandler(); 89 } public String getListenerID() { 90 return getID(DoLoginHandler.class); 91 } 92 }; 93 94 protected static Logger logger = Logger.getLogger(LoginEventGateway.class.getName()); 96 97 private static final String LOGIN_FORM = LoginEventGateway.class.getName()+".LoginForm"; private static final String LOGIN_ERR = LoginEventGateway.class.getName()+".LoginErr"; 101 108 class GetLoginHandler extends DefaultBaseEventListener { 109 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 110 initUsingContext( context ); 112 113 DispatchQueue queue = context.getQueue(); 115 116 if ( null != getUser() ) { 119 if ( logger.isDebugEnabled() ) logger.debug("Adding DoLogin to Queue (Auto-login)"); 120 queue.addEvent(new DoLogin(context.getEvent())); 121 } else { 122 if ( logger.isDebugEnabled() ) logger.debug("Adding RenderLogin to Queue"); 123 queue.addEvent(new RenderLogin(context.getEvent())); 124 } 125 } 126 } 127 128 132 class DoLoginHandler extends DefaultBaseEventListener { 133 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 134 initUsingContext( context ); 136 137 HttpServletRequest req = context.getRequest(); 139 140 ValidationException ve = null; 142 if ( logger.isDebugEnabled() ) logger.debug("Creating login form"); 143 LoginForm lf = new LoginForm( getComms().session.getHttpSession() ); 144 try { 145 if ( logger.isDebugEnabled() ) logger.debug("Mapping/Validating login form"); 147 lf.map(req).validate(true); 148 } catch ( ValidationException e ) { 149 removeUserFromSession(); 150 if ( logger.isDebugEnabled() ) logger.debug("Validation Exception: "+e); 151 152 if ( logger.isDebugEnabled() ) 153 CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 154 155 ve = e; 156 } 157 158 if ( logger.isDebugEnabled() ) logger.debug("Saving form, errors"); 160 context.putState(LOGIN_FORM, lf); 161 context.putState(LOGIN_ERR, ve); 162 163 if ( ve==null ) { 165 if ( logger.isDebugEnabled() ) logger.debug("Redirecting to " + BasePO.DISC_EVENT_CATALOG_PAGE); 166 throw new ClientSideRedirectException(BasePO.DISC_EVENT_CATALOG_PAGE); 167 } else { 168 if ( logger.isDebugEnabled() ) logger.debug("InterruptDispatch to GetLogin"); 169 throw new InterruptDispatchException(new GetLogin()); 170 } 171 } 172 } 173 174 180 class RenderLoginHandler extends DefaultBaseEventListener { 181 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 182 initUsingContext( context ); 184 185 HttpServletRequest req = context.getRequest(); 187 188 LoginHTML page = (LoginHTML) getComms().xmlcFactory.create(LoginHTML.class); 190 191 if ( logger.isDebugEnabled() ) logger.debug("Creating template component"); 193 Node node = page.getDocument().getElementById("LoginForm"); 194 196 TemplateView tv = new DefaultTemplateView(node); 197 TemplateModel tm = new LoginModel(context); 198 BTemplate templateComp = new BTemplate(tm); 199 templateComp.setView(tv); 200 201 try { 202 templateComp.render(new DefaultViewContext(context)); 203 } catch ( RenderException re ) { 204 logger.warn("Render err:"+re); 205 } 206 207 if ( logger.isDebugEnabled() ) logger.debug("Rendering document"); 210 new DefaultDOMWriter().write(page, context.getResponse()); 211 } 212 } 213 214 220 class LoginModel extends AbstractTemplateModel { 221 222 LoginForm fm = null; 223 ValidationException ve = null; 224 225 public LoginModel(EventContext ec) { 227 fm = (LoginForm) ec.getState(LOGIN_FORM); 228 ve = (ValidationException) ec.getState(LOGIN_ERR); 229 } 230 231 public String getName() { 233 return "Login"; 234 } 235 236 public Object getItem(String key) { 238 ViewContext vc = getViewContext(); 239 if ( key.equals("Username") ) { 240 return(fm!=null ? fm.getStringVal(LoginForm.USER, "") : ""); 241 } else if ( key.equals("Password") ) { 242 return(fm!=null ? fm.getStringVal(LoginForm.PASSWORD, "") : ""); 243 } else if ( key.equals("Errors") ) { 244 if ( ve==null ) return ""; 245 List errlist = ve.getExceptionList(); 246 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 247 Iterator it = errlist.iterator(); 248 while ( it.hasNext() ) { 249 sb.append(" "+((ValidationException) it.next()).getMessage()); 250 } 251 return sb.toString(); 252 } else 253 return super.getItem(key); 254 } 255 256 } 257 258 262 265 class LoginForm extends DefaultFormMap { 266 static final String USER = "login"; 268 static final String PASSWORD = "password"; 269 270 HttpSession session = null; 271 272 public LoginForm(HttpSession isession) { 273 session = isession; 274 275 if ( logger.isDebugEnabled() ) logger.debug("Defining Login form elements"); 277 this.defineElement(new DefaultFormElement(USER, FormType.STRING, null, new NotNullValidator("You must enter a Login."))); 278 this.defineElement(new DefaultFormElement(PASSWORD, FormType.STRING, null, new NotNullValidator("You must enter a Password."))); 279 280 if ( logger.isDebugEnabled() ) logger.debug("Defining Registration form validator"); 282 this.defineValidator(new LoginValidator(session)); 283 } 284 285 290 public FormMap validate(boolean deferExceptions) throws ValidationException { 291 if ( logger.isDebugEnabled() ) logger.debug("Checking to see if already logged in (prevalidated)"); 293 Person p = getUser(); 294 if ( null != p ) { 295 try { 296 String fuser = this.getStringVal(LoginForm.USER); 297 String fpassword = this.getStringVal(LoginForm.PASSWORD); 298 if ( fuser==null && fpassword==null ) return this; if ( p.getLogin().equals(fuser) && 300 p.getPassword().equals(fpassword) ) return this; } catch ( DiscRackBusinessException e ) { 302 } 303 } 304 305 return super.validate(deferExceptions); 307 } 308 } 309 310 313 class LoginValidator extends DefaultFormValidator { 314 315 HttpSession session = null; 316 317 public LoginValidator(HttpSession isession) { 318 session = isession; 319 } 320 321 public void validateForm(FormMap imap, boolean deferExceptions) throws ValidationException { 322 if ( logger.isDebugEnabled() ) logger.debug("Validating login form"); 323 324 if ( logger.isDebugEnabled() ) logger.debug("Validate the user/pwd info"); 326 LoginForm map = (LoginForm) imap; 327 String user = map.getStringVal(LoginForm.USER); 328 String password = map.getStringVal(LoginForm.PASSWORD); 329 String errMsg = "Invalid username or password. Please check your information and try again."; 330 try { 331 Person person = PersonFactory.findPerson(user); 333 if ( person==null ) throw new ValidationException(map.getElement(LoginForm.USER), errMsg); 334 if ( !person.getPassword().equals(password) ) throw new ValidationException(map.getElement(LoginForm.PASSWORD), errMsg); 335 336 setUser(person); 338 } catch ( DiscRackBusinessException e ) { 339 removeUserFromSession(); throw new ValidationException(map.getElement(LoginForm.USER), "System error finding user: " + e.getMessage()+". Please try again. If the problem persists, contact your system administrator.", e); 341 } catch ( DiscRackPresentationException e ) { 342 throw new ValidationException(map.getElement(LoginForm.USER), "System error setting user: " + e.getMessage()+". Please try again. If the problem persists, contact your system administrator.", e); 343 } 344 } 345 } 346 } 347 348 | Popular Tags |