[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/captcha ...
Christian Bauer
christian at hibernate.org
Tue Dec 18 23:29:24 EST 2007
User: cbauer
Date: 07/12/18 23:29:24
Modified: examples/wiki/src/main/org/jboss/seam/wiki/core/captcha
WikiCaptcha.java
Removed: examples/wiki/src/main/org/jboss/seam/wiki/core/captcha
WikiCaptchaResponseValidator.java
WikiCaptchaResponse.java
Log:
Major rewrite of the most of the application
Revision Changes Path
1.5 +88 -58 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/captcha/WikiCaptcha.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WikiCaptcha.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/captcha/WikiCaptcha.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- WikiCaptcha.java 12 Oct 2007 16:31:30 -0000 1.4
+++ WikiCaptcha.java 19 Dec 2007 04:29:24 -0000 1.5
@@ -1,83 +1,113 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Distributable under LGPL license.
- * See terms of license at gnu.org.
- */
package org.jboss.seam.wiki.core.captcha;
-import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.captcha.Captcha;
+import org.jboss.seam.captcha.CaptchaResponse;
-import java.io.Serializable;
-import java.security.SecureRandom;
-import java.util.Random;
+import java.awt.*;
+import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
/**
- * Supertrivial replacement for the broken JCaptcha stuff.
- *
- * TODO: Replace with good captcha image generator.
- *
- * @author Christian Bauer
+ * Some code borrowed here: http://www.jroller.com/mlconnor/entry/simple_captcha_jsp
*/
@Name("org.jboss.seam.captcha.captcha")
- at Scope(ScopeType.PAGE)
- at Install(precedence = Install.DEPLOYMENT)
-public class WikiCaptcha implements Serializable {
-
- private int one;
- private int two;
- private transient String response;
- private transient Random myRamdom = new SecureRandom();
-
- public String getQuestion() {
- return one + " + " + two;
- }
+ at Scope(ScopeType.SESSION)
+ at Install(precedence = Install.APPLICATION)
+public class WikiCaptcha extends Captcha {
+
+ Color backgroundColor = new Color(0xf5,0xf5, 0xf5);
+ Font textFont = new Font("Arial", Font.PLAIN, 25);
+ int charsToPrint = 6;
+ int width = 120;
+ int height = 25;
+ int circlesToDraw = 4;
+ float horizMargin = 20.0f;
+ double rotationRange = 0.2;
+ String elegibleChars = "ABDEFGHJKLMRSTUVWXYabdefhjkmnrstuvwxy23456789";
+ char[] chars = elegibleChars.toCharArray();
+ @Override
@Create
- public void reset() {
- one = myRamdom.nextInt(50);
- two = myRamdom.nextInt(50);
- }
-
- @WikiCaptchaResponse
- public String getResponse() {
- return response;
- }
+ public void init() {
+ super.init();
- public void setResponse(String input) {
- this.response = input;
+ StringBuffer finalString = new StringBuffer();
+ for (int i = 0; i < charsToPrint; i++) {
+ double randomValue = Math.random();
+ int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
+ char characterToShow = chars[randomIndex];
+ finalString.append(characterToShow);
+ }
+
+ setChallenge(finalString.toString());
+ setCorrectResponse(finalString.toString());
+ }
+
+ @Override
+ public BufferedImage renderChallenge() {
+
+ // Background
+ BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
+ g.setColor(backgroundColor);
+ g.fillRect(0, 0, width, height);
+
+ // Some obfuscation circles
+ for (int i = 0; i < circlesToDraw; i++) {
+ int circleColor = 80 + (int)(Math.random() * 70);
+ float circleLinewidth = 0.3f + (float)(Math.random());
+ g.setColor(new Color(circleColor, circleColor, circleColor));
+ g.setStroke(new BasicStroke(circleLinewidth));
+ int circleRadius = (int) (Math.random() * height / 2.0);
+ int circleX = (int) (Math.random() * width - circleRadius);
+ int circleY = (int) (Math.random() * height - circleRadius);
+ g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2);
+ }
+
+ // Text
+ g.setFont(textFont);
+ FontMetrics fontMetrics = g.getFontMetrics();
+ int maxAdvance = fontMetrics.getMaxAdvance();
+ int fontHeight = fontMetrics.getHeight();
+ float spaceForLetters = -horizMargin * 2 + width;
+ float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
+
+ char[] allChars = getChallenge().toCharArray();
+ for (int i = 0; i < allChars.length; i++ ) {
+ char charToPrint = allChars[i];
+ int charWidth = fontMetrics.charWidth(charToPrint);
+ int charDim = Math.max(maxAdvance, fontHeight);
+ int halfCharDim = (charDim / 2);
+ BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D charGraphics = charImage.createGraphics();
+ charGraphics.translate(halfCharDim, halfCharDim);
+ double angle = (Math.random() - 0.5) * rotationRange;
+ charGraphics.transform(AffineTransform.getRotateInstance(angle));
+ charGraphics.translate(-halfCharDim, -halfCharDim);
+ int charColor = 60 + (int)(Math.random() * 90);
+ charGraphics.setColor(new Color(charColor, charColor, charColor));
+ charGraphics.setFont(textFont);
+ int charX = (int) (0.5 * charDim - 0.5 * charWidth);
+ charGraphics.drawString("" + charToPrint, charX, ((charDim - fontMetrics.getAscent())/2 + fontMetrics.getAscent()));
+ float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
+ int y = ((height - charDim) / 2);
+ g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
+
+ charGraphics.dispose();
}
+ g.dispose();
- public boolean validateResponse(String response) {
- try {
- new Integer(response);
- } catch (NumberFormatException ex) {
- this.response = null;
- return false;
+ return bufferedImage;
}
- if (new Integer(one + two).equals(new Integer(response))) {
- // TODO: Fuck that, doesn't clean out the old value.... no idea why
- this.response = null;
- reset();
- return true;
- } else {
- this.response = null;
- return false;
- }
- }
-
- public static WikiCaptcha instance() {
- if (!Contexts.isPageContextActive()) {
- throw new IllegalStateException("No page context active");
- }
- return (WikiCaptcha) Component.getInstance(WikiCaptcha.class, ScopeType.PAGE);
+ @Override
+ @CaptchaResponse(message = "#{messages['lacewiki.label.VerificationError']}")
+ public String getResponse() {
+ return super.getResponse();
}
}
-
More information about the jboss-cvs-commits
mailing list