View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.pages.login;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import fr.ifremer.quadrige3.synchro.server.application.WebSession;
29  import fr.ifremer.quadrige3.synchro.server.pages.BasePage;
30  import fr.ifremer.quadrige3.synchro.server.pages.home.HomePage;
31  import fr.ifremer.quadrige3.synchro.server.security.SecurityContextHelper;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.wicket.ajax.AjaxRequestTarget;
35  import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
36  import org.apache.wicket.markup.html.basic.Label;
37  import org.apache.wicket.markup.html.form.Form;
38  import org.apache.wicket.markup.html.form.PasswordTextField;
39  import org.apache.wicket.markup.html.form.RequiredTextField;
40  import org.apache.wicket.model.CompoundPropertyModel;
41  import org.apache.wicket.model.IModel;
42  import org.apache.wicket.model.StringResourceModel;
43  import org.apache.wicket.request.mapper.parameter.PageParameters;
44  
45  /**
46   * <p>LoginPage class.</p>
47   *
48   */
49  public class LoginPage extends BasePage {
50  
51      private final static Log log = LogFactory.getLog(LoginPage.class);
52      private static final long serialVersionUID = 1L;
53  
54      private String username;
55      private String password;
56      private String error;
57      private Label errorLabel;
58  
59      /**
60       * <p>Constructor for LoginPage.</p>
61       *
62       * @param parameters a {@link org.apache.wicket.request.mapper.parameter.PageParameters} object.
63       */
64      public LoginPage(final PageParameters parameters) {
65          super(parameters);
66  
67          if (SecurityContextHelper.isAuthenticateNotAnonymous()) {
68              setResponsePage(HomePage.class);
69              return;
70          }
71  
72          final Form<LoginPage> form = new Form<>("form", new CompoundPropertyModel<>(this));
73          add(form);
74  
75          // Username
76          RequiredTextField<String> usernameText = new RequiredTextField<>("username");
77          //usernameText.add(new FocusOnLoadBehavior());
78          form.add(usernameText);
79  
80          // Password
81          form.add(new PasswordTextField("password"));
82  
83          // Submit button
84          IndicatingAjaxButton submitButton = new IndicatingAjaxButton("submit") {
85              private static final long serialVersionUID = 6186054816945761563L;
86  
87              @Override
88              protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
89                  try {
90                      form.process(null);
91                      if (WebSession.get().signIn(username, password)) {
92                          continueToOriginalDestination();
93                          setResponsePage(HomePage.class);
94                      } else {
95                          error = getString("login.failed");
96                          log.error(error);
97                      }
98                  } catch (Exception ex) {
99                      error = getString("login.failed") + " : " + ex.getLocalizedMessage();
100                     log.error(error);
101                     target.add(errorLabel);
102                 }
103             }
104         };
105         form.setDefaultButton(submitButton);
106         form.add(submitButton);
107 
108         // Error label
109         errorLabel = new Label("error");
110         errorLabel.setOutputMarkupId(true);
111         form.add(errorLabel);
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     protected IModel<String> getTitleModel() {
117         return new StringResourceModel("login.title", this, null);
118     }
119 
120 }