[jboss-cvs] jboss-seam/src/main/org/jboss/seam/captcha ...
Gavin King
gavin.king at jboss.com
Thu Nov 8 00:52:25 EST 2007
User: gavin
Date: 07/11/08 00:52:25
Modified: src/main/org/jboss/seam/captcha package-info.java
Captcha.java CaptchaImage.java
Log:
New, simplified CAPTCHA
Revision Changes Path
1.3 +1 -1 jboss-seam/src/main/org/jboss/seam/captcha/package-info.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: package-info.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/captcha/package-info.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- package-info.java 8 Nov 2007 03:17:19 -0000 1.2
+++ package-info.java 8 Nov 2007 05:52:25 -0000 1.3
@@ -1,5 +1,5 @@
/**
- * Integration with jcaptcha.
+ * A simple CAPTCHA algorithm that supports customization
*/
@Namespace(value="http://jboss.com/products/seam/captcha", prefix="org.jboss.seam.captcha")
package org.jboss.seam.captcha;
1.7 +53 -15 jboss-seam/src/main/org/jboss/seam/captcha/Captcha.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Captcha.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/captcha/Captcha.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- Captcha.java 6 Apr 2007 09:31:08 -0000 1.6
+++ Captcha.java 8 Nov 2007 05:52:25 -0000 1.7
@@ -1,7 +1,7 @@
package org.jboss.seam.captcha;
import java.io.Serializable;
-import java.rmi.server.UID;
+import java.util.Random;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
@@ -9,31 +9,74 @@
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.contexts.Contexts;
/**
- * Supports captcha functionality for any JSF page.
+ * Default CAPTCHA algorithm, a simple addition problem. May be
+ * extended and customized.
*
* @author Gavin King
*
*/
@Name("org.jboss.seam.captcha.captcha")
- at Scope(ScopeType.PAGE)
+ at Scope(ScopeType.SESSION)
@Install(dependencies="org.jboss.seam.captcha.captchaImage", precedence=Install.BUILT_IN)
+ at BypassInterceptors
public class Captcha implements Serializable
{
- private String id;
+ private static Random random = new Random( System.currentTimeMillis() );
+
+ private String correctResponse;
+ private String challenge;
private transient String response;
+ /**
+ * Initialize the challenge and correct response.
+ * May be overridden and customized by a subclass.
+ */
@Create
public void init()
{
- id = new UID().toString().replace(":", "-");
+ int x = random.nextInt(50);
+ int y = random.nextInt(50);
+ setCorrectResponse( Integer.toString( x + y ) );
+ setChallenge( Integer.toString(x) + " + " + Integer.toString(y) + " =" );
+ }
+
+ /**
+ * Set the challenge question
+ */
+ protected void setChallenge(String challenge)
+ {
+ this.challenge = challenge;
+ }
+
+ /**
+ * Get the challenge question
+ */
+ protected String getChallenge()
+ {
+ return challenge;
}
- public boolean validateResponse(String response)
+ /**
+ * Set the correct response
+ */
+ protected void setCorrectResponse(String correctResponse)
{
- boolean valid = CaptchaImage.instance().validateResponse(id, response);
+ this.correctResponse = correctResponse;
+ }
+
+ /**
+ * Validate that the entered response is the correct
+ * response
+ */
+ protected boolean validateResponse(String response)
+ {
+ boolean valid = response!=null &&
+ correctResponse!=null &&
+ response.trim().equals(correctResponse);
if (!valid)
{
init();
@@ -54,16 +97,11 @@
public static Captcha instance()
{
- if ( !Contexts.isPageContextActive() )
+ if ( !Contexts.isSessionContextActive() )
{
throw new IllegalStateException("No page context active");
}
- return (Captcha) Component.getInstance(Captcha.class, ScopeType.PAGE);
- }
-
- public String getId()
- {
- return id;
+ return (Captcha) Component.getInstance(Captcha.class, ScopeType.SESSION);
}
}
1.17 +9 -59 jboss-seam/src/main/org/jboss/seam/captcha/CaptchaImage.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CaptchaImage.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/captcha/CaptchaImage.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- CaptchaImage.java 8 Nov 2007 03:17:19 -0000 1.16
+++ CaptchaImage.java 8 Nov 2007 05:52:25 -0000 1.17
@@ -3,6 +3,8 @@
import static org.jboss.seam.ScopeType.APPLICATION;
import static org.jboss.seam.annotations.Install.BUILT_IN;
+import java.awt.Color;
+import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -11,7 +13,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@@ -22,8 +23,6 @@
import org.jboss.seam.web.AbstractResource;
import com.octo.captcha.service.CaptchaServiceException;
-import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
-import com.octo.captcha.service.image.ImageCaptchaService;
/**
* Provides Captcha image resources
@@ -34,13 +33,9 @@
@Scope(APPLICATION)
@Name("org.jboss.seam.captcha.captchaImage")
@BypassInterceptors
- at Install(precedence = BUILT_IN,
- classDependencies="com.octo.captcha.service.image.ImageCaptchaService")
+ at Install(precedence = BUILT_IN)
public class CaptchaImage extends AbstractResource
{
- private ImageCaptchaService service;
- private String engineName;
-
public static CaptchaImage instance()
{
if ( !Contexts.isApplicationContextActive() )
@@ -50,34 +45,6 @@
return (CaptchaImage) Contexts.getApplicationContext().get(CaptchaImage.class);
}
- public boolean validateResponse(String id, String response)
- {
- try
- {
- return service.validateResponseForID(id, response);
- }
- catch (CaptchaServiceException cse)
- {
- return false;
- }
- }
-
- @Create
- public void create()
- {
- if (service == null)
- {
- service = createService();
- }
- }
-
- protected ImageCaptchaService createService()
- {
- DefaultManageableImageCaptchaService result = new DefaultManageableImageCaptchaService();
- result.setCaptchaEngineClass(engineName);
- return result;
- }
-
@Override
public String getResourcePath()
{
@@ -93,8 +60,12 @@
ServletLifecycle.beginRequest(request);
try
{
- String captchaId = request.getQueryString();
- BufferedImage challenge = service.getImageChallengeForID( captchaId, request.getLocale() );
+ BufferedImage challenge = new BufferedImage(70, 20, BufferedImage.TYPE_BYTE_GRAY);
+ Graphics graphics = challenge.getGraphics();
+ graphics.setColor(Color.WHITE);
+ graphics.fillRect(0, 0, 70, 20);
+ graphics.setColor(Color.BLACK);
+ graphics.drawString( Captcha.instance().getChallenge() , 5, 15 );
ImageIO.write(challenge, "jpeg", out);
}
catch (IllegalArgumentException e)
@@ -121,25 +92,4 @@
response.getOutputStream().close();
}
- @Deprecated
- public ImageCaptchaService getService()
- {
- return service;
- }
-
- @Deprecated
- public void setService(ImageCaptchaService service)
- {
- this.service = service;
- }
-
- public String getEngineName()
- {
- return engineName;
- }
-
- public void setEngineName(String engineName)
- {
- this.engineName = engineName;
- }
}
More information about the jboss-cvs-commits
mailing list