myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r633121 - in /myfaces/tomahawk/trunk: ./ sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/ sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/ sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/ ...
Date Mon, 03 Mar 2008 15:16:32 GMT
Author: lu4242
Date: Mon Mar  3 07:16:26 2008
New Revision: 633121

URL: http://svn.apache.org/viewvc?rev=633121&view=rev
Log:
TOMAHAWK-1207 New cool component CAPTCHA! 

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp   (with props)
Modified:
    myfaces/tomahawk/trunk/pom.xml
    myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
    myfaces/tomahawk/trunk/sandbox/examples/pom.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp

Modified: myfaces/tomahawk/trunk/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/pom.xml?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/pom.xml (original)
+++ myfaces/tomahawk/trunk/pom.xml Mon Mar  3 07:16:26 2008
@@ -104,6 +104,11 @@
           <version>${myfaces.version}</version>
           <scope>${jsf-impl.scope}</scope>
         </dependency>
+	<dependency>
+		<groupId>batik</groupId>
+		<artifactId>batik-awt-util</artifactId>
+		<version>1.6-1</version>
+	</dependency>        
       </dependencies>
 
       <properties>
@@ -175,6 +180,13 @@
           <version>1.1.0</version>
           <scope>${jsf-impl.scope}</scope>
         </dependency>
+        
+	<dependency>
+		<groupId>batik</groupId>
+		<artifactId>batik-awt-util</artifactId>
+		<version>1.6-1</version>
+	</dependency>        
+        
       </dependencies>
 
       <repositories>
@@ -182,6 +194,13 @@
           <id>java.net</id>
           <name>java.net Maven 1 Repository</name>
           <url>https://maven-repository.dev.java.net/nonav/repository</url>
+          <layout>legacy</layout>
+        </repository>
+
+        <repository>
+          <id>Maven2 Repo1</id>
+          <name>Maven2 Repo1</name>
+          <url>http://repo1.maven.org/maven2</url>
           <layout>legacy</layout>
         </repository>
       </repositories>

