001: /*
002: * $Id: HelloWorldApplication.java 5394 2006-04-16 13:36:52 +0000 (Sun, 16 Apr
003: * 2006) jdonnerstag $ $Revision: 5394 $ $Date: 2006-04-16 13:36:52 +0000 (Sun,
004: * 16 Apr 2006) $
005: *
006: * ==================================================================== Licensed
007: * under the Apache License, Version 2.0 (the "License"); you may not use this
008: * file except in compliance with the License. You may obtain a copy of the
009: * License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.examples.captcha;
020:
021: import wicket.PageParameters;
022: import wicket.examples.WicketExamplePage;
023: import wicket.extensions.markup.html.captcha.CaptchaImageResource;
024: import wicket.markup.ComponentTag;
025: import wicket.markup.html.form.Form;
026: import wicket.markup.html.form.RequiredTextField;
027: import wicket.markup.html.image.Image;
028: import wicket.markup.html.panel.FeedbackPanel;
029: import wicket.model.PropertyModel;
030: import wicket.util.value.ValueMap;
031:
032: /**
033: * Captcha example page.
034: *
035: * @author Joshua Perlow
036: */
037: public class Captcha extends WicketExamplePage {
038: private final class CaptchaForm extends Form {
039: private static final long serialVersionUID = 1L;
040:
041: private final CaptchaImageResource captchaImageResource;
042:
043: /**
044: * Construct.
045: *
046: * @param id
047: */
048: public CaptchaForm(String id) {
049: super (id);
050:
051: captchaImageResource = new CaptchaImageResource(imagePass);
052: add(new Image("captchaImage", captchaImageResource));
053: add(new RequiredTextField("password", new PropertyModel(
054: properties, "password")) {
055: protected final void onComponentTag(
056: final ComponentTag tag) {
057: super .onComponentTag(tag);
058: // clear the field after each render
059: tag.put("value", "");
060: }
061: });
062: }
063:
064: /**
065: * @see wicket.markup.html.form.Form#onSubmit()
066: */
067: public void onSubmit() {
068: if (!imagePass.equals(getPassword())) {
069: error("Captcha password '" + getPassword()
070: + "' is wrong.\n" + "Correct password was: "
071: + imagePass);
072: } else {
073: info("Success!");
074: }
075:
076: // force redrawing
077: captchaImageResource.invalidate();
078: }
079: }
080:
081: private static final long serialVersionUID = 1L;
082:
083: private static int randomInt(int min, int max) {
084: return (int) (Math.random() * (max - min) + min);
085: }
086:
087: private static String randomString(int min, int max) {
088: int num = randomInt(min, max);
089: byte b[] = new byte[num];
090: for (int i = 0; i < num; i++)
091: b[i] = (byte) randomInt('a', 'z');
092: return new String(b);
093: }
094:
095: /** Random captcha password to match against. */
096: private String imagePass = randomString(6, 8);
097:
098: private final ValueMap properties = new ValueMap();
099:
100: /**
101: * Construct.
102: *
103: * @param parameters
104: */
105: public Captcha(final PageParameters parameters) {
106: final FeedbackPanel feedback = new FeedbackPanel("feedback");
107: add(feedback);
108: add(new CaptchaForm("captchaForm"));
109: }
110:
111: private String getPassword() {
112: return properties.getString("password");
113: }
114: }
|