01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.validation.impl;
18:
19: import org.apache.cocoon.forms.formmodel.Widget;
20: import org.apache.cocoon.forms.validation.ValidationError;
21: import org.apache.cocoon.forms.validation.ValidationErrorAware;
22: import org.apache.cocoon.forms.validation.WidgetValidator;
23:
24: /**
25: * A {@link org.apache.cocoon.forms.validation.WidgetValidator} that relies on a CAPTCHA
26: * test.
27: *
28: * @see <a href="http://www.captcha.net">www.captcha.net</a>
29: * @version $Id: CaptchaValidator.java 449149 2006-09-23 03:58:05Z crossley $
30: */
31: public class CaptchaValidator implements WidgetValidator {
32:
33: private static final String VALIDATION_MESSAGE_KEY = "validation.captcha.mismatch";
34:
35: public boolean validate(Widget widget) {
36: if (!(widget instanceof ValidationErrorAware)) {
37: // Invalid widget type
38: throw new IllegalArgumentException("Widget '"
39: + widget.getRequestParameterName()
40: + "' is not ValidationErrorAware");
41: }
42: boolean result = widget.getValue() != null
43: && widget.getValue().equals(
44: widget.getAttribute("secret"));
45: if (!result) {
46: ((ValidationErrorAware) widget)
47: .setValidationError(new ValidationError(
48: VALIDATION_MESSAGE_KEY));
49: }
50: return result;
51: }
52: }
|