Guest User

Untitled

a guest
Nov 19th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package com.aestasit.gwtibatis.server.handlers;
  2.  
  3. import javax.naming.NamingException;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpSession;
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;
  9.  
  10. import com.aestasit.gwtibatis.Client;
  11. import com.aestasit.gwtibatis.UserService;
  12. import com.aestasit.gwtibatis.UserServiceRemote;
  13. import com.aestasit.gwtibatis.exceptions.AuthenticationException;
  14. import com.aestasit.gwtibatis.model.User;
  15. import com.aestasit.gwtibatis.shared.LoginAction;
  16. import com.aestasit.gwtibatis.shared.LoginActionResult;
  17. import com.aestasit.gwtibatis.shared.sec.CurrentUser;
  18. import com.gwtplatform.dispatch.server.ExecutionContext;
  19. import com.gwtplatform.dispatch.server.actionhandler.AbstractActionHandler;
  20. import com.gwtplatform.dispatch.shared.ActionException;
  21.  
  22. public class LoginHandler extends
  23. AbstractActionHandler<LoginAction, LoginActionResult> {
  24.  
  25. @Autowired
  26. private ServletContext servletContext;
  27.  
  28. @Autowired
  29. private UserService userService;
  30.  
  31. @Autowired
  32. private HttpServletRequest servletRequest;
  33.  
  34. public LoginHandler() {
  35. super(LoginAction.class);
  36. }
  37.  
  38. @Override
  39. public LoginActionResult execute(LoginAction action,
  40. ExecutionContext context) throws ActionException {
  41.  
  42. String username = action.getUsername();
  43. String password = action.getPassword();
  44. LoginActionResult result = null;
  45. User u = null;
  46. if (!action.getEjb()) {
  47.  
  48. try {
  49. u = userService.login(username, password);
  50.  
  51. } catch (AuthenticationException ae) {
  52. result = new LoginActionResult(null);
  53. }
  54. } else {
  55. try {
  56. UserServiceRemote bean = (UserServiceRemote) new Client()
  57. .getContext()
  58. .lookup("gwtbatis-ear/gwtibatis-ejb/UserServiceEJB!com.aestasit.gwtibatis.UserServiceRemote");
  59. u = bean.login(username, password);
  60. } catch (NamingException e) {
  61. result = new LoginActionResult(null);
  62. } catch (AuthenticationException ae) {
  63. result = new LoginActionResult(null);
  64. }
  65.  
  66. }
  67. if(u!=null){
  68. HttpSession s = servletRequest.getSession(true);
  69. s.setAttribute("currentUserName", u.getUsername());
  70. CurrentUser cu = new CurrentUser();
  71. cu.setUsername(u.getUsername());
  72. cu.setId(u.getId());
  73. cu.setEmail(u.getEmail());
  74. result = new LoginActionResult(cu);
  75. }
  76. return result;
  77. }
  78.  
  79. @Override
  80. public Class<LoginAction> getActionType() {
  81. return LoginAction.class;
  82. }
  83.  
  84. @Override
  85. public void undo(LoginAction action, LoginActionResult result,
  86. ExecutionContext context) throws ActionException {
  87. // Not undoable
  88. }
  89.  
  90. }
Add Comment
Please, Sign In to add comment