001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.examples.signin;
018:
019: import org.apache.wicket.PageParameters;
020: import org.apache.wicket.examples.WicketExamplePage;
021: import org.apache.wicket.markup.html.form.Form;
022: import org.apache.wicket.markup.html.form.PasswordTextField;
023: import org.apache.wicket.markup.html.form.TextField;
024: import org.apache.wicket.markup.html.panel.FeedbackPanel;
025: import org.apache.wicket.model.PropertyModel;
026: import org.apache.wicket.util.value.ValueMap;
027:
028: /**
029: * Simple example of a sign in page.
030: *
031: * @author Jonathan Locke
032: */
033: public final class SignIn extends WicketExamplePage {
034: /**
035: * Constructor
036: */
037: public SignIn() {
038: this (null);
039: }
040:
041: /**
042: * Constructor
043: *
044: * @param parameters
045: * The page parameters
046: */
047: public SignIn(final PageParameters parameters) {
048: // Create feedback panel and add to page
049: final FeedbackPanel feedback = new FeedbackPanel("feedback");
050:
051: add(feedback);
052:
053: // Add sign-in form to page, passing feedback panel as validation error
054: // handler
055: add(new SignInForm("signInForm"));
056: }
057:
058: /**
059: * Sign in form
060: *
061: * @author Jonathan Locke
062: */
063: public final class SignInForm extends Form {
064: // El-cheapo model for form
065: private final ValueMap properties = new ValueMap();
066:
067: /**
068: * Constructor
069: *
070: * @param id
071: * id of the form component
072: */
073: public SignInForm(final String id) {
074: super (id);
075:
076: // Attach textfield components that edit properties map model
077: add(new TextField("username", new PropertyModel(properties,
078: "username")));
079: add(new PasswordTextField("password", new PropertyModel(
080: properties, "password")));
081: }
082:
083: /**
084: * @see org.apache.wicket.markup.html.form.Form#onSubmit()
085: */
086: public final void onSubmit() {
087: // Get session info
088: SignInSession session = (SignInSession) getSession();
089:
090: // Sign the user in
091: if (session.authenticate(properties.getString("username"),
092: properties.getString("password"))) {
093: if (!continueToOriginalDestination()) {
094: setResponsePage(getApplication().getHomePage());
095: }
096: } else {
097: // Form method that will notify feedback panel
098: // Try the component based localizer first. If not found try the
099: // application localizer. Else use the default
100: final String errmsg = getLocalizer().getString(
101: "loginError", this , "Unable to sign you in");
102: error(errmsg);
103: }
104: }
105: }
106: }
|