1 64 65 66 package com.jcorporate.expresso.services.controller; 67 68 import com.jcorporate.expresso.core.controller.ControllerException; 69 import com.jcorporate.expresso.core.controller.ControllerRequest; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.controller.DBController; 72 import com.jcorporate.expresso.core.controller.ErrorCollection; 73 import com.jcorporate.expresso.core.controller.Input; 74 import com.jcorporate.expresso.core.controller.NonHandleableException; 75 import com.jcorporate.expresso.core.controller.State; 76 import com.jcorporate.expresso.core.controller.Transition; 77 import com.jcorporate.expresso.core.db.DBException; 78 import com.jcorporate.expresso.services.validation.AuthValidationException; 79 import com.jcorporate.expresso.services.validation.ValidationEntry; 80 import com.jcorporate.expresso.services.validation.ValidationHandler; 81 import org.apache.log4j.Logger; 82 83 import java.util.Date ; 84 85 86 99 public class ValidationController 100 extends DBController { 101 private static Logger log = 102 Logger.getLogger(ValidationController.class.getName()); 103 104 113 public ValidationController() { 114 super(); 115 addState(new State("validate", "Validate")); 116 addState(new State("promptValidation", "ValidationCode")); 117 setInitialState("validate"); 118 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 119 } 120 121 129 public String getTitle() { 130 return ("AuthorizationProcessing"); 131 } 132 133 146 protected void runPromptValidationState(ControllerRequest request, 147 ControllerResponse response) 148 throws ControllerException { 149 150 Input ctx = new Input("ctx", "Validation Context"); 152 ctx.setDefaultValue(response.getFormCache("ctx")); 153 response.add(ctx); 154 155 Input id = new Input("id", "ID"); 157 id.setDefaultValue(response.getFormCache("id")); 158 response.add(id); 159 160 Input code = new Input("code", "Code"); 162 code.setDefaultValue(response.getFormCache("code")); 163 response.add(code); 164 165 Transition val = new Transition("validate", this); 167 val.setLabel("RetryValidation"); 168 response.add(val); 169 } 170 171 188 protected ControllerResponse runValidateState(ControllerRequest request, 189 ControllerResponse response) 190 throws ControllerException, 191 DBException, 192 NonHandleableException { 193 ValidationEntry ve = null; 194 try { 195 ErrorCollection errors = new ErrorCollection(); 196 197 String dbName = request.getParameter("ctx"); 199 200 if ((dbName == null) || (dbName.equals(""))) { 201 errors.addError("Validation operation needs a db context"); 202 } else { 203 response.setFormCache("ctx", dbName); 204 } 205 206 String id = request.getParameter("id"); 208 209 if ((id == null) || (id.equals(""))) { 210 errors.addError("Validation operation needs an ID"); 211 } else { 212 response.setFormCache("id", id); 213 } 214 215 String code = request.getParameter("code"); 217 218 if ((code == null) || (code.equals(""))) { 219 errors.addError("Validation operation needs a code."); 220 } else { 221 response.setFormCache("code", code); 222 } 223 224 try { 226 ve = new ValidationEntry(dbName, id); 227 } catch (AuthValidationException e) { 228 errors.addError("This validation link could not be found"); 229 } 230 231 if (errors.isEmpty()) { 233 String state = ve.getStatus(); 234 235 if (state.equals(ValidationEntry.EXPIRED)) { 237 errors.addError("This validation link has expired"); 238 } else if (state.equals(ValidationEntry.VALIDATED)) { 239 errors.addError("This validation link is no longer available, it was already validated"); 240 } else { 241 242 Date now = new Date (); 244 Date expiresAt = ve.getExpiresAt(); 245 246 if (now.after(expiresAt)) { 247 ve.setStatus(ValidationEntry.EXPIRED); 248 errors.addError("This validation link has expired"); 249 } 250 } 251 } 252 if (errors.isEmpty()) { 254 if (!ve.codeMatches(code)) { 255 errors.addError("Specified code is invalid, validation failed"); 256 log.warn("Received invalid validation code. " + code); 257 } 258 } 259 if (!errors.isEmpty()) { 261 response.saveErrors(errors); 262 response = this.newState("promptValidation", request); 263 log.warn("error in validation for code: " + code); 264 return response; 265 } 266 267 ValidationHandler vh = ve.instantiateHandler(); 269 270 ve.setStatus(ValidationEntry.VALIDATED); 272 273 request.getSession().setPersistentAttribute(ValidationEntry.SESSION_KEY, ve); 277 278 response = vh.validated(ve.getParams(), request, response, this); 280 if (log.isInfoEnabled()) { 281 log.info("Successfully validated code" + code + 282 " for validation handler: " + vh.getClass().getName()); 283 } 284 285 return response; 286 } catch (AuthValidationException vex) { 287 log.error("Error processing validation", vex); 288 289 try { 290 ve.setStatus(ValidationEntry.WAITING); 293 } catch (AuthValidationException ex) { 294 log.error("There is an error validating code: " 295 + request.getParameter("code") + 296 "it could not be reset to a waiting state." + ex); 297 } 298 299 throw new ControllerException("Validation error", vex); 300 } catch (Throwable t) { 301 try { 302 ve.setStatus(ValidationEntry.WAITING); 305 } catch (AuthValidationException ex) { 306 log.error("There is an error validating code: " 307 + request.getParameter("code") + 308 "it could not be reset to a waiting state." + ex); 309 } 310 311 log.error("Error processing validation", t); 312 throw new ControllerException("Validation error", t); 313 } 314 } 315 316 329 public synchronized boolean stateAllowed(String newState, 330 ControllerRequest params) 331 throws ControllerException { 332 return true; 333 } 334 335 336 } | Popular Tags |