001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.petstore.action;
008:
009: import java.io.IOException;
010: import java.util.ArrayList;
011: import java.util.List;
012: import javax.ejb.EJB;
013: import javax.security.auth.callback.Callback;
014: import javax.security.auth.callback.CallbackHandler;
015: import javax.security.auth.callback.UnsupportedCallbackException;
016:
017: import org.jfox.ejb3.security.JAASLoginRequestCallback;
018: import org.jfox.ejb3.security.JAASLoginResponseCallback;
019: import org.jfox.ejb3.security.JAASLoginService;
020: import org.jfox.entity.EntityFactory;
021: import org.jfox.framework.annotation.Inject;
022: import org.jfox.framework.annotation.Service;
023: import org.jfox.mvc.ActionContext;
024: import org.jfox.mvc.ActionSupport;
025: import org.jfox.mvc.Invocation;
026: import org.jfox.mvc.PageContext;
027: import org.jfox.mvc.SessionContext;
028: import org.jfox.mvc.annotation.ActionMethod;
029: import org.jfox.mvc.validate.StringValidation;
030: import org.jfox.mvc.validate.ValidateException;
031: import org.jfox.petstore.bo.AccountBO;
032: import org.jfox.petstore.bo.CategoryBO;
033: import org.jfox.petstore.entity.Account;
034: import org.jfox.petstore.entity.Category;
035:
036: /**
037: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
038: */
039: @Service(id="account")
040: public class AccountAction extends ActionSupport implements
041: CallbackHandler {
042:
043: @Inject
044: JAASLoginService loginService;
045:
046: @EJB
047: AccountBO accountBO;
048:
049: @EJB
050: CategoryBO categoryBO;
051:
052: public static final String ACCOUNT_SESSION_KEY = "__ACCOUNT__";
053:
054: private static List<String> languages = new ArrayList<String>();
055:
056: static {
057: languages.add("English");
058: // languages.add("French");
059: languages.add("Chinese");
060: }
061:
062: @ActionMethod(name="newaccount",successView="NewAccountForm.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
063: public void doGetNewAccount(ActionContext actionContext)
064: throws Exception {
065: // do nothing
066: PageContext pageContext = actionContext.getPageContext();
067: pageContext.setAttribute("languages", languages);
068:
069: List<Category> categories = categoryBO.getCategoryList();
070: pageContext.setAttribute("categories", categories);
071: }
072:
073: @ActionMethod(name="create",successView="index.vhtml",errorView="NewAccountForm.vhtml",invocationClass=NewAccountInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
074: public void doPostCreate(ActionContext actionContext)
075: throws Exception {
076: NewAccountInvocation invocation = (NewAccountInvocation) actionContext
077: .getInvocation();
078: Account newAccount = EntityFactory
079: .newEntityObject(Account.class);
080: newAccount.setUsername(invocation.getUsername());
081: newAccount.setStatus("OK");
082: newAccount.setPassword(invocation.getPassword());
083: newAccount.setAddress1(invocation.getAddress1());
084: newAccount.setAddress2(invocation.getAddress2());
085: newAccount.setBannerOption(invocation.getBannerOption());
086: newAccount.setCity(invocation.getCity());
087: newAccount.setCountry(invocation.getCountry());
088: newAccount.setEmail(invocation.getEmail());
089: newAccount.setFavouriteCategoryId(invocation
090: .getFavouriteCategoryId());
091: newAccount.setFirstName(invocation.getFirstName());
092: newAccount.setLanguagePreference(invocation
093: .getLanguagePreference());
094: newAccount.setLastName(invocation.getLastName());
095: newAccount.setListOption(invocation.getListOption());
096: newAccount.setPassword(invocation.getPassword());
097: newAccount.setPhone(invocation.getPhone());
098: newAccount.setState(invocation.getState());
099: newAccount.setZip(invocation.getZip());
100:
101: try {
102: accountBO.insertAccount(newAccount);
103: } catch (Exception e) {
104: // update failed
105: throw e;
106: }
107: }
108:
109: @ActionMethod(name="signon",successView="signon.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
110: public void doGetSignon(ActionContext actionContext)
111: throws Exception {
112: // don't need do anything, just forward to successView
113: }
114:
115: @ActionMethod(name="signon",successView="index.vhtml",errorView="signon.vhtml",invocationClass=SignonInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
116: public void doPostSignon(ActionContext actionContext)
117: throws Exception {
118: SignonInvocation invocation = (SignonInvocation) actionContext
119: .getInvocation();
120:
121: Account account = (Account) loginService.login(actionContext
122: .getSessionContext(), this , invocation.getUsername(),
123: invocation.getPassword());
124: if (account == null) {
125: String msg = "Invalid username or password. Signon failed";
126: PageContext pageContext = actionContext.getPageContext();
127: pageContext.setAttribute("errorMessage", msg);
128: throw new Exception(msg);
129: } else {
130: SessionContext sessionContext = actionContext
131: .getSessionContext();
132: sessionContext.setAttribute(ACCOUNT_SESSION_KEY, account);
133: }
134: }
135:
136: /**
137: * JAAS CallbackHandler method
138: */
139: public void handle(Callback[] callbacks) throws IOException,
140: UnsupportedCallbackException {
141: JAASLoginRequestCallback requestCallback = (JAASLoginRequestCallback) callbacks[0];
142: JAASLoginResponseCallback responseCallback = (JAASLoginResponseCallback) callbacks[1];
143:
144: // first parameter is username
145: String username = requestCallback.getParams().get(0);
146: // second parameter is password
147: String password = requestCallback.getParams().get(1);
148:
149: Account account = accountBO.getAccount(username, password);
150:
151: // set callback object, will return by LoginService.login
152: responseCallback.setCallbackObject(account);
153: // set principal name
154: responseCallback.setPrincipalName(username);
155: // set role
156: responseCallback.setRole(username);
157: }
158:
159: @ActionMethod(name="signoff",successView="index.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
160: public void doGetSignoff(ActionContext actionContext)
161: throws Exception {
162: SessionContext sessionContext = actionContext
163: .getSessionContext();
164: sessionContext.clearAttributes();
165: actionContext.destroySessionContext();
166: }
167:
168: @ActionMethod(name="editaccount",successView="EditAccount.vhtml",errorView="signon.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
169: public void doGetEditAccount(ActionContext actionContext)
170: throws Exception {
171: SessionContext sessionContext = actionContext
172: .getSessionContext();
173: Account account = (Account) sessionContext
174: .getAttribute(ACCOUNT_SESSION_KEY);
175:
176: if (account == null) {
177: throw new IllegalArgumentException(
178: "Not login, please login first!");
179: }
180:
181: PageContext pageContext = actionContext.getPageContext();
182: pageContext.setAttribute("account", account);
183: pageContext.setAttribute("languages", languages);
184:
185: List<Category> categories = categoryBO.getCategoryList();
186: pageContext.setAttribute("categories", categories);
187: }
188:
189: @ActionMethod(name="edit",successView="index.vhtml",errorView="EditAccount.vhtml",invocationClass=EditAccountInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
190: public void doPostEdit(ActionContext actionContext)
191: throws Exception {
192: EditAccountInvocation invocation = (EditAccountInvocation) actionContext
193: .getInvocation();
194:
195: SessionContext sessionContext = actionContext
196: .getSessionContext();
197: Account account = (Account) sessionContext
198: .getAttribute(ACCOUNT_SESSION_KEY);
199:
200: Account newAccount = new Account();
201: newAccount.setBannerName(account.getBannerName());
202: newAccount.setUsername(account.getUsername());
203: newAccount.setStatus(account.getStatus());
204:
205: newAccount.setPassword(invocation.getPassword());
206: newAccount.setAddress1(invocation.getAddress1());
207: newAccount.setAddress2(invocation.getAddress2());
208: newAccount.setBannerOption(invocation.getBannerOption());
209: newAccount.setCity(invocation.getCity());
210: newAccount.setCountry(invocation.getCountry());
211: newAccount.setEmail(invocation.getEmail());
212: newAccount.setFavouriteCategoryId(invocation
213: .getFavouriteCategoryId());
214: newAccount.setFirstName(invocation.getFirstName());
215: newAccount.setLanguagePreference(invocation
216: .getLanguagePreference());
217: newAccount.setLastName(invocation.getLastName());
218: newAccount.setListOption(invocation.getListOption());
219: newAccount.setPassword(invocation.getPassword());
220: newAccount.setPhone(invocation.getPhone());
221: newAccount.setState(invocation.getState());
222: newAccount.setZip(invocation.getZip());
223:
224: try {
225: accountBO.updateAccount(newAccount);
226: sessionContext
227: .setAttribute(ACCOUNT_SESSION_KEY, newAccount);
228: } catch (Exception e) {
229: // update failed
230: throw e;
231: }
232: }
233:
234: /**
235: * 在 doPostEdit �生异常时,通过该方法设置 PageContext account,
236: * 以便跳转到 errorView 时,�以预设数�
237: *
238: * @param actionContext invocationContext
239: */
240: protected void doActionFailed(ActionContext actionContext) {
241: if (actionContext.getActionMethod().getName().equals(
242: "doPostEdit")) {
243: SessionContext sessionContext = actionContext
244: .getSessionContext();
245: Account account = (Account) sessionContext
246: .getAttribute(ACCOUNT_SESSION_KEY);
247:
248: PageContext pageContext = actionContext.getPageContext();
249: pageContext.setAttribute("account", account);
250: pageContext.setAttribute("languages", languages);
251:
252: List<Category> categories = categoryBO.getCategoryList();
253: pageContext.setAttribute("categories", categories);
254: } else if (actionContext.getActionMethod().getName().equals(
255: "doPostCreate")) {
256: NewAccountInvocation invocation = (NewAccountInvocation) actionContext
257: .getInvocation();
258: Account newAccount = EntityFactory
259: .newEntityObject(Account.class);
260: newAccount.setUsername(invocation.getUsername());
261: newAccount.setStatus("OK");
262: newAccount.setPassword(invocation.getPassword());
263: newAccount.setAddress1(invocation.getAddress1());
264: newAccount.setAddress2(invocation.getAddress2());
265: newAccount.setBannerOption(invocation.getBannerOption());
266: newAccount.setCity(invocation.getCity());
267: newAccount.setCountry(invocation.getCountry());
268: newAccount.setEmail(invocation.getEmail());
269: newAccount.setFavouriteCategoryId(invocation
270: .getFavouriteCategoryId());
271: newAccount.setFirstName(invocation.getFirstName());
272: newAccount.setLanguagePreference(invocation
273: .getLanguagePreference());
274: newAccount.setLastName(invocation.getLastName());
275: newAccount.setListOption(invocation.getListOption());
276: newAccount.setPassword(invocation.getPassword());
277: newAccount.setPhone(invocation.getPhone());
278: newAccount.setState(invocation.getState());
279: newAccount.setZip(invocation.getZip());
280: PageContext pageContext = actionContext.getPageContext();
281: pageContext.setAttribute("account", newAccount);
282: try {
283: doGetNewAccount(actionContext);
284: } catch (Exception e) {
285: logger.error("doActionFailed error.", e);
286: }
287: }
288: }
289:
290: public static class SignonInvocation extends Invocation {
291: @StringValidation(minLength=4,nullable=false)
292: private String username;
293:
294: @StringValidation(minLength=4,nullable=false)
295: private String password;
296:
297: public String getPassword() {
298: return password;
299: }
300:
301: public void setPassword(String password) {
302: this .password = password;
303: }
304:
305: public String getUsername() {
306: return username;
307: }
308:
309: public void setUsername(String username) {
310: this .username = username;
311: }
312: }
313:
314: public static class EditAccountInvocation extends Invocation {
315:
316: @StringValidation(minLength=4,nullable=false)
317: private String password;
318:
319: private String repeatpassword;
320:
321: private String email;
322:
323: private String firstName;
324:
325: private String lastName;
326:
327: private String address1;
328:
329: private String address2;
330:
331: private String city;
332:
333: private String state;
334:
335: private String zip;
336:
337: private String country;
338:
339: private String phone;
340:
341: private String favouriteCategoryId;
342:
343: private String languagePreference;
344:
345: private int listOption;
346:
347: private int bannerOption;
348:
349: public String getAddress1() {
350: return address1;
351: }
352:
353: public void setAddress1(String address1) {
354: this .address1 = address1;
355: }
356:
357: public String getAddress2() {
358: return address2;
359: }
360:
361: public void setAddress2(String address2) {
362: this .address2 = address2;
363: }
364:
365: public int getBannerOption() {
366: return bannerOption;
367: }
368:
369: public void setBannerOption(int bannerOption) {
370: this .bannerOption = bannerOption;
371: }
372:
373: public String getCity() {
374: return city;
375: }
376:
377: public void setCity(String city) {
378: this .city = city;
379: }
380:
381: public String getCountry() {
382: return country;
383: }
384:
385: public void setCountry(String country) {
386: this .country = country;
387: }
388:
389: public String getEmail() {
390: return email;
391: }
392:
393: public void setEmail(String email) {
394: this .email = email;
395: }
396:
397: public String getFavouriteCategoryId() {
398: return favouriteCategoryId;
399: }
400:
401: public void setFavouriteCategoryId(String favouriteCategoryId) {
402: this .favouriteCategoryId = favouriteCategoryId;
403: }
404:
405: public String getFirstName() {
406: return firstName;
407: }
408:
409: public void setFirstName(String firstName) {
410: this .firstName = firstName;
411: }
412:
413: public String getLanguagePreference() {
414: return languagePreference;
415: }
416:
417: public void setLanguagePreference(String languagePreference) {
418: this .languagePreference = languagePreference;
419: }
420:
421: public String getLastName() {
422: return lastName;
423: }
424:
425: public void setLastName(String lastName) {
426: this .lastName = lastName;
427: }
428:
429: public int getListOption() {
430: return listOption;
431: }
432:
433: public void setListOption(int listOption) {
434: this .listOption = listOption;
435: }
436:
437: public String getPassword() {
438: return password;
439: }
440:
441: public void setPassword(String password) {
442: this .password = password;
443: }
444:
445: public String getPhone() {
446: return phone;
447: }
448:
449: public void setPhone(String phone) {
450: this .phone = phone;
451: }
452:
453: public String getState() {
454: return state;
455: }
456:
457: public void setState(String state) {
458: this .state = state;
459: }
460:
461: public String getZip() {
462: return zip;
463: }
464:
465: public void setZip(String zip) {
466: this .zip = zip;
467: }
468:
469: public String getRepeatpassword() {
470: return repeatpassword;
471: }
472:
473: public void setRepeatpassword(String repeatpassword) {
474: this .repeatpassword = repeatpassword;
475: }
476:
477: public void validateAll() throws ValidateException {
478: //验è¯?密ç ?是å?¦ä¸€è‡´
479: if (!getPassword().equals(getRepeatpassword())) {
480: throw new ValidateException(
481: "password twice input are different.",
482: "password", getPassword());
483: }
484: }
485:
486: }
487:
488: public static class NewAccountInvocation extends
489: EditAccountInvocation {
490: private String username;
491:
492: public String getUsername() {
493: return username;
494: }
495:
496: public void setUsername(String username) {
497: this .username = username;
498: }
499: }
500:
501: public static void main(String[] args) {
502:
503: }
504: }
|