Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/Captcha.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/Captcha.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/Captcha.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; + +import org.xwiki.component.annotation.ComponentRole; + +/** + * The Captcha interface allows us to check if an answer to a captcha was correct. + * + * @version $Id $ + * @since Future + */ +@ComponentRole() +public interface Captcha +{ + /** + * Check if the solution to the captcha is correct. + * + * @param answer The provided solution. + * @return true if the solution is correct. + */ + boolean isAnswerCorrect(String answer); +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/Captcha.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultSoundCaptchaAction.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultSoundCaptchaAction.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultSoundCaptchaAction.java (revision 0) @@ -0,0 +1,49 @@ +/* + * 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.sound.SoundCaptchaService; +import com.octo.captcha.service.sound.DefaultManageableSoundCaptchaService; + +/** + * The DefaultSoundCaptchaAction creates a wav file with a word which is spelled + * out loud by a speech synthesizer. + * + * @version $Id $ + * @since Future + */ +@Component("defaultSoundCaptcha") +public class DefaultSoundCaptchaAction extends AbstractSoundCaptchaAction +{ + /** The service which provides the captcha. */ + private static final SoundCaptchaService SERVICE = new DefaultManageableSoundCaptchaService(); + + /** + * {@inheritDoc} + * + * @see AbstractCaptchaAction#getCaptchaService() + */ + SoundCaptchaService getCaptchaService() + { + return SERVICE; + } +} Property changes on: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultSoundCaptchaAction.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native Index: core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractCaptchaAction.java =================================================================== --- core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractCaptchaAction.java (revision 0) +++ core/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/AbstractCaptchaAction.java (revision 0) @@ -0,0 +1,129 @@ +/* + * 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.HttpServletResponse; +import javax.servlet.http.HttpServletRequest; + +import org.xwiki.component.annotation.Requirement; +import org.xwiki.component.logging.AbstractLogEnabled; +import org.xwiki.container.Container; +import org.xwiki.container.Request; +import org.xwiki.container.Response; +import org.xwiki.container.servlet.ServletRequest; +import org.xwiki.container.servlet.ServletResponse; +import org.xwiki.action.Action; +import org.xwiki.action.ActionException; +import org.xwiki.captcha.Captcha; + +import com.octo.captcha.service.CaptchaServiceException; +import com.octo.captcha.module.config.CaptchaModuleConfigHelper; +import com.octo.captcha.service.CaptchaService; + +/** + * The AbstractCaptchaAction handles things which are needed by all types of captchas. + * + * @param The type of captcha service for the implementation. + * @version $Id $ + * @since Future + */ +public abstract class AbstractCaptchaAction + extends AbstractLogEnabled + implements Action, Captcha +{ + /** The error message to throw if the contaier is not a ServletContainer. */ + private static final String NOT_SERVLET_ERROR = "Getting captcha only supported with servlet"; + + /** For getting the request and response. */ + @Requirement + private Container container; + + /** + * {@inheritDoc} + * + * @see Action#execute() + */ + public abstract void execute() throws ActionException; + + /** + * {@inheritDoc} + * + * @see Action#execute(Object) + */ + public void execute(Object additionalData) throws ActionException + { + execute(); + } + + /** + * {@inheritDoc} + * + * @see Captcha#isAnswerCorrect(String) + */ + public boolean isAnswerCorrect(String answer) + { + try { + return getCaptchaService().validateResponseForID(getId(), answer); + } catch (CaptchaServiceException e) { + getLogger().debug("Tried to solve a captcha which had not yet been loaded."); + } catch (Exception e) { + getLogger().debug("Tried to solve a captcha with a browser which doesn't support session cookies."); + } + return false; + } + + /** + * @return The session id. + * @throws Exception If the session is null (browser doesn't support session cookies) + */ + String getId() throws Exception + { + return CaptchaModuleConfigHelper.getId(getHttpServletRequest()); + } + + /** @return The CaptchaService which supplies the captchas */ + abstract T getCaptchaService(); + + /** + * @return The HttpServletRequest + * @throws Exception if request is not a ServletRequest + */ + HttpServletRequest getHttpServletRequest() throws Exception + { + Request xrequest = this.container.getRequest(); + if (!(xrequest instanceof ServletRequest)) { + throw new Exception(NOT_SERVLET_ERROR); + } + return ((ServletRequest) xrequest).getHttpServletRequest(); + } + + /** + * @return The HttpServletResponse + * @throws Exception if response is not a ServletResponse + */ + HttpServletResponse getHttpServletResponse() throws Exception + { + Response xresponse = this.container.getResponse(); + if (!(xresponse instanceof ServletResponse)) { + throw new Exception(NOT_SERVLET_ERROR); + } + return ((ServletResponse) xresponse).getHttpServletResponse(); + } +} 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,63 @@ +/* + * 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 java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + +import org.xwiki.action.ActionException; + +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 Action#execute() + */ + public void execute() throws ActionException + { + try { + HttpServletRequest request = getHttpServletRequest(); + this.helper.flushNewCaptchaToResponse(request, + getHttpServletResponse(), + null, + getCaptchaService(), + getId(), + request.getLocale()); + } catch (IOException e) { + getLogger().warn("Couldn't create captcha due to io exception", e.getMessage()); + } catch (Exception e) { + getLogger().warn("Couldn't create captcha due to exception", e.getMessage()); + } + } +} 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,47 @@ +/* + * 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.module.struts.CaptchaServicePlugin; + +/** + * The DefaultImageCaptchaAction creates a simple generic jpeg image captcha. + * DefaultImageCaptchaAction can take answers to captchas gotten from + * com.octo.captcha.module.struts.image.RenderImageCaptchaAction + * + * @version $Id $ + * @since Future + */ +@Component("defaultImageCaptcha") +public class DefaultImageCaptchaAction extends AbstractImageCaptchaAction +{ + /** + * {@inheritDoc} + * + * @see AbstractCaptchaAction#getCaptchaService() + */ + ImageCaptchaService getCaptchaService() + { + return (ImageCaptchaService) CaptchaServicePlugin.getInstance().getService(); + } +} 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,71 @@ +/* + * 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.Captcha; + +/** + * Provides access to the classes implementing Captcha. + * + * @version $Id $ + * @since future + */ +public class VelocityCaptchaService +{ + /** A Map of all captchas by their names. */ + private Map captchas; + + /** + * The Constructor. + * + * @param captchas A Map of registered components which implement Captcha by their names + */ + public VelocityCaptchaService(Map captchas) + { + this.captchas = captchas; + } + + /** + * Check if the solution to the captcha is correct. + * + * @param captchaName The name of the Captcha you are checking the answer against + * @param answer The provided solution + * @return true if the solution is correct + * @throws UnsupportedOperationException If there is no captcha by the name captchaName + */ + public boolean isAnswerCorrect(String captchaName, String answer) throws UnsupportedOperationException + { + Captcha captcha = captchas.get(captchaName); + if (captcha == null) { + throw new UnsupportedOperationException("There is no captcha registered by the name " + captchaName); + } + return captcha.isAnswerCorrect(answer); + } + + /** @return a List of the names of all registered classes implementing {@link org.xwiki.captcha.Captcha}. */ + public List listCaptchaNames() + { + return new ArrayList(captchas.keySet()); + } +} 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,56 @@ +/* + * 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.Captcha; +import org.xwiki.velocity.VelocityContextInitializer; + +import org.apache.velocity.VelocityContext; + +/** + * Loads VelocityCaptchaService into the Velocity context. + * + * @version $Id $ + * @since future + */ +@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 = Captcha.class) + private Map captchas; + + /** + * {@inheritDoc} + * + * @see org.xwiki.velocity.VelocityContextInitializer#initialize(VelocityContext) + */ + public void initialize(VelocityContext context) + { + context.put(VELOCITY_CONTEXT_KEY, new VelocityCaptchaService(captchas)); + } +} 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/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,63 @@ +/* + * 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 java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + +import org.xwiki.action.ActionException; + +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 Action#execute() + */ + public void execute() throws ActionException + { + try { + HttpServletRequest request = getHttpServletRequest(); + this.helper.flushNewCaptchaToResponse(request, + getHttpServletResponse(), + null, + getCaptchaService(), + getId(), + request.getLocale()); + } catch (IOException e) { + getLogger().warn("Couldn't create captcha due to io exception", e.getMessage()); + } catch (Exception e) { + getLogger().warn("Couldn't create captcha due to exception", e.getMessage()); + } + } +} 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,93 @@ + + + + + + 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-velocity + ${pom.version} + + + + org.xwiki.platform + xwiki-core-container-api + ${pom.version} + + + + org.xwiki.platform + xwiki-core-container-servlet + ${pom.version} + + + + com.octo.captcha + jcaptcha-all + 1.0-RC6 + + + + javax.servlet + servlet-api + 2.4 + provided + + + + + + + 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