001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.Jtrac;
020: import info.jtrac.domain.User;
021: import info.jtrac.util.WebUtils;
022: import javax.servlet.http.Cookie;
023: import org.apache.wicket.behavior.HeaderContributor;
024: import org.apache.wicket.markup.html.IHeaderContributor;
025: import org.apache.wicket.markup.html.IHeaderResponse;
026: import org.apache.wicket.markup.html.WebMarkupContainer;
027: import org.apache.wicket.markup.html.WebPage;
028: import org.apache.wicket.markup.html.basic.Label;
029: import org.apache.wicket.markup.html.form.CheckBox;
030: import org.apache.wicket.markup.html.form.PasswordTextField;
031: import org.apache.wicket.markup.html.form.StatelessForm;
032: import org.apache.wicket.markup.html.form.TextField;
033: import org.apache.wicket.markup.html.panel.FeedbackPanel;
034: import org.apache.wicket.model.BoundCompoundPropertyModel;
035: import org.slf4j.Logger;
036: import org.slf4j.LoggerFactory;
037:
038: /**
039: * login page
040: */
041: public class LoginPage extends WebPage {
042:
043: private static final Logger logger = LoggerFactory
044: .getLogger(LoginPage.class);
045:
046: private Jtrac getJtrac() {
047: return ((JtracApplication) getApplication()).getJtrac();
048: }
049:
050: public LoginPage() {
051: setVersioned(false);
052: add(new Label("title", getLocalizer().getString("login.title",
053: null)));
054: add(new LoginForm("form"));
055: String jtracVersion = ComponentUtils.getJtrac(this )
056: .getReleaseVersion();
057: add(new Label("version", jtracVersion));
058: }
059:
060: /**
061: * wicket form
062: */
063: private class LoginForm extends StatelessForm {
064:
065: private String loginName;
066: private String password;
067: private boolean rememberMe;
068:
069: public String getLoginName() {
070: return loginName;
071: }
072:
073: public void setLoginName(String loginName) {
074: this .loginName = loginName;
075: }
076:
077: public String getPassword() {
078: return password;
079: }
080:
081: public void setPassword(String password) {
082: this .password = password;
083: }
084:
085: public boolean isRememberMe() {
086: return rememberMe;
087: }
088:
089: public void setRememberMe(boolean rememberMe) {
090: this .rememberMe = rememberMe;
091: }
092:
093: public LoginForm(String id) {
094: super (id);
095: add(new WebMarkupContainer("hide") {
096: @Override
097: public boolean isVisible() {
098: return !LoginForm.this .hasError();
099: }
100: });
101: add(new FeedbackPanel("feedback"));
102: setModel(new BoundCompoundPropertyModel(this ));
103: final TextField loginNameField = new TextField("loginName");
104: loginNameField.setOutputMarkupId(true);
105: add(loginNameField);
106: final PasswordTextField passwordField = new PasswordTextField(
107: "password");
108: passwordField.setRequired(false);
109: passwordField.setOutputMarkupId(true);
110: add(passwordField);
111: // intelligently set focus on the appropriate textbox
112: add(new HeaderContributor(new IHeaderContributor() {
113: public void renderHead(IHeaderResponse response) {
114: String markupId;
115: if (loginNameField.getConvertedInput() == null) {
116: markupId = loginNameField.getMarkupId();
117: } else {
118: markupId = passwordField.getMarkupId();
119: }
120: response
121: .renderOnLoadJavascript("document.getElementById('"
122: + markupId + "').focus()");
123: }
124: }));
125: add(new CheckBox("rememberMe"));
126:
127: }
128:
129: @Override
130: protected void onSubmit() {
131: if (loginName == null || password == null) {
132: logger
133: .debug("login failed - login name or password is null");
134: error(getLocalizer().getString("login.error", null));
135: return;
136: }
137: User user = ((JtracApplication) getApplication())
138: .authenticate(loginName, password);
139: if (user == null) { // login failed
140: error(getLocalizer().getString("login.error", null));
141: } else { // login success
142: // remember me cookie
143: if (rememberMe) {
144: Cookie cookie = new Cookie("jtrac", loginName + ":"
145: + getJtrac().encodeClearText(password));
146: cookie.setMaxAge(30 * 24 * 60 * 60); // 30 days in seconds
147: String path = getWebRequestCycle().getWebRequest()
148: .getHttpServletRequest().getContextPath();
149: cookie.setPath(path);
150: getWebRequestCycle().getWebResponse().addCookie(
151: cookie);
152: logger
153: .debug("remember me requested, cookie added, "
154: + WebUtils
155: .getDebugStringForCookie(cookie));
156: }
157: // setup session with principal
158: ((JtracSession) getSession()).setUser(user);
159: // proceed to bookmarkable page or default dashboard
160: if (!continueToOriginalDestination()) {
161: setResponsePage(DashboardPage.class);
162: }
163: }
164: }
165:
166: }
167:
168: }
|