[jboss-cvs] jboss-seam/src/main/org/jboss/seam/captcha ...
Gavin King
gavin.king at jboss.com
Thu Nov 8 01:30:04 EST 2007
User: gavin
Date: 07/11/08 01:30:04
Modified: src/main/org/jboss/seam/captcha Captcha.java
CaptchaImage.java
Log:
improvements
Revision Changes Path
1.8 +40 -3 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.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- Captcha.java 8 Nov 2007 05:52:25 -0000 1.7
+++ Captcha.java 8 Nov 2007 06:30:04 -0000 1.8
@@ -1,5 +1,8 @@
package org.jboss.seam.captcha;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Random;
@@ -21,7 +24,7 @@
*/
@Name("org.jboss.seam.captcha.captcha")
@Scope(ScopeType.SESSION)
- at Install(dependencies="org.jboss.seam.captcha.captchaImage", precedence=Install.BUILT_IN)
+ at Install(precedence=Install.BUILT_IN)
@BypassInterceptors
public class Captcha implements Serializable
{
@@ -29,7 +32,7 @@
private String correctResponse;
private String challenge;
- private transient String response;
+ private String response;
/**
* Initialize the challenge and correct response.
@@ -72,7 +75,7 @@
* Validate that the entered response is the correct
* response
*/
- protected boolean validateResponse(String response)
+ public boolean validateResponse(String response)
{
boolean valid = response!=null &&
correctResponse!=null &&
@@ -95,6 +98,40 @@
this.response = input;
}
+ /**
+ * Render the challenge question as an image.
+ * May be overridden by subclasses to achieve
+ * a stronger CAPTCHA.
+ */
+ public BufferedImage renderChallenge()
+ {
+ BufferedImage challenge = new BufferedImage(70, 20, BufferedImage.TYPE_BYTE_GRAY);
+ Graphics graphics = challenge.getGraphics();
+ graphics.setColor( getChallengeBackgroundColor() );
+ graphics.fillRect(0, 0, 70, 20);
+ graphics.setColor( getChallengeTextColor() );
+ graphics.drawString( getChallenge() , 5, 15 );
+ return challenge;
+ }
+
+ /**
+ * May be overridden by subclasses
+ * @return the background color of the challenge image
+ */
+ protected Color getChallengeBackgroundColor()
+ {
+ return Color.WHITE;
+ }
+
+ /**
+ * May be overridden by subclasses
+ * @return @return the foreground color of the challenge image
+ */
+ protected Color getChallengeTextColor()
+ {
+ return Color.BLACK;
+ }
+
public static Captcha instance()
{
if ( !Contexts.isSessionContextActive() )
1.18 +2 -11 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.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- CaptchaImage.java 8 Nov 2007 05:52:25 -0000 1.17
+++ CaptchaImage.java 8 Nov 2007 06:30:04 -0000 1.18
@@ -3,9 +3,6 @@
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;
@@ -25,7 +22,7 @@
import com.octo.captcha.service.CaptchaServiceException;
/**
- * Provides Captcha image resources
+ * Serves CAPTCHA images
*
* @author Shane Bryzak
*/
@@ -60,13 +57,7 @@
ServletLifecycle.beginRequest(request);
try
{
- 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);
+ ImageIO.write( Captcha.instance().renderChallenge(), "jpeg", out );
}
catch (IllegalArgumentException e)
{
More information about the jboss-cvs-commits
mailing list