cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject svn commit: r170589 - in /cocoon/branches/BRANCH_2_1_X/src/blocks/forms: conf/ java/org/apache/cocoon/forms/formmodel/ java/org/apache/cocoon/forms/validation/impl/
Date Tue, 17 May 2005 13:01:10 GMT
Author: ugo
Date: Tue May 17 06:01:09 2005
New Revision: 170589

URL: http://svn.apache.org/viewcvs?rev=170589&view=rev
Log:
Support for CAPTCHA validation

Added:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
  (with props)
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
  (with props)
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
  (with props)
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
  (with props)
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
  (with props)
Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-form.xconf
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-form.xconf
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-form.xconf?rev=170589&r1=170588&r2=170589&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-form.xconf (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-form.xconf Tue May 17 06:01:09
2005
@@ -41,6 +41,7 @@
       <widget name="struct" src="org.apache.cocoon.forms.formmodel.StructDefinitionBuilder"/>
       <widget name="union" src="org.apache.cocoon.forms.formmodel.UnionDefinitionBuilder"/>
       <widget name="group" src="org.apache.cocoon.forms.formmodel.GroupDefinitionBuilder"/>
+      <widget name="captcha" src="org.apache.cocoon.forms.formmodel.CaptchaDefinitionBuilder"/>
     </widgets>
   </forms-formmanager>
 </xconf>

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf?rev=170589&r1=170588&r2=170589&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf Tue May 17 06:01:09
2005
@@ -27,6 +27,7 @@
     <validator name="assert" class="org.apache.cocoon.forms.validation.impl.AssertValidatorBuilder"/>
     <validator name="mod10" class="org.apache.cocoon.forms.validation.impl.Mod10ValidatorBuilder"/>
     <validator name="regexp" class="org.apache.cocoon.forms.validation.impl.RegExpValidatorBuilder"/>
+    <validator name="captcha" class="org.apache.cocoon.forms.validation.impl.CaptchaValidatorBuilder"/>
   </forms-validators>
 
 </xconf>

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java?rev=170589&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
Tue May 17 06:01:09 2005
@@ -0,0 +1,53 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.formmodel;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.forms.util.DomHelper;
+import org.w3c.dom.Element;
+
+/**
+ * Builds {@link CaptchaFieldDefinition}s.
+ *
+ * @see http://www.captcha.net/
+ * @version $Id$
+ */
+public class CaptchaDefinitionBuilder extends AbstractDatatypeWidgetDefinitionBuilder implements
Contextualizable, ThreadSafe {
+    
+    private Context avalonContext;
+    
+    public void contextualize(Context context) throws ContextException {
+        this.avalonContext = context;
+    }
+
+    public WidgetDefinition buildWidgetDefinition(Element widgetElement) throws Exception
{
+        FieldDefinition definition = new CaptchaFieldDefinition(avalonContext);
+        setupDefinition(widgetElement, definition);
+        definition.makeImmutable();
+        return definition;
+    }
+
+    protected void setupDefinition(Element widgetElement, FieldDefinition definition) throws
Exception {
+        super.setupDefinition(widgetElement, definition);
+        
+        // parse "@required"
+        boolean required = DomHelper.getAttributeAsBoolean(widgetElement, "required", false);
+        definition.setRequired(required);
+    }
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
------------------------------------------------------------------------------
    svn:eol = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaDefinitionBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java?rev=170589&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
Tue May 17 06:01:09 2005
@@ -0,0 +1,81 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.formmodel;
+
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Session;
+import org.apache.cocoon.forms.Constants;
+import org.apache.cocoon.xml.XMLUtils;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+
+/**
+ * A {@link Field} for CAPTCHA validation. Upon generation, a secret random string is stored
+ * in a session attribute having the same name as the field's ID, for use by a 
+ * {@link org.apache.cocoon.forms.validation.impl.CaptchaValidator}.
+ * <br>
+ * Usage sample:
+ * <pre>
+    &lt;fd:captcha id="f1" required="true">
+      &lt;fd:label>Copy the number shown into the input field&lt;/fd:label>
+      &lt;fd:datatype base="string"/>
+      &lt;fd:validation>
+        &lt;fd:captcha/>
+      &lt;/fd:validation>
+    &lt;/fd:captcha>
+ * </pre>
+ * 
+ * @see http://www.captcha.net/
+ * @version CVS $Id$
+ */
+public class CaptchaField extends Field {
+
+    private static final String IMAGE_EL = "captcha-image";
+    public static final String SESSION_ATTR_PREFIX = "captcha-";
+    private static final String SECRET_CHARS = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
+    private static final int SECRET_LENGTH = 7;
+    private Context avalonContext;
+    
+    private String generateSecret() {
+        StringBuffer secret = new StringBuffer(SECRET_LENGTH);
+        for (int n = 0 ; n < SECRET_LENGTH ; n++) {
+            int randomnumber = (int) Math.floor(SECRET_CHARS.length() * Math.random());
+            secret.append(SECRET_CHARS.charAt(randomnumber)); 
+        }
+        return secret.toString();
+    }
+
+    public CaptchaField(FieldDefinition fieldDefinition, Context avalonContext) {
+        super(fieldDefinition);
+        this.avalonContext = avalonContext;
+    }
+
+    public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws
SAXException {
+        super.generateItemSaxFragment(contentHandler, locale);
+        contentHandler.startElement(Constants.INSTANCE_NS, IMAGE_EL, Constants.INSTANCE_PREFIX_COLON
+ IMAGE_EL, XMLUtils.EMPTY_ATTRIBUTES);
+        contentHandler.endElement(Constants.INSTANCE_NS, IMAGE_EL, Constants.INSTANCE_PREFIX_COLON
+ IMAGE_EL);
+        Map objectModel = ContextHelper.getObjectModel(this.avalonContext);
+        Session session = ObjectModelHelper.getRequest(objectModel).getSession(true);
+        session.setAttribute(SESSION_ATTR_PREFIX + getId(), generateSecret());
+    }
+    
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
------------------------------------------------------------------------------
    svn:eol = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaField.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java?rev=170589&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
Tue May 17 06:01:09 2005
@@ -0,0 +1,39 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.formmodel;
+
+import org.apache.avalon.framework.context.Context;
+
+/**
+ * A {@link FieldDefinition} for {@link CaptchaField}s.
+ * 
+ * @see http://www.captcha.net/
+ * @version CVS $Id$
+ */
+public class CaptchaFieldDefinition extends FieldDefinition {
+    
+    private Context avalonContext;
+
+    public CaptchaFieldDefinition(Context avalonContext) {
+        this.avalonContext = avalonContext;
+    }
+
+    public Widget createInstance() {
+        CaptchaField field = new CaptchaField(this, avalonContext);
+        return field;
+    }
+
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
------------------------------------------------------------------------------
    svn:eol = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/CaptchaFieldDefinition.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java?rev=170589&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
Tue May 17 06:01:09 2005
@@ -0,0 +1,65 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.validation.impl;
+
+import java.util.Map;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Session;
+import org.apache.cocoon.forms.formmodel.CaptchaField;
+import org.apache.cocoon.forms.formmodel.Widget;
+import org.apache.cocoon.forms.validation.ValidationError;
+import org.apache.cocoon.forms.validation.ValidationErrorAware;
+import org.apache.cocoon.forms.validation.WidgetValidator;
+
+
+/**
+ * A {@link org.apache.cocoon.forms.validation.WidgetValidator} that relies on a CAPTCHA
+ * test.
+ * 
+ * @see http://www.captcha.net/
+ * @version $Id$
+ */
+public class CaptchaValidator implements WidgetValidator {
+
+    private final Context avalonContext;
+
+    public CaptchaValidator(Context avalonContext) {
+        this.avalonContext = avalonContext;
+    }
+
+    public boolean validate(Widget widget) {
+        if (! (widget instanceof ValidationErrorAware)) {
+            // Invalid widget type
+            throw new IllegalArgumentException("Widget '" + widget.getRequestParameterName()
+ "' is not ValidationErrorAware");
+        }
+        Map objectModel = ContextHelper.getObjectModel(this.avalonContext);
+        Session session = ObjectModelHelper.getRequest(objectModel).getSession(false);
+        if (session == null) {
+            throw new RuntimeException("No session associated with request.");
+        }
+        if (session.getAttribute(CaptchaField.SESSION_ATTR_PREFIX + widget.getId()) == null)
{
+            throw new RuntimeException("No CAPTCHA attribute associated with widget " + widget.getId());
+        }
+        boolean result = widget.getValue() != null && widget.getValue().equals(session.getAttribute(CaptchaField.SESSION_ATTR_PREFIX
+ widget.getId()));
+        if (! result) {
+            ((ValidationErrorAware) widget).setValidationError(new ValidationError("general.captcha-mismatch"));
+        }
+        return result;
+    }
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
------------------------------------------------------------------------------
    svn:eol = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidator.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java?rev=170589&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
Tue May 17 06:01:09 2005
@@ -0,0 +1,48 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.validation.impl;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.forms.formmodel.WidgetDefinition;
+import org.apache.cocoon.forms.validation.WidgetValidator;
+import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
+import org.w3c.dom.Element;
+
+
+/**
+ * Builds a CAPTCHA validator.
+ * 
+ * @see org.apache.cocoon.forms.validation.impl.CaptchaValidator
+ * @version $Id$
+ */
+public class CaptchaValidatorBuilder implements WidgetValidatorBuilder, Contextualizable,
ThreadSafe {
+    
+    private Context avalonContext;
+
+    public void contextualize(Context context) throws ContextException {
+        System.err.println("CaptchaValidatorBuilder.contextualize(" + context + ")");
+        this.avalonContext = context;
+    }
+
+    public WidgetValidator build(Element validationRuleElement,
+            WidgetDefinition definition) throws Exception {
+        return new CaptchaValidator(avalonContext);
+    }
+
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
------------------------------------------------------------------------------
    svn:eol = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/validation/impl/CaptchaValidatorBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message