Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aestasit.gwtibatis.server.handlers;
- import javax.naming.NamingException;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.aestasit.gwtibatis.Client;
- import com.aestasit.gwtibatis.UserService;
- import com.aestasit.gwtibatis.UserServiceRemote;
- import com.aestasit.gwtibatis.exceptions.AuthenticationException;
- import com.aestasit.gwtibatis.model.User;
- import com.aestasit.gwtibatis.shared.LoginAction;
- import com.aestasit.gwtibatis.shared.LoginActionResult;
- import com.aestasit.gwtibatis.shared.sec.CurrentUser;
- import com.gwtplatform.dispatch.server.ExecutionContext;
- import com.gwtplatform.dispatch.server.actionhandler.AbstractActionHandler;
- import com.gwtplatform.dispatch.shared.ActionException;
- public class LoginHandler extends
- AbstractActionHandler<LoginAction, LoginActionResult> {
- @Autowired
- private ServletContext servletContext;
- @Autowired
- private UserService userService;
- @Autowired
- private HttpServletRequest servletRequest;
- public LoginHandler() {
- super(LoginAction.class);
- }
- @Override
- public LoginActionResult execute(LoginAction action,
- ExecutionContext context) throws ActionException {
- String username = action.getUsername();
- String password = action.getPassword();
- LoginActionResult result = null;
- User u = null;
- if (!action.getEjb()) {
- try {
- u = userService.login(username, password);
- } catch (AuthenticationException ae) {
- result = new LoginActionResult(null);
- }
- } else {
- try {
- UserServiceRemote bean = (UserServiceRemote) new Client()
- .getContext()
- .lookup("gwtbatis-ear/gwtibatis-ejb/UserServiceEJB!com.aestasit.gwtibatis.UserServiceRemote");
- u = bean.login(username, password);
- } catch (NamingException e) {
- result = new LoginActionResult(null);
- } catch (AuthenticationException ae) {
- result = new LoginActionResult(null);
- }
- }
- if(u!=null){
- HttpSession s = servletRequest.getSession(true);
- s.setAttribute("currentUserName", u.getUsername());
- CurrentUser cu = new CurrentUser();
- cu.setUsername(u.getUsername());
- cu.setId(u.getId());
- cu.setEmail(u.getEmail());
- result = new LoginActionResult(cu);
- }
- return result;
- }
- @Override
- public Class<LoginAction> getActionType() {
- return LoginAction.class;
- }
- @Override
- public void undo(LoginAction action, LoginActionResult result,
- ExecutionContext context) throws ActionException {
- // Not undoable
- }
- }
Add Comment
Please, Sign In to add comment