1 7 package org.jfox.petstore.action; 8 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 import javax.ejb.EJB ; 13 import javax.security.auth.callback.Callback ; 14 import javax.security.auth.callback.CallbackHandler ; 15 import javax.security.auth.callback.UnsupportedCallbackException ; 16 17 import org.jfox.ejb3.security.JAASLoginRequestCallback; 18 import org.jfox.ejb3.security.JAASLoginResponseCallback; 19 import org.jfox.ejb3.security.JAASLoginService; 20 import org.jfox.entity.EntityFactory; 21 import org.jfox.framework.annotation.Inject; 22 import org.jfox.framework.annotation.Service; 23 import org.jfox.mvc.ActionSupport; 24 import org.jfox.mvc.Invocation; 25 import org.jfox.mvc.InvocationContext; 26 import org.jfox.mvc.PageContext; 27 import org.jfox.mvc.SessionContext; 28 import org.jfox.mvc.annotation.ActionMethod; 29 import org.jfox.mvc.validate.StringValidation; 30 import org.jfox.mvc.validate.ValidateException; 31 import org.jfox.petstore.bo.AccountBO; 32 import org.jfox.petstore.bo.CategoryBO; 33 import org.jfox.petstore.entity.Account; 34 import org.jfox.petstore.entity.Category; 35 36 39 @Service(id = "account") 40 public class AccountAction extends ActionSupport implements CallbackHandler { 41 42 @Inject 43 JAASLoginService loginService; 44 45 @EJB 46 AccountBO accountBO; 47 48 @EJB 49 CategoryBO categoryBO; 50 51 public static final String ACCOUNT_SESSION_KEY = "__ACCOUNT__"; 52 53 private static List <String > languages = new ArrayList <String >(); 54 55 static { 56 languages.add("English"); 57 languages.add("Chinese"); 59 } 60 61 62 @ActionMethod(successView = "NewAccountForm.vhtml") 63 public void doGetNewAccount(InvocationContext invocationContext) throws Exception { 64 PageContext pageContext = invocationContext.getPageContext(); 66 pageContext.setAttribute("languages", languages); 67 68 List <Category> categories = categoryBO.getCategoryList(); 69 pageContext.setAttribute("categories", categories); 70 } 71 72 @ActionMethod(successView = "index.vhtml", errorView = "NewAccountForm.vhtml", invocationClass = NewAccountInvocation.class) 73 public void doPostCreate(InvocationContext invocationContext) throws Exception { 74 NewAccountInvocation invocation = (NewAccountInvocation)invocationContext.getInvocation(); 75 Account newAccount = EntityFactory.newEntityObject(Account.class); 76 newAccount.setUsername(invocation.getUsername()); 77 newAccount.setStatus("OK"); 78 newAccount.setPassword(invocation.getPassword()); 79 newAccount.setAddress1(invocation.getAddress1()); 80 newAccount.setAddress2(invocation.getAddress2()); 81 newAccount.setBannerOption(invocation.getBannerOption()); 82 newAccount.setCity(invocation.getCity()); 83 newAccount.setCountry(invocation.getCountry()); 84 newAccount.setEmail(invocation.getEmail()); 85 newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId()); 86 newAccount.setFirstName(invocation.getFirstName()); 87 newAccount.setLanguagePreference(invocation.getLanguagePreference()); 88 newAccount.setLastName(invocation.getLastName()); 89 newAccount.setListOption(invocation.getListOption()); 90 newAccount.setPassword(invocation.getPassword()); 91 newAccount.setPhone(invocation.getPhone()); 92 newAccount.setState(invocation.getState()); 93 newAccount.setZip(invocation.getZip()); 94 95 try { 96 accountBO.insertAccount(newAccount); 97 } 98 catch (Exception e) { 99 throw e; 101 } 102 } 103 104 105 @ActionMethod(successView = "signon.vhtml") 106 public void doGetSignon(InvocationContext invocationContext) throws Exception { 107 } 109 110 @ActionMethod(successView = "index.vhtml", errorView = "signon.vhtml", invocationClass = SignonInvocation.class) 111 public void doPostSignon(InvocationContext invocationContext) throws Exception { 112 SignonInvocation invocation = (SignonInvocation)invocationContext.getInvocation(); 113 114 Account account = (Account)loginService.login(invocationContext.getSessionContext(), this, invocation.getUsername(),invocation.getPassword()); 115 if (account == null) { 116 String msg = "Invalid username or password. Signon failed"; 117 PageContext pageContext = invocationContext.getPageContext(); 118 pageContext.setAttribute("errorMessage", msg); 119 throw new Exception (msg); 120 } 121 else { 122 SessionContext sessionContext = invocationContext.getSessionContext(); 123 sessionContext.setAttribute(ACCOUNT_SESSION_KEY, account); 124 } 125 } 126 127 130 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 131 JAASLoginRequestCallback requestCallback = (JAASLoginRequestCallback)callbacks[0]; 132 JAASLoginResponseCallback responseCallback = (JAASLoginResponseCallback)callbacks[1]; 133 134 String username = requestCallback.getParams().get(0); 136 String password = requestCallback.getParams().get(1); 138 139 Account account = accountBO.getAccount(username, password); 140 141 responseCallback.setCallbackObject(account); 143 responseCallback.setPrincipalName(username); 145 responseCallback.setRole(username); 147 } 148 149 @ActionMethod(successView = "index.vhtml") 150 public void doGetSignoff(InvocationContext invocationContext) throws Exception { 151 SessionContext sessionContext = invocationContext.getSessionContext(); 152 sessionContext.destroy(); 153 } 154 155 @ActionMethod(successView = "EditAccount.vhtml", errorView = "signon.vhtml") 156 public void doGetEditAccount(InvocationContext invocationContext) throws Exception { 157 SessionContext sessionContext = invocationContext.getSessionContext(); 158 Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY); 159 160 if(account == null) { 161 throw new IllegalArgumentException ("Not login, please login first!"); 162 } 163 164 PageContext pageContext = invocationContext.getPageContext(); 165 pageContext.setAttribute("account", account); 166 pageContext.setAttribute("languages", languages); 167 168 List <Category> categories = categoryBO.getCategoryList(); 169 pageContext.setAttribute("categories", categories); 170 } 171 172 @ActionMethod(successView = "index.vhtml", errorView = "EditAccount.vhtml", invocationClass = EditAccountInvocation.class) 173 public void doPostEdit(InvocationContext invocationContext) throws Exception { 174 EditAccountInvocation invocation = (EditAccountInvocation)invocationContext.getInvocation(); 175 176 SessionContext sessionContext = invocationContext.getSessionContext(); 177 Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY); 178 179 Account newAccount = new Account(); 180 newAccount.setBannerName(account.getBannerName()); 181 newAccount.setUsername(account.getUsername()); 182 newAccount.setStatus(account.getStatus()); 183 184 newAccount.setPassword(invocation.getPassword()); 185 newAccount.setAddress1(invocation.getAddress1()); 186 newAccount.setAddress2(invocation.getAddress2()); 187 newAccount.setBannerOption(invocation.getBannerOption()); 188 newAccount.setCity(invocation.getCity()); 189 newAccount.setCountry(invocation.getCountry()); 190 newAccount.setEmail(invocation.getEmail()); 191 newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId()); 192 newAccount.setFirstName(invocation.getFirstName()); 193 newAccount.setLanguagePreference(invocation.getLanguagePreference()); 194 newAccount.setLastName(invocation.getLastName()); 195 newAccount.setListOption(invocation.getListOption()); 196 newAccount.setPassword(invocation.getPassword()); 197 newAccount.setPhone(invocation.getPhone()); 198 newAccount.setState(invocation.getState()); 199 newAccount.setZip(invocation.getZip()); 200 201 try { 202 accountBO.updateAccount(newAccount); 203 sessionContext.setAttribute(ACCOUNT_SESSION_KEY, newAccount); 204 } 205 catch (Exception e) { 206 throw e; 208 } 209 } 210 211 217 protected void doActionFailed(InvocationContext invocationContext) { 218 if (invocationContext.getActionMethod().getName().equals("doPostEdit")) { 219 SessionContext sessionContext = invocationContext.getSessionContext(); 220 Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY); 221 222 PageContext pageContext = invocationContext.getPageContext(); 223 pageContext.setAttribute("account", account); 224 pageContext.setAttribute("languages", languages); 225 226 List <Category> categories = categoryBO.getCategoryList(); 227 pageContext.setAttribute("categories", categories); 228 } 229 else if(invocationContext.getActionMethod().getName().equals("doPostCreate")){ 230 NewAccountInvocation invocation = (NewAccountInvocation)invocationContext.getInvocation(); 231 Account newAccount = EntityFactory.newEntityObject(Account.class); 232 newAccount.setUsername(invocation.getUsername()); 233 newAccount.setStatus("OK"); 234 newAccount.setPassword(invocation.getPassword()); 235 newAccount.setAddress1(invocation.getAddress1()); 236 newAccount.setAddress2(invocation.getAddress2()); 237 newAccount.setBannerOption(invocation.getBannerOption()); 238 newAccount.setCity(invocation.getCity()); 239 newAccount.setCountry(invocation.getCountry()); 240 newAccount.setEmail(invocation.getEmail()); 241 newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId()); 242 newAccount.setFirstName(invocation.getFirstName()); 243 newAccount.setLanguagePreference(invocation.getLanguagePreference()); 244 newAccount.setLastName(invocation.getLastName()); 245 newAccount.setListOption(invocation.getListOption()); 246 newAccount.setPassword(invocation.getPassword()); 247 newAccount.setPhone(invocation.getPhone()); 248 newAccount.setState(invocation.getState()); 249 newAccount.setZip(invocation.getZip()); 250 PageContext pageContext = invocationContext.getPageContext(); 251 pageContext.setAttribute("account", newAccount); 252 try { 253 doGetNewAccount(invocationContext); 254 } 255 catch(Exception e) { 256 logger.error("doActionFailed error.", e); 257 } 258 } 259 } 260 261 public static class SignonInvocation extends Invocation { 262 @StringValidation(minLength = 4, nullable = false) 263 private String username; 264 265 @StringValidation(minLength = 4, nullable = false) 266 private String password; 267 268 public String getPassword() { 269 return password; 270 } 271 272 public void setPassword(String password) { 273 this.password = password; 274 } 275 276 public String getUsername() { 277 return username; 278 } 279 280 public void setUsername(String username) { 281 this.username = username; 282 } 283 } 284 285 public static class EditAccountInvocation extends Invocation { 286 287 @StringValidation(minLength = 4, nullable = false) 288 private String password; 289 290 private String repeatpassword; 291 292 private String email; 293 294 private String firstName; 295 296 private String lastName; 297 298 private String address1; 299 300 private String address2; 301 302 private String city; 303 304 private String state; 305 306 private String zip; 307 308 private String country; 309 310 private String phone; 311 312 private String favouriteCategoryId; 313 314 private String languagePreference; 315 316 private int listOption; 317 318 private int bannerOption; 319 320 public String getAddress1() { 321 return address1; 322 } 323 324 public void setAddress1(String address1) { 325 this.address1 = address1; 326 } 327 328 public String getAddress2() { 329 return address2; 330 } 331 332 public void setAddress2(String address2) { 333 this.address2 = address2; 334 } 335 336 public int getBannerOption() { 337 return bannerOption; 338 } 339 340 public void setBannerOption(int bannerOption) { 341 this.bannerOption = bannerOption; 342 } 343 344 public String getCity() { 345 return city; 346 } 347 348 public void setCity(String city) { 349 this.city = city; 350 } 351 352 public String getCountry() { 353 return country; 354 } 355 356 public void setCountry(String country) { 357 this.country = country; 358 } 359 360 public String getEmail() { 361 return email; 362 } 363 364 public void setEmail(String email) { 365 this.email = email; 366 } 367 368 public String getFavouriteCategoryId() { 369 return favouriteCategoryId; 370 } 371 372 public void setFavouriteCategoryId(String favouriteCategoryId) { 373 this.favouriteCategoryId = favouriteCategoryId; 374 } 375 376 public String getFirstName() { 377 return firstName; 378 } 379 380 public void setFirstName(String firstName) { 381 this.firstName = firstName; 382 } 383 384 public String getLanguagePreference() { 385 return languagePreference; 386 } 387 388 public void setLanguagePreference(String languagePreference) { 389 this.languagePreference = languagePreference; 390 } 391 392 public String getLastName() { 393 return lastName; 394 } 395 396 public void setLastName(String lastName) { 397 this.lastName = lastName; 398 } 399 400 public int getListOption() { 401 return listOption; 402 } 403 404 public void setListOption(int listOption) { 405 this.listOption = listOption; 406 } 407 408 public String getPassword() { 409 return password; 410 } 411 412 public void setPassword(String password) { 413 this.password = password; 414 } 415 416 public String getPhone() { 417 return phone; 418 } 419 420 public void setPhone(String phone) { 421 this.phone = phone; 422 } 423 424 public String getState() { 425 return state; 426 } 427 428 public void setState(String state) { 429 this.state = state; 430 } 431 432 public String getZip() { 433 return zip; 434 } 435 436 public void setZip(String zip) { 437 this.zip = zip; 438 } 439 440 public String getRepeatpassword() { 441 return repeatpassword; 442 } 443 444 public void setRepeatpassword(String repeatpassword) { 445 this.repeatpassword = repeatpassword; 446 } 447 448 public void validateAll() throws ValidateException { 449 if (!getPassword().equals(getRepeatpassword())) { 451 throw new ValidateException("password twice input are different.", "password", getPassword()); 452 } 453 } 454 455 } 456 457 public static class NewAccountInvocation extends EditAccountInvocation { 458 private String username; 459 460 public String getUsername() { 461 return username; 462 } 463 464 public void setUsername(String username) { 465 this.username = username; 466 } 467 } 468 469 public static void main(String [] args) { 470 471 } 472 } 473 | Popular Tags |