You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/04/24 07:47:29 UTC
svn commit: r651161 - in /myfaces/tomahawk/trunk/sandbox/core/src/main:
java/org/apache/myfaces/custom/captcha/
java/org/apache/myfaces/custom/captcha/servlet/
java/org/apache/myfaces/custom/captcha/util/ tld/
Author: lu4242
Date: Wed Apr 23 22:47:20 2008
New Revision: 651161
URL: http://svn.apache.org/viewvc?rev=651161&view=rev
Log:
fix TOMAHAWK-1238 CAPTCHA enhancement add width and height
Modified:
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java Wed Apr 23 22:47:20 2008
@@ -25,36 +25,80 @@
import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
-public class CAPTCHAComponent extends UIComponentBase {
+public class CAPTCHAComponent extends UIComponentBase {
- public static final String ATTRIBUTE_CAPTCHASESSIONKEYNAME = "captchaSessionKeyName";
- public static String COMPONENT_TYPE = "org.apache.myfaces.CAPTCHA";
- public static String RENDERER_TYPE = "org.apache.myfaces.CAPTCHA";
- private String _captchaSessionKeyName;
-
- public String getFamily() {
- return "org.apache.myfaces.CAPTCHA";
+ public static String COMPONENT_TYPE = "org.apache.myfaces.CAPTCHA";
+ public static String RENDERER_TYPE = "org.apache.myfaces.CAPTCHA";
+
+ // Value binding constants
+ public static final String ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME = "captchaSessionKeyName";
+ public static final String ATTRIBUTE_IMAGE_WIDTH = "imageWidth";
+ public static final String ATTRIBUTE_IMAGE_HEIGHT = "imageHeight";
+
+ private String _captchaSessionKeyName;
+ private String _imageWidth;
+ private String _imageHeight;
+
+ public String getFamily() {
+ return "org.apache.myfaces.CAPTCHA";
+ }
+
+ public String getCaptchaSessionKeyName() {
+ if (_captchaSessionKeyName != null)
+ {
+ return _captchaSessionKeyName;
}
-
- public String getCaptchaSessionKeyName() {
- if (_captchaSessionKeyName != null)
- return _captchaSessionKeyName;
-
- ValueBinding vb = getValueBinding("captchaSessionKeyName");
- return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb)
- : null;
+
+ ValueBinding vb = getValueBinding(ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME);
+ return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb)
+ : null;
+ }
+
+ public String getImageWidth() {
+ if (_imageWidth != null)
+ {
+ return _imageWidth;
}
- public Object saveState(FacesContext context) {
- Object values[] = new Object[2];
- values[0] = super.saveState(context);
- values[1] = _captchaSessionKeyName;
- return ((values));
+ ValueBinding vb = getValueBinding(ATTRIBUTE_IMAGE_WIDTH);
+ return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb)
+ : null;
+ }
+
+ public String getImageHeight() {
+ if (_imageHeight != null)
+ {
+ return _imageHeight;
}
- public void restoreState(FacesContext context, Object state) {
- Object values[] = (Object[]) state;
- super.restoreState(context, values[0]);
- _captchaSessionKeyName = (String) values[1];
- }
+ ValueBinding vb = getValueBinding(ATTRIBUTE_IMAGE_HEIGHT);
+ return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb)
+ : null;
+ }
+
+ public void setImageWidth(String width) {
+ this._imageWidth = width;
+ }
+
+ public void setImageHeight(String height) {
+ this._imageHeight = height;
+ }
+
+
+ public Object saveState(FacesContext context) {
+ Object values[] = new Object[4];
+ values[0] = super.saveState(context);
+ values[1] = _captchaSessionKeyName;
+ values[2] = _imageWidth;
+ values[3] = _imageHeight;
+ return ((values));
+ }
+
+ public void restoreState(FacesContext context, Object state) {
+ Object values[] = (Object[]) state;
+ super.restoreState(context, values[0]);
+ _captchaSessionKeyName = (String) values[1];
+ _imageWidth = (String) values[2];
+ _imageHeight = (String) values[3];
+ }
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java Wed Apr 23 22:47:20 2008
@@ -36,6 +36,7 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.myfaces.component.html.util.ParameterResourceHandler;
+import org.apache.myfaces.custom.captcha.util.CAPTCHAConstants;
import org.apache.myfaces.custom.captcha.util.CAPTCHAImageGenerator;
import org.apache.myfaces.custom.captcha.util.CAPTCHAResponseStream;
import org.apache.myfaces.custom.captcha.util.CAPTCHATextGenerator;
@@ -47,117 +48,150 @@
public class CAPTCHARenderer extends Renderer implements ResourceLoader {
- public void encodeBegin(FacesContext context, UIComponent component)
- throws IOException {
+ public void encodeBegin(FacesContext context, UIComponent component)
+ throws IOException {
- CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
-
- generateImageTag(context, captchaComponent);
- }
-
- public void encodeEnd(FacesContext context, UIComponent component)
- throws IOException {
- super.encodeEnd(context, component);
- }
-
- /*
- * This helper method is used for generating the img tag that will
- * use the (AddResource) to generate the url of the generated image.
- */
- private void generateImageTag(FacesContext context, CAPTCHAComponent component)
- throws IOException {
-
- AddResource addResource;
- String url;
- ResponseWriter writer = context.getResponseWriter();
- Map params = ComponentUtils.getParameterMap(component);
- String captchaSessionKeyName = component.getCaptchaSessionKeyName();
-
- writer.startElement(HTML.IMG_ELEM, component);
-
- if (captchaSessionKeyName != null) {
- params.put(CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME,
- captchaSessionKeyName);
- }
-
- addResource = AddResourceFactory.getInstance(context);
-
- url = context.getExternalContext().encodeResourceURL(
- addResource.getResourceUri(context,
- new ParameterResourceHandler(this.getClass(), params)));
-
- writer.writeAttribute(HTML.SRC_ATTR, url, null);
+ CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
- writer.endElement(HTML.IMG_ELEM);
+ generateImageTag(context, captchaComponent);
+ }
+
+ public void encodeEnd(FacesContext context, UIComponent component)
+ throws IOException {
+ super.encodeEnd(context, component);
+ }
+
+ /*
+ * This helper method is used for generating the img tag that will
+ * use the (AddResource) to generate the url of the generated image.
+ */
+ private void generateImageTag(FacesContext context,
+ CAPTCHAComponent component) throws IOException {
+
+ AddResource addResource;
+ ResponseWriter writer = context.getResponseWriter();
+ Map params = ComponentUtils.getParameterMap(component);
+ String url;
+ String captchaSessionKeyName = component.getCaptchaSessionKeyName();
+ String width = component.getImageWidth();
+ String height = component.getImageHeight();
+
+ // determine width and height of the generated image.
+ if(width == null)
+ {
+ width = CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH + "";
+ }
+
+ if(height == null)
+ {
+ height = CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT + "";
+ }
+
+ writer.startElement(HTML.IMG_ELEM, component);
+
+ // constructing the parameter map to be passed to the (AddResource).
+ if (captchaSessionKeyName != null)
+ {
+ params.put(CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME,
+ captchaSessionKeyName);
}
+
+ // write the url to trigger the (AddResource).
+ addResource = AddResourceFactory.getInstance(context);
+
+ url = context.getExternalContext().encodeResourceURL(
+ addResource.getResourceUri(context,
+ new ParameterResourceHandler(this.getClass(), params)));
+
+
+ writer.writeAttribute(HTML.SRC_ATTR, url, null);
+ // write rest of attributes.
+ writer.writeAttribute(HTML.WIDTH_ATTR, width, null);
+ writer.writeAttribute(HTML.HEIGHT_ATTR, height, null);
+
+ writer.endElement(HTML.IMG_ELEM);
+ }
+
/*
* This method is implemented to be called from the (AddResource).
* It wraps the CAPTCHA image generation.
*/
- public void serveResource(ServletContext servletContext,
- HttpServletRequest request, HttpServletResponse response,
- String resourceUri) throws IOException {
-
- // get the FacesContext from the ServletContext.
- FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder
- .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
- LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
- .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
- Lifecycle lifecycle = lifecycleFactory.getLifecycle(ComponentUtils
- .getLifecycleId(servletContext));
- FacesContext facesContext = facesContextFactory.getFacesContext(
- servletContext, request, response, lifecycle);
- facesContext.setResponseStream(new CAPTCHAResponseStream(response
- .getOutputStream()));
-
- // render the CAPTCHA.
- try {
- try {
- renderCAPTCHA(facesContext);
- } catch (IOException e) {
- throw new FacesException("Could not render the CAPTCHA : "
- + e.getMessage(), e);
- }
- facesContext.getResponseStream().close();
- } finally {
- facesContext.release();
- }
+ public void serveResource(ServletContext servletContext,
+ HttpServletRequest request, HttpServletResponse response,
+ String resourceUri) throws IOException {
+
+ // get the FacesContext from the ServletContext.
+ FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder
+ .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
+ LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
+ .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
+ Lifecycle lifecycle = lifecycleFactory.getLifecycle(ComponentUtils
+ .getLifecycleId(servletContext));
+ FacesContext facesContext = facesContextFactory.getFacesContext(
+ servletContext, request, response, lifecycle);
+ facesContext.setResponseStream(new CAPTCHAResponseStream(response
+ .getOutputStream()));
+
+ // render the CAPTCHA.
+ try
+ {
+ try
+ {
+ renderCAPTCHA(facesContext);
+ }
+ catch (IOException e)
+ {
+ throw new FacesException("Could not render the CAPTCHA : "
+ + e.getMessage(), e);
+ }
+ facesContext.getResponseStream().close();
}
+ finally
+ {
+ facesContext.release();
+ }
+ }
+
+ /*
+ * This method is used for rendering the CAPTCHA component.
+ */
+ protected void renderCAPTCHA(FacesContext facesContext) throws IOException {
+
+ // getting CAPTCHA attributes.
+ HttpServletResponse response = (HttpServletResponse) facesContext
+ .getExternalContext().getResponse();
+ ResponseStream out = facesContext.getResponseStream();
+ Map requestMap = facesContext.getExternalContext()
+ .getRequestParameterMap();
+ HttpServletRequest request = (HttpServletRequest) facesContext
+ .getExternalContext().getRequest();
+ String captchaSessionKeyName = requestMap.get(
+ CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME).toString();
+
+ // construct the CAPTCHA image generator object.
+ CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
+
+ try
+ {
+ String captchaText;
- /*
- * This method is used for rendering the CAPTCHA component.
- */
- protected void renderCAPTCHA(FacesContext facesContext) throws IOException{
-
- // Initialize the CAPTCHA world.
- HttpServletResponse response = (HttpServletResponse) facesContext
- .getExternalContext().getResponse();
- ResponseStream out = facesContext.getResponseStream();
- final Map requestMap = facesContext.getExternalContext()
- .getRequestParameterMap();
- HttpServletRequest request = (HttpServletRequest) facesContext
- .getExternalContext().getRequest();
- String captchaText;
- CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
- String captchaSessionKeyName = requestMap.get(
- CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME).toString();
-
- try {
-
- // Generate random CAPTCHA text.
- captchaText = CAPTCHATextGenerator.generateRandomText();
-
- // Generate the image.
- captchaImageGenerator.generateImage(response, captchaText);
-
- // Set the generated text in the user session.
- request.getSession().setAttribute(captchaSessionKeyName, captchaText);
-
- } finally {
- out.close();
- facesContext.responseComplete();
- }
- }
+ // Generate random CAPTCHA text.
+ captchaText = CAPTCHATextGenerator.generateRandomText();
+
+ // Generate the image.
+ captchaImageGenerator.generateImage(response, captchaText);
+
+ // Set the generated text in the user session.
+ request.getSession().setAttribute(captchaSessionKeyName,
+ captchaText);
+
+ }
+ finally
+ {
+ out.close();
+ facesContext.responseComplete();
+ }
+ }
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java Wed Apr 23 22:47:20 2008
@@ -24,42 +24,64 @@
public class CAPTCHATag extends UIComponentTagBase {
- /**
- * return the component class ...
- */
- public String getComponentType() {
- return CAPTCHAComponent.COMPONENT_TYPE;
- }
-
- /**
- * Returns the component renderer ...
- */
- public String getRendererType() {
- return CAPTCHAComponent.RENDERER_TYPE;
- }
-
- /**
- * release the used resources ...
- */
- public void release() {
- super.release();
- _captchaSessionKeyName = null;
- }
-
- protected void setProperties(UIComponent component) {
- super.setProperties(component);
- setStringProperty(component, "captchaSessionKeyName",
- _captchaSessionKeyName);
- }
-
- public String getCaptchaSessionKeyName() {
- return _captchaSessionKeyName;
- }
-
- public void setCaptchaSessionKeyName(String captchaSessionKeyName) {
- this._captchaSessionKeyName = captchaSessionKeyName;
- }
-
- //Attributes ...
- private String _captchaSessionKeyName;
+ /**
+ * return the component class ...
+ */
+ public String getComponentType() {
+ return CAPTCHAComponent.COMPONENT_TYPE;
+ }
+
+ /**
+ * Returns the component renderer ...
+ */
+ public String getRendererType() {
+ return CAPTCHAComponent.RENDERER_TYPE;
+ }
+
+ /**
+ * release the used resources ...
+ */
+ public void release() {
+ super.release();
+ _captchaSessionKeyName = null;
+ _imageHeight = null;
+ _imageWidth = null;
+ }
+
+ protected void setProperties(UIComponent component) {
+ super.setProperties(component);
+ setStringProperty(component, CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME,
+ _captchaSessionKeyName);
+ setStringProperty(component, CAPTCHAComponent.ATTRIBUTE_IMAGE_WIDTH, _imageWidth);
+ setStringProperty(component, CAPTCHAComponent.ATTRIBUTE_IMAGE_HEIGHT, _imageHeight);
+ }
+
+ public String getCaptchaSessionKeyName() {
+ return _captchaSessionKeyName;
+ }
+
+ public void setCaptchaSessionKeyName(String captchaSessionKeyName) {
+ this._captchaSessionKeyName = captchaSessionKeyName;
+ }
+
+ public String getImageWidth() {
+ return _imageWidth;
+ }
+
+ public void setImageWidth(String width) {
+ this._imageWidth = width;
+ }
+
+ public String getImageHeight() {
+ return _imageHeight;
+ }
+
+ public void setImageHeight(String height) {
+ this._imageHeight = height;
+ }
+
+ // Attributes ...
+ private String _captchaSessionKeyName;
+ private String _imageWidth;
+ private String _imageHeight;
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java Wed Apr 23 22:47:20 2008
@@ -41,7 +41,7 @@
String captchaText = null;
CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
String captchaSessionKeyName = request
- .getParameter(CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME);
+ .getParameter(CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME);
// Generate random CAPTCHA text.
captchaText = CAPTCHATextGenerator.generateRandomText();
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java Wed Apr 23 22:47:20 2008
@@ -22,15 +22,9 @@
* This interface holds all the CAPTCHA related constants.
*/
public interface CAPTCHAConstants {
-
- public static final int CAPTCHA_WIDTH = 290;
-
- public static final int CAPTCHA_HEIGHT = 81;
-
+ public static final int DEFAULT_CAPTCHA_WIDTH = 290;
+ public static final int DEFAULT_CAPTCHA_HEIGHT = 81;
public static final double PI = 3.1415926535897932384626433832795;
-
public static final int TEXT_X_COORDINATE = 50;
-
public static final int TEXT_Y_COORDINATE = 60;
-
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java Wed Apr 23 22:47:20 2008
@@ -38,232 +38,239 @@
* This class is responsible for generating the CAPTCHA image.
*/
public class CAPTCHAImageGenerator {
-
- private static final Color startingColor = new Color(150, 50, 150);
- private static final Color endingColor = new Color(255, 255, 255);
-
- /*
- * A helper method to draw the captcha text on the generated image.
- */
- private void drawTextOnImage(Graphics2D graphics, String captchaText) {
-
- Font font;
- TextLayout textLayout;
- double currentFontStatus = Math.random();
-
- // Generate random font status.
- if(currentFontStatus >= 0.5) {
- font = new Font("Arial", Font.PLAIN, 60);
- } else {
- font = new Font("Arial", Font.BOLD, 60);
- }
-
- graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- textLayout = new TextLayout(captchaText, font, graphics
- .getFontRenderContext());
-
- textLayout.draw(graphics, CAPTCHAConstants.TEXT_X_COORDINATE,
- CAPTCHAConstants.TEXT_Y_COORDINATE);
- }
-
- /*
- * A helper method to apply noise on the generated image.
- */
- private void applyNoiseOnImage(Graphics2D graphics, int bufferedImageWidth,
- int bufferedImageHeight) {
-
- // Applying shear.
- applyShear(graphics, bufferedImageWidth, bufferedImageHeight);
-
- // Drawing a broken line on the image.
- drawBrokenLineOnImage(graphics);
- }
-
- /*
- * This helper method is used for applying current gradient paint
- * to the Graphics2D object.
- */
- private static void applyCurrentGradientPaint(Graphics2D graphics,
- int width, int height) {
-
- GradientPaint gradientPaint = new GradientPaint(0, 0, startingColor,
- width, height, endingColor);
-
- graphics.setPaint(gradientPaint);
- }
-
- /**
- * This method generates the CAPTCHA image.
- * @param response
- * @param captchaText
- * @throws IOException
- */
- public void generateImage(HttpServletResponse response, String captchaText)
- throws IOException {
-
- BufferedImage bufferedImage;
- Graphics2D graphics;
- PNGEncodeParam param;
- PNGImageEncoder captchaPNGImage;
-
-
- // Create the CAPTCHA Image.
- bufferedImage = new BufferedImage(CAPTCHAConstants.CAPTCHA_WIDTH,
- CAPTCHAConstants.CAPTCHA_HEIGHT,
- BufferedImage.TYPE_BYTE_INDEXED);
-
- // Setup the graphics object.
- graphics = bufferedImage.createGraphics();
-
- applyCurrentGradientPaint(graphics, bufferedImage.getWidth(),
- bufferedImage.getHeight());
-
- graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage
- .getHeight());
- graphics.setColor(Color.black);
+ private static final Color startingColor = new Color(150, 50, 150);
+ private static final Color endingColor = new Color(255, 255, 255);
- // Draw text on the CAPTCHA image.
- drawTextOnImage(graphics, captchaText);
+ /*
+ * A helper method to draw the captcha text on the generated image.
+ */
+ private void drawTextOnImage(Graphics2D graphics, String captchaText) {
+
+ Font font;
+ TextLayout textLayout;
+ double currentFontStatus = Math.random();
+
+ // Generate random font status.
+ if (currentFontStatus >= 0.5)
+ {
+ font = new Font("Arial", Font.PLAIN, 60);
+ }
+ else
+ {
+ font = new Font("Arial", Font.BOLD, 60);
+ }
+
+ graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+ textLayout = new TextLayout(captchaText, font, graphics
+ .getFontRenderContext());
+
+ textLayout.draw(graphics, CAPTCHAConstants.TEXT_X_COORDINATE,
+ CAPTCHAConstants.TEXT_Y_COORDINATE);
+ }
+
+ /*
+ * A helper method to apply noise on the generated image.
+ */
+ private void applyNoiseOnImage(Graphics2D graphics, int bufferedImageWidth,
+ int bufferedImageHeight) {
+
+ // Applying shear.
+ applyShear(graphics, bufferedImageWidth, bufferedImageHeight);
+
+ // Drawing a broken line on the image.
+ drawBrokenLineOnImage(graphics);
+ }
+
+ /*
+ * This helper method is used for applying current gradient paint to the
+ * Graphics2D object.
+ */
+ private static void applyCurrentGradientPaint(Graphics2D graphics,
+ int width, int height) {
+
+ GradientPaint gradientPaint = new GradientPaint(0, 0, startingColor,
+ width, height, endingColor);
+
+ graphics.setPaint(gradientPaint);
+ }
+
+ /**
+ * This method generates the CAPTCHA image.
+ *
+ * @param response
+ * @param captchaText
+ * @throws IOException
+ */
+ public void generateImage(HttpServletResponse response, String captchaText)
+ throws IOException {
+
+ BufferedImage bufferedImage;
+ Graphics2D graphics;
+ PNGEncodeParam param;
+ PNGImageEncoder captchaPNGImage;
- // Apply noise on the CAPTCHA image.
- applyNoiseOnImage(graphics, bufferedImage.getWidth(), bufferedImage.getHeight());
-
- // Draw the image border.
- drawBorders(graphics, bufferedImage.getWidth(), bufferedImage.getHeight());
-
- // Set the reponse content type to jpeg.
- response.setContentType("image/jpg");
-
-
- param = PNGEncodeParam
- .getDefaultEncodeParam(bufferedImage);
- captchaPNGImage = new PNGImageEncoder(response
- .getOutputStream(), param);
-
- captchaPNGImage.encode(bufferedImage);
- }
-
- /*
- * This helper method is used for drawing a thick line on the image.
- */
- private void drawThickLineOnImage(Graphics graphics, int x1, int y1,
- int x2, int y2) {
-
- int dX = x2 - x1;
- int dY = y2 - y1;
- int xPoints[] = new int[4];
- int yPoints[] = new int[4];
- int thickness = 2;
-
- double lineLength = Math.sqrt(dX * dX + dY * dY);
- double scale = (double) (thickness) / (2 * lineLength);
- double ddx = -scale * (double) dY;
- double ddy = scale * (double) dX;
-
- graphics.setColor(Color.black);
-
- ddx += (ddx > 0) ? 0.5 : -0.5;
- ddy += (ddy > 0) ? 0.5 : -0.5;
- dX = (int) ddx;
- dY = (int) ddy;
-
- xPoints[0] = x1 + dX;
- yPoints[0] = y1 + dY;
- xPoints[1] = x1 - dX;
- yPoints[1] = y1 - dY;
- xPoints[2] = x2 - dX;
- yPoints[2] = y2 - dY;
- xPoints[3] = x2 + dX;
- yPoints[3] = y2 + dY;
-
- graphics.fillPolygon(xPoints, yPoints, 4);
- }
-
- /*
- * This helper method is used for drawing a broken line on the image.
- */
- private void drawBrokenLineOnImage(Graphics2D graphics) {
-
- int yPoint1;
- int yPoint2;
- int yPoint3;
- int yPoint4;
- int yPoint5;
- Random random = new Random();
-
- // Random Y Points.
- yPoint1 = random.nextInt(CAPTCHAConstants.CAPTCHA_HEIGHT);
- yPoint2 = random.nextInt(CAPTCHAConstants.CAPTCHA_HEIGHT);
- yPoint3 = CAPTCHAConstants.CAPTCHA_HEIGHT / 2;
- yPoint4 = random.nextInt(CAPTCHAConstants.CAPTCHA_HEIGHT);
- yPoint5 = random.nextInt(CAPTCHAConstants.CAPTCHA_HEIGHT);
-
- // Draw the random broken line.
- drawThickLineOnImage(graphics, 0, yPoint1,
- CAPTCHAConstants.CAPTCHA_WIDTH / 4, yPoint2);
- drawThickLineOnImage(graphics, CAPTCHAConstants.CAPTCHA_WIDTH / 4,
- yPoint2, CAPTCHAConstants.CAPTCHA_WIDTH / 2, yPoint3);
- drawThickLineOnImage(graphics, CAPTCHAConstants.CAPTCHA_WIDTH / 2,
- yPoint3, 3 * CAPTCHAConstants.CAPTCHA_WIDTH / 4, yPoint4);
- drawThickLineOnImage(graphics, 3 * CAPTCHAConstants.CAPTCHA_WIDTH / 4,
- yPoint4, CAPTCHAConstants.CAPTCHA_WIDTH, yPoint5);
- }
-
- /*
- * This helper method is used for calculating the delta of the shearing
- * equation.
- */
- private double getDelta(int period, double i, double phase, double frames) {
- return (double) (period / 2)
- * Math.sin(i / (double) period
- + (2 * CAPTCHAConstants.PI * phase)
- / frames);
- }
-
- /*
- * This helper method is used for applying shear on the image.
- */
- private void applyShear(Graphics2D graphics, int bufferedImageWidth,
- int bufferedImageHeight) {
-
- int periodValue = 20;
- int numberOfFrames = 15;
- int phaseNumber = 7;
- double deltaX;
- double deltaY;
-
- applyCurrentGradientPaint(graphics, bufferedImageWidth,
- bufferedImageHeight);
-
- for (int i = 0; i < bufferedImageWidth; ++i) {
- deltaX = getDelta(periodValue, i, phaseNumber, numberOfFrames);
- graphics.copyArea(i, 0, 1, bufferedImageHeight, 0, (int) deltaX);
- graphics.drawLine(i, (int) deltaX, i, 0);
- graphics.drawLine(i, (int) deltaX + bufferedImageHeight, i,
- bufferedImageHeight);
- }
-
- for (int i = 0; i < bufferedImageHeight; ++i) {
- deltaY = getDelta(periodValue, i, phaseNumber, numberOfFrames);
- graphics.copyArea(0, i, bufferedImageWidth, 1, (int) deltaY, 0);
- graphics.drawLine((int) deltaY, i, 0, i);
- graphics.drawLine((int) deltaY + bufferedImageWidth, i,
- bufferedImageWidth, i);
- }
- }
-
- /*
- * This helper method is used for drawing the borders the image.
- */
- private void drawBorders(Graphics2D graphics, int width, int height) {
- graphics.setColor(Color.black);
-
- graphics.drawLine(0, 0, 0, width - 1);
- graphics.drawLine(0, 0, width - 1, 0);
- graphics.drawLine(0, height - 1, width, height - 1);
- graphics.drawLine(width - 1, height - 1, width - 1, 0);
- }
+
+
+ // Create the CAPTCHA Image.
+ bufferedImage = new BufferedImage(CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH, CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT,
+ BufferedImage.TYPE_BYTE_INDEXED);
+
+ // Setup the graphics object.
+ graphics = bufferedImage.createGraphics();
+
+ applyCurrentGradientPaint(graphics, bufferedImage.getWidth(),
+ bufferedImage.getHeight());
+
+ graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage
+ .getHeight());
+
+ graphics.setColor(Color.black);
+
+ // Draw text on the CAPTCHA image.
+ drawTextOnImage(graphics, captchaText);
+
+ // Apply noise on the CAPTCHA image.
+ applyNoiseOnImage(graphics, bufferedImage.getWidth(), bufferedImage
+ .getHeight());
+
+ // Draw the image border.
+ drawBorders(graphics, bufferedImage.getWidth(), bufferedImage
+ .getHeight());
+
+ // Set the reponse content type to jpeg.
+ response.setContentType("image/jpg");
+
+ param = PNGEncodeParam.getDefaultEncodeParam(bufferedImage);
+ captchaPNGImage = new PNGImageEncoder(response.getOutputStream(), param);
+
+ captchaPNGImage.encode(bufferedImage);
+ }
+
+ /*
+ * This helper method is used for drawing a thick line on the image.
+ */
+ private void drawThickLineOnImage(Graphics graphics, int x1, int y1,
+ int x2, int y2) {
+
+ int dX = x2 - x1;
+ int dY = y2 - y1;
+ int xPoints[] = new int[4];
+ int yPoints[] = new int[4];
+ int thickness = 2;
+
+ double lineLength = Math.sqrt(dX * dX + dY * dY);
+ double scale = (double) (thickness) / (2 * lineLength);
+ double ddx = -scale * (double) dY;
+ double ddy = scale * (double) dX;
+
+ graphics.setColor(Color.black);
+
+ ddx += (ddx > 0) ? 0.5 : -0.5;
+ ddy += (ddy > 0) ? 0.5 : -0.5;
+ dX = (int) ddx;
+ dY = (int) ddy;
+
+ xPoints[0] = x1 + dX;
+ yPoints[0] = y1 + dY;
+ xPoints[1] = x1 - dX;
+ yPoints[1] = y1 - dY;
+ xPoints[2] = x2 - dX;
+ yPoints[2] = y2 - dY;
+ xPoints[3] = x2 + dX;
+ yPoints[3] = y2 + dY;
+
+ graphics.fillPolygon(xPoints, yPoints, 4);
+ }
+
+ /*
+ * This helper method is used for drawing a broken line on the image.
+ */
+ private void drawBrokenLineOnImage(Graphics2D graphics) {
+
+ int yPoint1;
+ int yPoint2;
+ int yPoint3;
+ int yPoint4;
+ int yPoint5;
+ Random random = new Random();
+
+ // Random Y Points.
+ yPoint1 = random.nextInt(CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT);
+ yPoint2 = random.nextInt(CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT);
+ yPoint3 = CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT / 2;
+ yPoint4 = random.nextInt(CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT);
+ yPoint5 = random.nextInt(CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT);
+
+ // Draw the random broken line.
+ drawThickLineOnImage(graphics, 0, yPoint1,
+ CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 4, yPoint2);
+ drawThickLineOnImage(graphics,
+ CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 4, yPoint2,
+ CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 2, yPoint3);
+ drawThickLineOnImage(graphics,
+ CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 2, yPoint3,
+ 3 * CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 4, yPoint4);
+ drawThickLineOnImage(graphics,
+ 3 * CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH / 4, yPoint4,
+ CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH, yPoint5);
+ }
+
+ /*
+ * This helper method is used for calculating the delta of the shearing
+ * equation.
+ */
+ private double getDelta(int period, double i, double phase, double frames) {
+ return (double) (period / 2)
+ * Math.sin(i / (double) period
+ + (2 * CAPTCHAConstants.PI * phase) / frames);
+ }
+
+ /*
+ * This helper method is used for applying shear on the image.
+ */
+ private void applyShear(Graphics2D graphics, int bufferedImageWidth,
+ int bufferedImageHeight) {
+
+ int periodValue = 20;
+ int numberOfFrames = 15;
+ int phaseNumber = 7;
+ double deltaX;
+ double deltaY;
+
+ applyCurrentGradientPaint(graphics, bufferedImageWidth,
+ bufferedImageHeight);
+
+ for (int i = 0; i < bufferedImageWidth; ++i)
+ {
+ deltaX = getDelta(periodValue, i, phaseNumber, numberOfFrames);
+ graphics.copyArea(i, 0, 1, bufferedImageHeight, 0, (int) deltaX);
+ graphics.drawLine(i, (int) deltaX, i, 0);
+ graphics.drawLine(i, (int) deltaX + bufferedImageHeight, i,
+ bufferedImageHeight);
+ }
+
+ for (int i = 0; i < bufferedImageHeight; ++i)
+ {
+ deltaY = getDelta(periodValue, i, phaseNumber, numberOfFrames);
+ graphics.copyArea(0, i, bufferedImageWidth, 1, (int) deltaY, 0);
+ graphics.drawLine((int) deltaY, i, 0, i);
+ graphics.drawLine((int) deltaY + bufferedImageWidth, i,
+ bufferedImageWidth, i);
+ }
+ }
+
+ /*
+ * This helper method is used for drawing the borders the image.
+ */
+ private void drawBorders(Graphics2D graphics, int width, int height) {
+ graphics.setColor(Color.black);
+
+ graphics.drawLine(0, 0, 0, width - 1);
+ graphics.drawLine(0, 0, width - 1, 0);
+ graphics.drawLine(0, height - 1, width, height - 1);
+ graphics.drawLine(width - 1, height - 1, width - 1, 0);
+ }
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld?rev=651161&r1=651160&r2=651161&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld Wed Apr 23 22:47:20 2008
@@ -1866,16 +1866,31 @@
<tag-class>org.apache.myfaces.custom.captcha.CAPTCHATag</tag-class>
<body-content>JSP</body-content>
&ui_component_attributes;
- &user_role_attributes;
<attribute>
<name>captchaSessionKeyName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
<description>
- Determines the CAPTCHA session key name.
+ Determines the CAPTCHA session key name.
</description>
</attribute>
+ <attribute>
+ <name>imageWidth</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <description>
+ Integer to indicate the CAPTCHA width. default is 290.
+ </description>
+ </attribute>
+ <attribute>
+ <name>imageHeight</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <description>
+ Integer to indicate the CAPTCHA height. default is 81.
+ </description>
+ </attribute>
</tag>