View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.login;
5   
6   import java.util.Set;
7   
8   import javax.validation.ConstraintViolation;
9   
10  import org.appfuse.webapp.client.application.Application;
11  import org.appfuse.webapp.client.application.base.activity.AbstractBaseActivity;
12  import org.appfuse.webapp.client.application.base.security.LoginEvent;
13  import org.appfuse.webapp.client.ui.login.LoginView.LoginDetails;
14  
15  import com.github.gwtbootstrap.client.ui.Alert;
16  import com.github.gwtbootstrap.client.ui.constants.AlertType;
17  import com.google.gwt.editor.client.EditorDriver;
18  import com.google.gwt.event.shared.EventBus;
19  import com.google.gwt.http.client.Request;
20  import com.google.gwt.http.client.RequestBuilder;
21  import com.google.gwt.http.client.RequestCallback;
22  import com.google.gwt.http.client.RequestException;
23  import com.google.gwt.http.client.Response;
24  import com.google.gwt.http.client.URL;
25  import com.google.gwt.user.client.Window;
26  import com.google.gwt.user.client.ui.AcceptsOneWidget;
27  import com.google.inject.Inject;
28  import com.google.web.bindery.requestfactory.shared.Receiver;
29  
30  /**
31   * @author ivangsa
32   *
33   */
34  public class LoginActivity extends AbstractBaseActivity implements LoginView.Delegate {
35  
36      private final LoginView view;
37  
38      @Inject
39      public LoginActivity(final Application application, final LoginView view) {
40          super(application);
41          this.view = view;
42          setTitle(i18n.login_title());
43          setBodyId("login");
44          setBodyClassname("login");
45      }
46  
47      /**
48       * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
49       *      com.google.gwt.event.shared.EventBus)
50       */
51      @Override
52      public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
53          view.setDelegate(this);
54          view.setRememberMeEnabled(application.isRememberMeEnabled());
55          view.setWaiting(false);
56          view.setMessage(null);
57          panel.setWidget(view);
58          setDocumentTitleAndBodyAttributtes();
59      }
60  
61      @Override
62      public void onLoginClick() {
63          view.setMessage(null);
64          final EditorDriver<LoginDetails> editorDriver = view.getEditorDriver();
65          final LoginDetails login = editorDriver.flush();
66          final Set<ConstraintViolation<LoginDetails>> violations = getValidator().validate(login);
67          if (!violations.isEmpty()) {
68              editorDriver.setConstraintViolations((Set) violations);
69              return;
70          }
71          final RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, "j_security_check");
72          requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");
73          requestBuilder.setHeader("X-Requested-With", "XMLHttpRequest");
74          try {
75              view.setWaiting(true);
76              requestBuilder.sendRequest(createLoginPostData(login), new RequestCallback() {
77  
78                  @Override
79                  public void onResponseReceived(final Request request, final Response response) {
80                      final int statusCode = response.getStatusCode();
81                      if (statusCode == Response.SC_OK) {
82                          eventBus.fireEvent(new LoginEvent());
83                      }
84                      else if (statusCode == Response.SC_FORBIDDEN || statusCode == Response.SC_UNAUTHORIZED) {
85                          view.setWaiting(false);
86                          view.setMessage(new Alert(i18n.errors_password_mismatch(), AlertType.ERROR, false));
87                      }
88                      else {
89                          throw new RuntimeException("Login could not understand response code: " + statusCode);
90                      }
91                  }
92  
93                  @Override
94                  public void onError(final Request request, final Throwable exception) {
95                      view.setWaiting(false);
96                      Window.alert("Response error " + exception.getMessage());
97                  }
98              });
99          } catch (final RequestException e) {
100             throw new RuntimeException(e);
101         }
102     }
103 
104     private String createLoginPostData(final LoginView.LoginDetails login) {
105         return "j_username=" + URL.encodeQueryString(login.getUsername()) +
106                 "&j_password=" + URL.encodeQueryString(login.getPassword()) +
107                 (login.isRememberMe() ? "&_spring_security_remember_me=on" : "");
108     }
109 
110     @Override
111     public void onPasswordHintClick() {
112         final LoginDetails login = view.getEditorDriver().flush();
113         final String username = login.getUsername();
114         if (username == null || "".equals(username.trim())) {
115             Window.alert(i18n.errors_required(i18n.user_username()));
116             return;
117         }
118         requests.userRequest().sendPasswordHint(login.getUsername()).fire(new Receiver<String>() {
119             @Override
120             public void onSuccess(final String userEmail) {
121                 Alert message = null;
122                 if (userEmail != null) {
123                     message = new Alert(i18n.login_passwordHint_sent(username, userEmail), AlertType.SUCCESS);
124                 } else {
125                     message = new Alert(i18n.login_passwordHint_error(username), AlertType.ERROR);
126                 }
127                 if (message != null) {
128                     shell.addMessage(message);
129                 }
130             }
131         });
132     }
133 
134     @Override
135     public void onRequestPasswordRecoveryClick() {
136         final LoginDetails login = view.getEditorDriver().flush();
137         final String username = login.getUsername();
138         if (username == null || "".equals(username.trim())) {
139             Window.alert(i18n.errors_required(i18n.user_username()));
140             return;
141         }
142         requests.userRequest().requestRecoveryToken(login.getUsername()).fire(new Receiver<String>() {
143             @Override
144             public void onSuccess(final String message) {
145                 if (message != null) {
146                     shell.addMessage(message, AlertType.SUCCESS);
147                 }
148             }
149         });
150     }
151 
152     @Override
153     public void onCancelClick() {
154 
155     }
156 }