You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2008/10/04 00:03:19 UTC
svn commit: r701549 -
/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java
Author: ivaynberg
Date: Fri Oct 3 15:03:19 2008
New Revision: 701549
URL: http://svn.apache.org/viewvc?rev=701549&view=rev
Log:
WICKET-1696
Modified:
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java
Modified: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java?rev=701549&r1=701548&r2=701549&view=diff
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java (original)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/captcha/CaptchaImageResource.java Fri Oct 3 15:03:19 2008
@@ -34,6 +34,8 @@
import org.apache.wicket.IClusterable;
import org.apache.wicket.markup.html.image.resource.DynamicImageResource;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
import org.apache.wicket.util.time.Time;
@@ -118,8 +120,9 @@
return new String(b);
}
- private final String challengeId;
- private final List<CharAttributes> charAttsList;
+ private final IModel<String> challengeId;
+ private Integer challengeIdhashCode;
+ private List<CharAttributes> charAttsList;
private final List<String> fontNames = Arrays.asList(new String[] { "Helventica", "Arial",
"Courier" });
@@ -135,6 +138,7 @@
private int width = 0;
+
/**
* Construct.
*/
@@ -147,10 +151,22 @@
* Construct.
*
* @param challengeId
- * The id of the challenge
+ * The id of the challenge
*/
public CaptchaImageResource(String challengeId)
{
+ this(new Model<String>(challengeId));
+ }
+
+ /**
+ *
+ * Construct.
+ *
+ * @param challengeId
+ * The id of the challenge
+ */
+ public CaptchaImageResource(IModel<String> challengeId)
+ {
this(challengeId, 48, 30);
}
@@ -158,57 +174,53 @@
* Construct.
*
* @param challengeId
- * The id of the challenge
+ * The id of the challenge
* @param fontSize
- * The font size
+ * The font size
* @param margin
- * The image's margin
+ * The image's margin
*/
- public CaptchaImageResource(String challengeId, int fontSize, int margin)
+ public CaptchaImageResource(IModel<String> challengeId, int fontSize, int margin)
{
this.challengeId = challengeId;
fontStyle = 1;
this.fontSize = fontSize;
this.margin = margin;
- width = this.margin * 2;
- height = this.margin * 2;
- char[] chars = challengeId.toCharArray();
- charAttsList = new ArrayList<CharAttributes>();
- TextLayout text;
- AffineTransform textAt;
- Shape shape;
- for (int i = 0; i < chars.length; i++)
- {
- String fontName = fontNames.get(randomInt(0, fontNames.size()));
- double rotation = Math.toRadians(randomInt(-35, 35));
- int rise = randomInt(margin / 2, margin);
- Random ran = new Random();
- double shearX = ran.nextDouble() * 0.2;
- double shearY = ran.nextDouble() * 0.2;
- CharAttributes cf = new CharAttributes(chars[i], fontName, rotation, rise, shearX,
- shearY);
- charAttsList.add(cf);
- text = new TextLayout(chars[i] + "", getFont(fontName), new FontRenderContext(null,
- false, false));
- textAt = new AffineTransform();
- textAt.rotate(rotation);
- textAt.shear(shearX, shearY);
- shape = text.getOutline(textAt);
- width += (int)shape.getBounds2D().getWidth();
- if (height < (int)shape.getBounds2D().getHeight() + rise)
- {
- height = (int)shape.getBounds2D().getHeight() + rise;
- }
- }
+ }
+
+ /**
+ *
+ * Construct.
+ *
+ * @param challengeId
+ * The id of the challenge
+ * @param fontSize
+ * The font size
+ * @param margin
+ * The image's margin
+ */
+ public CaptchaImageResource(String challengeId, int fontSize, int margin)
+ {
+ this(new Model<String>(challengeId), fontSize, margin);
}
/**
* Gets the id for the challenge.
*
- * @return The the id for the challenge
+ * @return The id for the challenge
*/
public final String getChallengeId()
{
+ return challengeId.getObject();
+ }
+
+ /**
+ * Gets the id for the challenge
+ *
+ * @return The id for the challenge
+ */
+ public final IModel<String> getChallengeIdModel()
+ {
return challengeId;
}
@@ -220,6 +232,7 @@
@Override
public final void invalidate()
{
+ challengeIdhashCode = null;
imageData = null;
}
@@ -231,7 +244,8 @@
{
// get image data is always called in sync block
byte[] data = null;
- if (imageData != null)
+ if (imageData != null && challengeIdhashCode != null &&
+ challengeIdhashCode.equals(challengeId.getObject().hashCode()))
{
data = imageData.get();
}
@@ -256,6 +270,36 @@
*/
private final byte[] render()
{
+ width = margin * 2;
+ height = margin * 2;
+ char[] chars = challengeId.getObject().toCharArray();
+ charAttsList = new ArrayList<CharAttributes>();
+ TextLayout text;
+ AffineTransform textAt;
+ Shape shape;
+ for (int i = 0; i < chars.length; i++)
+ {
+ String fontName = fontNames.get(randomInt(0, fontNames.size()));
+ double rotation = Math.toRadians(randomInt(-35, 35));
+ int rise = randomInt(margin / 2, margin);
+ Random ran = new Random();
+ double shearX = ran.nextDouble() * 0.2;
+ double shearY = ran.nextDouble() * 0.2;
+ CharAttributes cf = new CharAttributes(chars[i], fontName, rotation, rise, shearX,
+ shearY);
+ charAttsList.add(cf);
+ text = new TextLayout(chars[i] + "", getFont(fontName), new FontRenderContext(null,
+ false, false));
+ textAt = new AffineTransform();
+ textAt.rotate(rotation);
+ textAt.shear(shearX, shearY);
+ shape = text.getOutline(textAt);
+ width += (int)shape.getBounds2D().getWidth();
+ if (height < (int)shape.getBounds2D().getHeight() + rise)
+ {
+ height = (int)shape.getBounds2D().getHeight() + rise;
+ }
+ }
while (true)
{
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
@@ -265,13 +309,13 @@
for (int i = 0; i < charAttsList.size(); i++)
{
CharAttributes cf = charAttsList.get(i);
- TextLayout text = new TextLayout(cf.getChar() + "", getFont(cf.getName()),
+ text = new TextLayout(cf.getChar() + "", getFont(cf.getName()),
gfx.getFontRenderContext());
- AffineTransform textAt = new AffineTransform();
+ textAt = new AffineTransform();
textAt.translate(curWidth, height - cf.getRise());
textAt.rotate(cf.getRotation());
textAt.shear(cf.getShearX(), cf.getShearY());
- Shape shape = text.getOutline(textAt);
+ shape = text.getOutline(textAt);
curWidth += shape.getBounds().getWidth();
gfx.setXORMode(Color.BLACK);
gfx.fill(shape);