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 */
017package org.apache.wicket.markup.html.form;
018
019import org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.model.IModel;
021
022
023/**
024 * A password text field component. As you type, characters show up as asterisks or some other such
025 * character so that nobody can look over your shoulder and read your password.
026 * <p>
027 * By default this text field is required. If it is not, call {@link #setRequired(boolean)} with
028 * value of <code>false</code>.
029 * <p>
030 * Note that by default the model object is nullified after each request to prevent the entered
031 * password to be serialized along with the containing page, see {@link #setResetPassword(boolean)}
032 * for details.
033 * 
034 * @author Jonathan Locke
035 */
036public class PasswordTextField extends TextField<String>
037{
038        private static final long serialVersionUID = 1L;
039
040        /**
041         * Should password be reset, see {@link #setResetPassword(boolean)}.
042         */
043        private boolean resetPassword = true;
044
045        /**
046         * @see org.apache.wicket.Component#Component(String)
047         */
048        public PasswordTextField(final String id)
049        {
050                this(id, null);
051        }
052
053        /**
054         * @param id
055         * @param model
056         * @see org.apache.wicket.Component#Component(String, IModel)
057         */
058        public PasswordTextField(final String id, IModel<String> model)
059        {
060                super(id, model);
061                setRequired(true);
062                setType(String.class);
063        }
064
065        /**
066         * Should password be reset, see {@link #setResetPassword(boolean)}.
067         * 
068         * @return should password be reset
069         */
070        public final boolean getResetPassword()
071        {
072                return resetPassword;
073        }
074
075        /**
076         * Flag indicating whether the password should be reset after each request.
077         * Additionally, any present value is not rendered into the markup.
078         * <br>
079         * If <code>true</code>, the model object is set to null after each request to prevent it
080         * being serialized along with the containing page. This is default and highly recommended
081         * for login forms. If <code>false</code> the model value is handled as in a standard
082         * {@link TextField}, this is useful for entry forms where the contents of the model should
083         * be editable, or resubmitted.
084         * 
085         * @param resetPassword
086         *            The resetPassword to set.
087         * @return <code>this</code>.
088         */
089        public final PasswordTextField setResetPassword(final boolean resetPassword)
090        {
091                this.resetPassword = resetPassword;
092                return this;
093        }
094
095        /**
096         * Processes the component tag.
097         * 
098         * @param tag
099         *            Tag to modify
100         * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
101         */
102        @Override
103        protected void onComponentTag(final ComponentTag tag)
104        {
105                super.onComponentTag(tag);
106                if (getResetPassword())
107                {
108                        tag.put("value", "");
109                }
110        }
111
112        @Override
113        protected String[] getInputTypes()
114        {
115                return new String[] {"password"};
116        }
117
118        /**
119         * Overridden to nullify the password.
120         */
121        @Override
122        protected void onDetach()
123        {
124                if (resetPassword) {
125                        clearInput();
126
127                        if (getModel() != null) {
128                                getModel().setObject(null);
129                        }
130                }
131
132                super.onDetach();
133        }
134}