Index: web/standard/pom.xml =================================================================== --- web/standard/pom.xml (revision 26266) +++ web/standard/pom.xml (working copy) @@ -91,6 +91,12 @@ 1.5.8 runtime + + + org.xwiki.platform + xwiki-core-captcha + ${platform.core.version} + Index: web/standard/src/main/webapp/WEB-INF/struts-config.xml =================================================================== --- web/standard/src/main/webapp/WEB-INF/struts-config.xml (revision 26266) +++ web/standard/src/main/webapp/WEB-INF/struts-config.xml (working copy) @@ -402,8 +402,8 @@ - + The type of captcha service for the implementation. + * @version $Id $ + * @since Future + */ +public abstract class AbstractCaptchaAction extends Action + implements CaptchaVerifier +{ + /** + * {@inheritDoc} + * + * @see CaptchaVerifier#isAnswerCorrect(String, String) + */ + public boolean isAnswerCorrect(String userId, String answer) + { + return getCaptchaService().validateResponseForID(userId, answer); + } + + /** + * {@inheritDoc} + * + * @see CaptchaVerifier#getUserId() + */ + public String getUserId(HttpServletRequest request) + { + return request.getSession().getId(); + } + + /** @return The CaptchaService which supplies the captchas */ + abstract T getCaptchaService(); +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractCaptchaAction.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractImageCaptchaAction.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractImageCaptchaAction.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractImageCaptchaAction.java (revision 0) @@ -0,0 +1,60 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +import com.octo.captcha.module.web.image.ImageToJpegHelper; +import com.octo.captcha.service.image.ImageCaptchaService; + +/** + * Extensions of AbstractImageCaptchaAction create jpeg image captchas. + * + * @version $Id $ + * @since Future + */ +public abstract class AbstractImageCaptchaAction extends AbstractCaptchaAction +{ + /** Easily puts a captcha from the captchaService to the response object as a jpeg image. */ + private final ImageToJpegHelper helper = new ImageToJpegHelper(); + + /** + * {@inheritDoc} + * + * @see AbstractCaptchaAction#execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) + */ + public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, + HttpServletRequest request, HttpServletResponse response) + throws Exception + { + this.helper.flushNewCaptchaToResponse(request, + response, + null, + getCaptchaService(), + getUserId(request), + request.getLocale()); + return null; + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractImageCaptchaAction.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultImageCaptchaAction.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultImageCaptchaAction.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultImageCaptchaAction.java (revision 0) @@ -0,0 +1,48 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal; + +import org.xwiki.component.annotation.Component; + +import com.octo.captcha.service.image.ImageCaptchaService; +import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; + +/** + * The DefaultImageCaptchaAction creates a simple generic jpeg image captcha. + * + * @version $Id $ + * @since Future + */ +@Component("image") +public class DefaultImageCaptchaAction extends AbstractImageCaptchaAction +{ + /** The service which provides the captcha, must be static because struts instanciates per request. */ + private static final ImageCaptchaService SERVICE = new DefaultManageableImageCaptchaService(); + + /** + * {@inheritDoc} + * + * @see AbstractCaptchaAction#getCaptchaService() + */ + ImageCaptchaService getCaptchaService() + { + return SERVICE; + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultImageCaptchaAction.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaService.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaService.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaService.java (revision 0) @@ -0,0 +1,89 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal.velocity; + +import java.util.List; +import java.util.ArrayList; +import java.util.Map; + +import org.xwiki.captcha.CaptchaVerifier; +import org.xwiki.configuration.ConfigurationSource; + +/** + * Provides access to the classes implementing Captcha. + * + * @version $Id$ + * @since 2.1M2 + */ +public class VelocityCaptchaService +{ + /** The configuration key for the. */ + private static final String IS_CAPTCHA_ENABLED_CFG_KEY = "captcha.internal.velocity.VelocityCaptchaService.enabled"; + + /** A Map of all captchas by their names. */ + private Map captchas; + + /** A ConfigurationSource in which to look for whether the captcha should be enabled. */ + private ConfigurationSource configuration; + + /** + * The Constructor. + * + * @param captchas A Map of registered components which implement Captcha by their names + * @param configuration A ConfigurationSource in which to look for whether the captcha should be enabled + */ + public VelocityCaptchaService(Map captchas, ConfigurationSource configuration) + { + this.captchas = captchas; + this.configuration = configuration; + } + + /** + * Get a {@link org.xwiki.captcha.CaptchaVerifier} from the service. + * + * @param captchaName The name of the type of captcha, use {@link #listCaptchaNames()} for a list + * @return A captcha object which can be used to check a specific answer for a given challange + * @throws CaptchaVerifierNotFoundException If the named VaptchaVerifier could not be found. + */ + public CaptchaVerifier getCaptchaVerifier(String captchaName) throws CaptchaVerifierNotFoundException + { + if (captchas.get(captchaName) == null) { + throw new CaptchaVerifierNotFoundException(captchaName); + } + return new VelocityCaptchaVerifier(captchas.get(captchaName)); + } + + /** @return a List of the names of all registered classes implementing {@link org.xwiki.captcha.CaptchaVerifier}. */ + public List listCaptchaNames() + { + return new ArrayList(captchas.keySet()); + } + + /** + * If this is false, the captcha system will still work. + * It is for velocity scripts to determine whether captchas are needed. + * + * @return Is the captcha service enabled in the configuration. + */ + public boolean isEnabled() + { + return configuration.getProperty(IS_CAPTCHA_ENABLED_CFG_KEY, Boolean.FALSE).booleanValue(); + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaService.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVelocityContextInitializer.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVelocityContextInitializer.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVelocityContextInitializer.java (revision 0) @@ -0,0 +1,61 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal.velocity; + +import java.util.Map; + +import org.xwiki.component.annotation.Component; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.captcha.CaptchaVerifier; +import org.xwiki.velocity.VelocityContextInitializer; +import org.xwiki.configuration.ConfigurationSource; + +import org.apache.velocity.VelocityContext; + +/** + * Loads VelocityCaptchaService into the Velocity context. + * + * @version $Id$ + * @since 2.1M2 + */ +@Component("captchaservice") +public class CaptchaVelocityContextInitializer implements VelocityContextInitializer +{ + /** The key to use for the captcha in the velocity context. */ + public static final String VELOCITY_CONTEXT_KEY = "captchaservice"; + + /** A Map of all captchas by their hint. */ + @Requirement(role = CaptchaVerifier.class) + private Map captchas; + + /** A ConfigurationSource in which to look for whether the captcha should be enabled. */ + @Requirement + private ConfigurationSource configuration; + + /** + * {@inheritDoc} + * + * @see org.xwiki.velocity.VelocityContextInitializer#initialize(VelocityContext) + */ + public void initialize(VelocityContext context) + { + context.put(VELOCITY_CONTEXT_KEY, new VelocityCaptchaService(captchas, configuration)); + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVelocityContextInitializer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVerifierNotFoundException.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVerifierNotFoundException.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVerifierNotFoundException.java (revision 0) @@ -0,0 +1,40 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal.velocity; + +/** + * Thrown when the velocity programmer tries to get a CaptchaVerifier which doesn't exist. + * + * @version $Id$ + * @since 2.1M2 + */ +public class CaptchaVerifierNotFoundException extends Exception +{ + /** + * The Constructor. + * + * @param captchaName The name of the CaptchaVerifier which was not found. + */ + public CaptchaVerifierNotFoundException(String captchaName) + { + super("The CaptchaVerifier you are trying to get (" + captchaName + + ") doesn't exist in the list, try $captchaservice.listCaptchaNames()"); + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/CaptchaVerifierNotFoundException.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaVerifier.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaVerifier.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaVerifier.java (revision 0) @@ -0,0 +1,74 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal.velocity; + +import javax.servlet.http.HttpServletRequest; + +import org.xwiki.captcha.CaptchaVerifier; + +/** + * VelocityCaptchaVerifier wraps a CaptchaVerifier object and passes all calls to it and returns from it. + * It catches any Exceptions because Velocity is unable to catch exceptions and it never returns null + * because Velocity will not make an assignment if the output is null. + * + * @version $Id $ + * @since future + */ +public class VelocityCaptchaVerifier implements CaptchaVerifier +{ + /** The internal captcha which this VelocityCaptcha wraps. */ + private final CaptchaVerifier wrappedCaptcha; + + /** + * The Constructor. + * + * @param toWrap A Captcha object which this VelocityCaptcha should wrap. + */ + public VelocityCaptchaVerifier(CaptchaVerifier toWrap) + { + this.wrappedCaptcha = toWrap; + } + + /** + * {@inheritDoc} + * + * @see CaptchaVerifier#getUserId(HttpServletRequest) + */ + public String getUserId(HttpServletRequest request) + { + String x = wrappedCaptcha.getUserId(request); + // If getUserId returns null, we should return "" because velocity will not assign null. + return (x == null) ? "" : x; + } + + /** + * {@inheritDoc} + * + * @see CaptchaVerifier#isAnswerCorrect(String, String) + */ + public boolean isAnswerCorrect(String userId, String answer) + { + try { + return (userId.length() == 0) ? false : wrappedCaptcha.isAnswerCorrect(userId, answer); + } catch (Exception e) { + return false; + } + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/velocity/VelocityCaptchaVerifier.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractSoundCaptchaAction.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractSoundCaptchaAction.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractSoundCaptchaAction.java (revision 0) @@ -0,0 +1,60 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.captcha.internal; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +import com.octo.captcha.module.web.sound.SoundToWavHelper; +import com.octo.captcha.service.sound.SoundCaptchaService; + +/** + * Extensions of AbstractSoundCaptchaAction create wav audio captchas. + * + * @version $Id $ + * @since Future + */ +public abstract class AbstractSoundCaptchaAction extends AbstractCaptchaAction +{ + /** Easily puts a captcha from the captchaService to the response object as a jpeg image. */ + private final SoundToWavHelper helper = new SoundToWavHelper(); + + /** + * {@inheritDoc} + * + * @see AbstractCaptchaAction#execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) + */ + public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, + HttpServletRequest request, HttpServletResponse response) + throws Exception + { + this.helper.flushNewCaptchaToResponse(request, + response, + null, + getCaptchaService(), + getUserId(request), + request.getLocale()); + return null; + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractSoundCaptchaAction.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/resources/META-INF/components.txt =================================================================== --- core/xwiki-captcha/src/main/resources/META-INF/components.txt (revision 0) +++ core/xwiki-captcha/src/main/resources/META-INF/components.txt (revision 0) @@ -0,0 +1,2 @@ +org.xwiki.captcha.internal.DefaultImageCaptchaAction +org.xwiki.captcha.internal.velocity.CaptchaVelocityContextInitializer Property changes on: core/xwiki-captcha/src/main/resources/META-INF/components.txt ___________________________________________________________________ Name: svn:eol-style + native Index: core/xwiki-captcha/pom.xml =================================================================== --- core/xwiki-captcha/pom.xml (revision 0) +++ core/xwiki-captcha/pom.xml (revision 0) @@ -0,0 +1,105 @@ + + + + + + 4.0.0 + + org.xwiki.platform + xwiki-core-parent + 2.2-SNAPSHOT + + xwiki-core-captcha + XWiki Platform - Core - Captcha + jar + XWiki Platform - Core - Captcha + + + + org.xwiki.platform + xwiki-core-component-api + ${pom.version} + + + + org.xwiki.platform + xwiki-core-action + ${pom.version} + + + + org.xwiki.platform + xwiki-core-configuration-api + ${pom.version} + + + + org.xwiki.platform + xwiki-core-velocity + ${pom.version} + + + + com.octo.captcha + jcaptcha-api + 2.0-alpha-1-SNAPSHOT + + + + com.octo.captcha + jcaptcha + 2.0-alpha-1-SNAPSHOT + + + + com.octo.captcha + jcaptcha-extension-sound-freetts + 2.0-alpha-1-SNAPSHOT + + + + javax.servlet + servlet-api + 2.4 + provided + + + + struts + struts + 1.2.9 + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + + Property changes on: core/xwiki-captcha/pom.xml ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native