Added: 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=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
+
+
+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 String getCaptchaSessionKeyName() {
+		if (_captchaSessionKeyName != null)
+			return _captchaSessionKeyName;
+
+		ValueBinding vb = getValueBinding("captchaSessionKeyName");
+		return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb)
+				: null;
+	}
+
+	public Object saveState(FacesContext context) {
+		Object values[] = new Object[2];
+		values[0] = super.saveState(context);
+		values[1] = _captchaSessionKeyName;	
+		return ((Object) (values));
+	}
+
+	public void restoreState(FacesContext context, Object state) {
+		Object values[] = (Object[]) state;
+		super.restoreState(context, values[0]);
+		_captchaSessionKeyName = (String) values[1];
+	}	
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHAComponent.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 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=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.Renderer;
+
+public class CAPTCHARenderer extends Renderer {
+	
+	private static final String CAPTCHA_SERVLET_NAME = "apache_captcha_servlet_url";
+
+	public void encodeBegin(FacesContext context, UIComponent component)
+			throws IOException {
+
+		CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
+		
+		renderCAPTCHA(context, captchaComponent);
+	}
+	
+	public void encodeEnd(FacesContext context, UIComponent component)
+			throws IOException {
+		super.encodeEnd(context, component);
+	}
+
+	/*
+	 * This helper method renders the img tag that will
+	 * call the CAPTCHAServlet to render the CAPTCHA image. 
+	 */
+	private void renderCAPTCHA(FacesContext context, CAPTCHAComponent component)
+			throws IOException {
+		ResponseWriter writer = context.getResponseWriter();
+
+		writer.startElement("img", component);
+		writer.writeAttribute("src", CAPTCHA_SERVLET_NAME + "?"
+				+ appendParameters(component), "src");
+		writer.endElement("img");
+	}
+
+	/*
+	 * This helper method is used for appending the parameters to the
+	 * CAPTCHA servlet.
+	 */
+	private String appendParameters(CAPTCHAComponent component) {
+		return CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME + "="
+				+ component.getCaptchaSessionKeyName();
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHARenderer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 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=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha;
+
+import javax.faces.component.UIComponent;
+
+import org.apache.myfaces.shared_tomahawk.taglib.UIComponentTagBase;
+
+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;
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/CAPTCHATag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 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=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.myfaces.custom.captcha.CAPTCHAComponent;
+import org.apache.myfaces.custom.captcha.util.*;
+
+
+/**
+ * The (CaptchaServlet) is responsible for generating the CAPTCHA image.
+ */
+public class CaptchaServlet extends HttpServlet {
+
+	public void doGet(HttpServletRequest request, HttpServletResponse response)
+			throws ServletException, IOException {
+
+		// Initialize world.
+		String captchaText = null;
+		CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
+		String captchaSessionKeyName = request
+				.getParameter(CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME);
+
+		// 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);
+	}
+
+	protected void doPost(HttpServletRequest request,
+			HttpServletResponse response) throws ServletException, IOException {
+		doGet(request, response);
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/servlet/CaptchaServlet.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 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=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha.util;
+import org.apache.batik.ext.awt.image.codec.*;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.font.TextLayout;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+
+import javax.servlet.http.HttpServletResponse;
+
+
+/**
+ * This class is responsible for generating the 
+ * CAPTCHA image.
+ */
+public class CAPTCHAImageGenerator {
+
+	private static final int CAPTCHA_WIDTH = 290;
+	private static final int CAPTCHA_HEIGHT = 81;	
+	private static final double PI = 3.1415926535897932384626433832795;
+	private static final int TEXT_X_COORDINATE = 50;	
+	private static final int TEXT_Y_COORDINATE = 60;
+	
+	/*
+	 * A helper method to draw the captcha text on the generated image.
+	 */
+	private void drawTextOnImage(Graphics2D graphics, String captchaText) {
+		
+		
+		
+		Font font = null;
+		TextLayout textLayout = null;
+		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, TEXT_X_COORDINATE, 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 method generates the CAPTCHA image.
+	 * @param response 
+	 * @param captchaText
+	 * @throws IOException
+	 */
+	public void generateImage(HttpServletResponse response, String captchaText)
+			throws IOException {
+		
+		int bufferedImageWidth = 0;
+		int bufferedImageHeight = 0;	
+		BufferedImage bufferedImage = null;
+		Graphics2D graphics = null;
+		PNGEncodeParam param = null;
+		PNGImageEncoder captchaPNGImage = null;
+		
+		
+		// Create the CAPTCHA Image.
+		bufferedImage = new BufferedImage(CAPTCHA_WIDTH,
+				CAPTCHA_HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
+
+		graphics = bufferedImage.createGraphics();
+
+		graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage
+				.getHeight());
+
+		graphics.setColor(Color.black);
+
+		bufferedImageWidth = bufferedImage.getWidth();
+		bufferedImageHeight = bufferedImage.getHeight();
+
+		// Draw text on the CAPTCHA image.
+		drawTextOnImage(graphics, captchaText);
+
+		// Apply noise on the CAPTCHA image.
+		applyNoiseOnImage(graphics, bufferedImageWidth, bufferedImageHeight);
+		
+		// Draw the image border.
+		drawBorders(graphics, bufferedImageWidth, bufferedImageHeight);
+		
+		// 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(Graphics graphics) {
+
+		int yPoint1 = 0;
+		int yPoint2 = 0;
+		int yPoint3 = 0;		
+		int yPoint4 = 0;
+		int yPoint5 = 0;
+		Random random = new Random();
+		
+		// Random Y Points.
+		yPoint1 = random.nextInt(CAPTCHA_HEIGHT);
+		yPoint2 = random.nextInt(CAPTCHA_HEIGHT);
+		yPoint3 = CAPTCHA_HEIGHT / 2;		
+		yPoint4 = random.nextInt(CAPTCHA_HEIGHT);
+		yPoint5 = random.nextInt(CAPTCHA_HEIGHT);		
+
+		// Draw the random broken line.
+		drawThickLineOnImage(graphics, 0, yPoint1, CAPTCHA_WIDTH / 4, yPoint2);
+		drawThickLineOnImage(graphics, CAPTCHA_WIDTH / 4, yPoint2,
+				CAPTCHA_WIDTH / 2, yPoint3);
+		drawThickLineOnImage(graphics, CAPTCHA_WIDTH / 2, yPoint3,
+				3 * CAPTCHA_WIDTH / 4, yPoint4);
+		drawThickLineOnImage(graphics, 3 * CAPTCHA_WIDTH / 4, yPoint4,
+				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((double) i / (double) period
+						+ (2 * PI * (double) phase) / (double) frames);
+	}	
+
+	/*
+	 * This helper method is used for applying shear on the image.
+	 */
+	private void applyShear(Graphics graphics, int bufferedImageWidth,
+			int bufferedImageHeight) {
+
+		int periodValue = 20;
+		int numberOfFrames = 15;
+		int phaseNumber = 7;
+		double deltaX = 0;
+		double deltaY = 0;
+
+		graphics.setColor(Color.WHITE);
+
+		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);
+	}		
+	
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java?rev=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.captcha.util;
+
+import java.util.Random;
+
+/**
+ * This class is responsible for generating the 
+ * CAPTCHA text.
+ */
+public class CAPTCHATextGenerator {
+	
+	/* CAPTCHA possible characters */
+	private final static char[] CAPTCHA_POSSIBLE_CHARS = new char[] { 'a', 'b',
+			'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+			'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
+			'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
+			'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
+			'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
+	
+	/**
+	 * generateRandomText() generates the CAPTCHA text
+	 * @return the random generated text. 
+	 */
+	public static String generateRandomText() {
+		
+		int totalPossibleCharacters = CAPTCHA_POSSIBLE_CHARS.length - 1;	
+		String captchaText = "";
+		Random random = new Random();
+		int captchaTextLength = 5;
+		int randomNumber = random.nextInt(10);
+		
+		// Determine the CAPTCHA Length whether it is five or six.
+		if(randomNumber >= 5) {
+			captchaTextLength = 6;
+		}
+		
+		// Generate the random String.
+		for (int i = 0; i < captchaTextLength; ++i) {
+			captchaText += CAPTCHA_POSSIBLE_CHARS[random
+					.nextInt(totalPossibleCharacters) + 1];
+		}
+		
+		return captchaText;
+	}
+	
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHATextGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
(original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
Mon Mar  3 07:16:26 2008
@@ -298,6 +298,11 @@
 	 <component-type>org.apache.myfaces.PasswordStrength</component-type>
 	 <component-class>org.apache.myfaces.custom.passwordStrength.PasswordStrengthComponent</component-class>
   </component> 
+  
+  <component>
+	 <component-type>org.apache.myfaces.CAPTCHA</component-type>
+	 <component-class>org.apache.myfaces.custom.captcha.CAPTCHAComponent</component-class>
+  </component>   
 
   <!-- sandbox converters -->
 
@@ -636,6 +641,13 @@
   		<renderer-type>org.apache.myfaces.PasswordStrength</renderer-type>
   		<renderer-class>org.apache.myfaces.custom.passwordStrength.PasswordStrengthRenderer</renderer-class>
   	</renderer>
+  	
+  	<renderer>
+  		<component-family>org.apache.myfaces.CAPTCHA</component-family>
+  		<renderer-type>org.apache.myfaces.CAPTCHA</renderer-type>
+  		<renderer-class>org.apache.myfaces.custom.captcha.CAPTCHARenderer</renderer-class>
+  	</renderer>  	
+  	
   </render-kit>
 
   <lifecycle>

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=633121&r1=633120&r2=633121&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 Mon Mar  3 07:16:26
2008
@@ -1825,4 +1825,25 @@
       </attribute>
 
   </tag>
+
+
+
+    <tag>
+        <name>captcha</name>
+        <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.
+	      </description>
+      </attribute>
+    </tag>
+
+
 </taglib>

Modified: myfaces/tomahawk/trunk/sandbox/examples/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/pom.xml?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/pom.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/pom.xml Mon Mar  3 07:16:26 2008
@@ -292,6 +292,12 @@
             <version>2.1</version>
             <scope>runtime</scope>
         </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.3</version>
+            <scope>provided</scope>
+        </dependency>        
     </dependencies>
 
     <build>

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java?rev=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
Mon Mar  3 07:16:26 2008
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.examples.captcha;
+
+import javax.faces.context.FacesContext;
+import javax.servlet.http.HttpSession;
+
+public class CaptchaBean {
+	
+	public final static String SESSION_KEY_NAME = "mySessionKeyName";
+	public final static String CORRECT = "Correct!";
+	public final static String WRONG = "Wrong";	
+	
+	String status;
+	String value;
+
+	public String check() {
+
+		// Compare the CAPTCHA value with the user entered value.
+		String captchaValue = (String)((HttpSession) FacesContext
+				.getCurrentInstance().getExternalContext().getSession(true))
+				.getAttribute(SESSION_KEY_NAME);
+		
+		if(captchaValue.equalsIgnoreCase(value)) {
+			status = CORRECT;
+		} else {
+			status = WRONG;
+		}
+		
+		return "";
+	}
+	
+	public String getStatus() {
+		return status;
+	}
+
+	public void setStatus(String status) {
+		this.status = status;
+	}
+	
+	public String getValue() {
+		return value;
+	}
+
+	public void setValue(String value) {
+		this.value = value;
+	}
+
+	public String getSessionKeyName() {
+		return SESSION_KEY_NAME;
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/captcha/CaptchaBean.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml Mon
Mar  3 07:16:26 2008
@@ -556,6 +556,13 @@
 		  <managed-bean-class>org.apache.myfaces.examples.passwordStrength.PasswordStrengthBean</managed-bean-class>
 		  <managed-bean-scope>request</managed-bean-scope>
 	 </managed-bean> 
+
+    <!-- managed bean for captcha -->	 
+	 <managed-bean>
+ 		<managed-bean-name>captchaBean</managed-bean-name>
+ 		<managed-bean-class>org.apache.myfaces.examples.captcha.CaptchaBean</managed-bean-class>
+ 		<managed-bean-scope>request</managed-bean-scope>
+ 	</managed-bean>
   
 	<!-- navigation rules -->
     <navigation-rule>

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml Mon Mar  3 07:16:26
2008
@@ -176,6 +176,12 @@
     <filter-name>conversationFilter</filter-name>
     <url-pattern>*.jsf</url-pattern>
   </filter-mapping>
+  
+  <!-- Captcha Servlet -->
+  <servlet>
+	<servlet-name>captcha</servlet-name>
+	<servlet-class>org.apache.myfaces.custom.captcha.servlet.CaptchaServlet</servlet-class>
+  </servlet>
 
   <servlet>
     <servlet-name>Faces Servlet</servlet-name>
@@ -196,6 +202,10 @@
     <servlet-name>SourceCodeServlet</servlet-name>
     <url-pattern>*.source</url-pattern>
   </servlet-mapping>
+  <servlet-mapping>
+	<servlet-name>captcha</servlet-name>
+	<url-pattern>/apache_captcha_servlet_url</url-pattern>
+  </servlet-mapping>    
   <welcome-file-list>
   	<welcome-file>index.html</welcome-file>
   	<welcome-file>index.htm</welcome-file>

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp?rev=633121&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp Mon Mar  3 07:16:26
2008
@@ -0,0 +1,55 @@
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+<html>
+
+<!--
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+//-->
+
+<%@include file="inc/head.inc"%>
+
+<body>
+
+<f:view>
+	   
+	<h:form id="form1">
+		<h:panelGrid columns="2">
+			<h:outputText value="Type the code shown "/>
+			<h:inputText value="#{captchaBean.value}"/>
+			
+			<h:commandLink value="Try a different image" />		
+			<s:captcha captchaSessionKeyName="#{captchaBean.sessionKeyName}" />
+			
+			<h:commandButton value="Submit" action="#{captchaBean.check}"/>
+			<h:outputText value="#{captchaBean.status}"/>
+		</h:panelGrid>
+	</h:form>		
+	    
+
+	<%@include file="inc/page_footer.jsp"%>
+
+</f:view>
+
+</body>
+
+</html>
+

Propchange: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/captcha.jsp
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp?rev=633121&r1=633120&r2=633121&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp Mon Mar  3 07:16:26 2008
@@ -189,6 +189,13 @@
 	            </h:outputLink>
          </h:panelGrid>
         
+         <h:outputText value="CAPTCHA"/>
+         <h:panelGrid style="padding-left:25px">
+	            <h:outputLink value="captcha.jsf" >
+	                <f:verbatim>CAPTCHA - component to generate random text images for
security purposes</f:verbatim>
+	            </h:outputLink>
+         </h:panelGrid>
+        
         <f:verbatim><br/><br/><br/><br/><br/><br/><br/></f:verbatim>
     </h:form>
     </f:view>



Mime
View raw message