You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gr...@apache.org on 2008/03/25 16:40:55 UTC
svn commit: r640864 - in /myfaces/tomahawk/trunk/sandbox:
core/src/main/java/org/apache/myfaces/custom/captcha/
core/src/main/java/org/apache/myfaces/custom/captcha/util/
core/src/main/java/org/apache/myfaces/custom/util/
examples/src/main/webapp/WEB-INF/
Author: grantsmith
Date: Tue Mar 25 08:40:50 2008
New Revision: 640864
URL: http://svn.apache.org/viewvc?rev=640864&view=rev
Log:
https://issues.apache.org/jira/browse/TOMAHAWK-1216 patch applied
Added:
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/CAPTCHAResponseStream.java
Modified:
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/util/CAPTCHAImageGenerator.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java
myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml
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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
@@ -19,22 +19,40 @@
package org.apache.myfaces.custom.captcha;
import java.io.IOException;
+import java.util.Map;
+import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.context.ResponseStream;
import javax.faces.context.ResponseWriter;
+import javax.faces.lifecycle.Lifecycle;
+import javax.faces.lifecycle.LifecycleFactory;
import javax.faces.render.Renderer;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.myfaces.component.html.util.ParameterResourceHandler;
+import org.apache.myfaces.custom.captcha.util.CAPTCHAImageGenerator;
+import org.apache.myfaces.custom.captcha.util.CAPTCHAResponseStream;
+import org.apache.myfaces.custom.captcha.util.CAPTCHATextGenerator;
+import org.apache.myfaces.custom.util.ComponentUtils;
+import org.apache.myfaces.renderkit.html.util.AddResource;
+import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
+import org.apache.myfaces.renderkit.html.util.ResourceLoader;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
-public class CAPTCHARenderer extends Renderer {
-
- private static final String CAPTCHA_SERVLET_NAME = "apache_captcha_servlet_url";
+public class CAPTCHARenderer extends Renderer implements ResourceLoader {
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
- renderCAPTCHA(context, captchaComponent);
+ generateImageTag(context, captchaComponent);
}
public void encodeEnd(FacesContext context, UIComponent component)
@@ -43,25 +61,104 @@
}
/*
- * This helper method renders the img tag that will
- * call the CAPTCHAServlet to render the CAPTCHA image.
+ * 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 renderCAPTCHA(FacesContext context, CAPTCHAComponent component)
+ private void generateImageTag(FacesContext context, CAPTCHAComponent component)
throws IOException {
+
+ AddResource addResource = null;
+ String url = null;
+ CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
ResponseWriter writer = context.getResponseWriter();
+ Map params = ComponentUtils.getParameterMap(component);
+ String captchaSessionKeyName = captchaComponent.getCaptchaSessionKeyName();
+
+ writer.startElement(HTML.IMG_ELEM, captchaComponent);
+
+ 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);
- writer.startElement("img", component);
- writer.writeAttribute("src", CAPTCHA_SERVLET_NAME + "?"
- + appendParameters(component), "src");
- writer.endElement("img");
+ 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();
+ }
+ }
+
/*
- * This helper method is used for appending the parameters to the
- * CAPTCHA servlet.
+ * This method is used for rendering the CAPTCHA component.
*/
- private String appendParameters(CAPTCHAComponent component) {
- return CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME + "="
- + component.getCaptchaSessionKeyName();
- }
+ 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 = null;
+ 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();
+ }
+ }
}
Added: 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=640864&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java Tue Mar 25 08:40:50 2008
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ * 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 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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
@@ -35,16 +35,10 @@
/**
- * This class is responsible for generating the
- * CAPTCHA image.
+ * 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;
+
private static final Color startingColor = new Color(150, 50, 150);
private static final Color endingColor = new Color(255, 255, 255);
@@ -53,8 +47,6 @@
*/
private void drawTextOnImage(Graphics2D graphics, String captchaText) {
-
-
Font font = null;
TextLayout textLayout = null;
double currentFontStatus = Math.random();
@@ -71,7 +63,8 @@
textLayout = new TextLayout(captchaText, font, graphics
.getFontRenderContext());
- textLayout.draw(graphics, TEXT_X_COORDINATE, TEXT_Y_COORDINATE);
+ textLayout.draw(graphics, CAPTCHAConstants.TEXT_X_COORDINATE,
+ CAPTCHAConstants.TEXT_Y_COORDINATE);
}
/*
@@ -116,8 +109,9 @@
// Create the CAPTCHA Image.
- bufferedImage = new BufferedImage(CAPTCHA_WIDTH,
- CAPTCHA_HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
+ bufferedImage = new BufferedImage(CAPTCHAConstants.CAPTCHA_WIDTH,
+ CAPTCHAConstants.CAPTCHA_HEIGHT,
+ BufferedImage.TYPE_BYTE_INDEXED);
// Setup the graphics object.
graphics = bufferedImage.createGraphics();
@@ -200,20 +194,21 @@
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);
+ 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, 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);
+ 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);
}
/*
@@ -223,7 +218,8 @@
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);
+ + (2 * CAPTCHAConstants.PI * (double) phase)
+ / (double) frames);
}
/*
Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java?rev=640864&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java Tue Mar 25 08:40:50 2008
@@ -0,0 +1,56 @@
+/*
+ * 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.io.IOException;
+import java.io.OutputStream;
+import javax.faces.context.ResponseStream;
+
+/**
+ * This class is responsible for wrapping the CAPTCHA Image
+ * response stream.
+ */
+public final class CAPTCHAResponseStream extends ResponseStream {
+ private final OutputStream _out;
+
+ public CAPTCHAResponseStream(OutputStream out) {
+ _out = out;
+ }
+
+ public void close() throws IOException {
+ _out.flush();
+ _out.close();
+ }
+
+ public void flush() throws IOException {
+ _out.flush();
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ _out.write(b, off, len);
+ }
+
+ public void write(byte[] b) throws IOException {
+ _out.write(b);
+ }
+
+ public void write(int b) throws IOException {
+ _out.write(b);
+ }
+}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java?rev=640864&r1=640863&r2=640864&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java Tue Mar 25 08:40:50 2008
@@ -20,9 +20,16 @@
package org.apache.myfaces.custom.util;
import javax.faces.component.UIComponent;
+import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlMessages;
import javax.faces.context.FacesContext;
+import javax.faces.lifecycle.LifecycleFactory;
+import javax.faces.webapp.FacesServlet;
+import javax.servlet.ServletContext;
+
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
/**
* User: treeder
@@ -32,7 +39,7 @@
public final class ComponentUtils
{
- private ComponentUtils(){
+ private ComponentUtils() {
}
@@ -44,111 +51,140 @@
* @param clientId
* @return component referenced by clientId or null if not found
*/
- public static UIComponent findComponentByClientId(FacesContext context, UIComponent root, String clientId)
- {
- UIComponent component = null;
- for (int i = 0; i < root.getChildCount() && component == null; i++)
- {
- UIComponent child = (UIComponent) root.getChildren().get(i);
- component = findComponentByClientId(context, child, clientId);
- }
- if (root.getId() != null)
- {
- if (component == null && root.getClientId(context).equals(clientId))
- {
- component = root;
- }
- }
- return component;
- }
+ public static UIComponent findComponentByClientId(FacesContext context,
+ UIComponent root, String clientId) {
+ UIComponent component = null;
+ for (int i = 0; i < root.getChildCount() && component == null; i++) {
+ UIComponent child = (UIComponent) root.getChildren().get(i);
+ component = findComponentByClientId(context, child, clientId);
+ }
+ if (root.getId() != null) {
+ if (component == null && root.getClientId(context).equals(clientId)) {
+ component = root;
+ }
+ }
+ return component;
+ }
/**
- * Useful if you don't know the clientId
- * <p/>
- * TR- This was moved from AjaxPhaseListenere on checkin 344383
- * Seems like this could be made more efficient
- *
- * @param context
- * @param root
- * @param id
- * @return component referenced by id or null if not found
- */
- public static UIComponent findComponentById(FacesContext context, UIComponent root, String id)
- {
- UIComponent component = null;
- for (int i = 0; i < root.getChildCount() && component == null; i++)
- {
- UIComponent child = (UIComponent) root.getChildren().get(i);
- component = findComponentById(context, child, id);
- }
- //System.out.println("component looking for: " + id + " - rootid: " + root.getId() + " " + root);
- if (root.getId() != null)
- {
- if (component == null && root.getId().equals(id))
- {
- component = root;
- }
- }
- return component;
- }
-
- public static UIComponent findFirstMessagesComponent(FacesContext context, UIComponent base)
- {
- if (base == null)
- {
- return null;
- }
-
- if (base instanceof HtmlMessages)
- {
- return base;
- }
-
- Iterator iterChildren = base.getFacetsAndChildren();
- while (iterChildren.hasNext())
- {
- UIComponent child = (UIComponent) iterChildren.next();
-
- UIComponent found = findFirstMessagesComponent(context, child);
- if (found != null)
- {
- return found;
- }
- }
+ * Useful if you don't know the clientId <p/> TR- This was moved from
+ * AjaxPhaseListenere on checkin 344383 Seems like this could be made more
+ * efficient
+ *
+ * @param context
+ * @param root
+ * @param id
+ * @return component referenced by id or null if not found
+ */
+ public static UIComponent findComponentById(FacesContext context,
+ UIComponent root, String id) {
+ UIComponent component = null;
+ for (int i = 0; i < root.getChildCount() && component == null; i++) {
+ UIComponent child = (UIComponent) root.getChildren().get(i);
+ component = findComponentById(context, child, id);
+ }
+ // System.out.println("component looking for: " + id + " - rootid: " +
+ // root.getId() + " " + root);
+ if (root.getId() != null) {
+ if (component == null && root.getId().equals(id)) {
+ component = root;
+ }
+ }
+ return component;
+ }
- return null;
- }
-
-
- private static boolean isDecorated(UIComponent component, String attribute, String value) {
- String attributeValue = (String) component.getAttributes().get(attribute);
-
- if(attributeValue == null || attributeValue.indexOf(value) == -1)
+ public static UIComponent findFirstMessagesComponent(FacesContext context,
+ UIComponent base) {
+ if (base == null) {
+ return null;
+ }
+
+ if (base instanceof HtmlMessages) {
+ return base;
+ }
+
+ Iterator iterChildren = base.getFacetsAndChildren();
+ while (iterChildren.hasNext()) {
+ UIComponent child = (UIComponent) iterChildren.next();
+
+ UIComponent found = findFirstMessagesComponent(context, child);
+ if (found != null) {
+ return found;
+ }
+ }
+
+ return null;
+ }
+
+ private static boolean isDecorated(UIComponent component, String attribute,
+ String value) {
+ String attributeValue = (String) component.getAttributes().get(
+ attribute);
+
+ if (attributeValue == null || attributeValue.indexOf(value) == -1)
return false;
else
return true;
}
/**
- * Changes the event attributes like onclick by appending the given value
- *
- * @param component UIComponent instance that the attribute belongs to
- * @param attribute Attribute to be changed
- * @param value Value to be appended
- */
- public static void decorateEventAttribute(UIComponent component, String attribute, String value) {
- if(isDecorated(component, attribute, value))
+ * Changes the event attributes like onclick by appending the given value
+ *
+ * @param component
+ * UIComponent instance that the attribute belongs to
+ * @param attribute
+ * Attribute to be changed
+ * @param value
+ * Value to be appended
+ */
+ public static void decorateEventAttribute(UIComponent component,
+ String attribute, String value) {
+ if (isDecorated(component, attribute, value))
return;
-
- String attributeValue = (String) component.getAttributes().get(attribute);
-
- if(attributeValue == null)
+
+ String attributeValue = (String) component.getAttributes().get(
+ attribute);
+
+ if (attributeValue == null)
component.getAttributes().put(attribute, value);
+ else if (attributeValue.endsWith(";"))
+ component.getAttributes().put(attribute, attributeValue + value);
else
- if( attributeValue.endsWith(";"))
- component.getAttributes().put(attribute, attributeValue + value);
- else
- component.getAttributes().put(attribute, attributeValue + ";" + value);
+ component.getAttributes().put(attribute,
+ attributeValue + ";" + value);
}
-
+
+ /**
+ * The getParameterMap() is used for getting the parameters
+ * of a specific component.
+ * @param component
+ * @return the Map of the component.
+ */
+ public static Map getParameterMap(UIComponent component) {
+ Map result = new HashMap();
+ for (Iterator iter = component.getChildren().iterator(); iter.hasNext();) {
+ UIComponent child = (UIComponent) iter.next();
+ if (child instanceof UIParameter) {
+ UIParameter uiparam = (UIParameter) child;
+ Object value = uiparam.getValue();
+ if (value != null) {
+ result.put(uiparam.getName(), value);
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * The getLifecycleId() is used for getting the id of
+ * the Lifecycle from the ServletContext.
+ * @param context
+ * @return the id of the life cycle.
+ */
+ public static String getLifecycleId(ServletContext context) {
+ String lifecycleId = context
+ .getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
+ return lifecycleId != null ? lifecycleId
+ : LifecycleFactory.DEFAULT_LIFECYCLE;
+ }
}
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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
@@ -177,12 +177,6 @@
<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>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
@@ -201,11 +195,7 @@
<servlet-mapping>
<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>
+ </servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
Re: svn commit: r640864 - in /myfaces/tomahawk/trunk/sandbox: core/src/main/java/org/apache/myfaces/custom/captcha/
core/src/main/java/org/apache/myfaces/custom/captcha/util/ core/src/main/java/org/apache/myfaces/custom/util/
examples/src/main/webapp/WEB-INF/
Posted by "simon.kitching@chello.at" <si...@chello.at>.
Hi Grant,
Could you please avoid combining code changes and reformatting in the
same patch? It is just impossible from this patch to determine what
*real* changes you have made to ComponentUtils.
And I believe the coding convention here is to use spaces, *not* tabs,
but you have replaced all spaces *with* tabs in ComponentUtils.
What were the actual ComponentUtils changes?
Thanks,
Simon
grantsmith@apache.org schrieb:
> Author: grantsmith
> Date: Tue Mar 25 08:40:50 2008
> New Revision: 640864
>
> URL: http://svn.apache.org/viewvc?rev=640864&view=rev
> Log:
> https://issues.apache.org/jira/browse/TOMAHAWK-1216 patch applied
>
> Added:
> 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/CAPTCHAResponseStream.java
> Modified:
> 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/util/CAPTCHAImageGenerator.java
> myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java
> myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/web.xml
>
> 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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
> @@ -19,22 +19,40 @@
> package org.apache.myfaces.custom.captcha;
>
> import java.io.IOException;
> +import java.util.Map;
>
> +import javax.faces.FacesException;
> +import javax.faces.FactoryFinder;
> import javax.faces.component.UIComponent;
> import javax.faces.context.FacesContext;
> +import javax.faces.context.FacesContextFactory;
> +import javax.faces.context.ResponseStream;
> import javax.faces.context.ResponseWriter;
> +import javax.faces.lifecycle.Lifecycle;
> +import javax.faces.lifecycle.LifecycleFactory;
> import javax.faces.render.Renderer;
> +import javax.servlet.ServletContext;
> +import javax.servlet.http.HttpServletRequest;
> +import javax.servlet.http.HttpServletResponse;
> +
> +import org.apache.myfaces.component.html.util.ParameterResourceHandler;
> +import org.apache.myfaces.custom.captcha.util.CAPTCHAImageGenerator;
> +import org.apache.myfaces.custom.captcha.util.CAPTCHAResponseStream;
> +import org.apache.myfaces.custom.captcha.util.CAPTCHATextGenerator;
> +import org.apache.myfaces.custom.util.ComponentUtils;
> +import org.apache.myfaces.renderkit.html.util.AddResource;
> +import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
> +import org.apache.myfaces.renderkit.html.util.ResourceLoader;
> +import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
>
> -public class CAPTCHARenderer extends Renderer {
> -
> - private static final String CAPTCHA_SERVLET_NAME = "apache_captcha_servlet_url";
> +public class CAPTCHARenderer extends Renderer implements ResourceLoader {
>
> public void encodeBegin(FacesContext context, UIComponent component)
> throws IOException {
>
> CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
>
> - renderCAPTCHA(context, captchaComponent);
> + generateImageTag(context, captchaComponent);
> }
>
> public void encodeEnd(FacesContext context, UIComponent component)
> @@ -43,25 +61,104 @@
> }
>
> /*
> - * This helper method renders the img tag that will
> - * call the CAPTCHAServlet to render the CAPTCHA image.
> + * 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 renderCAPTCHA(FacesContext context, CAPTCHAComponent component)
> + private void generateImageTag(FacesContext context, CAPTCHAComponent component)
> throws IOException {
> +
> + AddResource addResource = null;
> + String url = null;
> + CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
> ResponseWriter writer = context.getResponseWriter();
> + Map params = ComponentUtils.getParameterMap(component);
> + String captchaSessionKeyName = captchaComponent.getCaptchaSessionKeyName();
> +
> + writer.startElement(HTML.IMG_ELEM, captchaComponent);
> +
> + 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);
>
> - writer.startElement("img", component);
> - writer.writeAttribute("src", CAPTCHA_SERVLET_NAME + "?"
> - + appendParameters(component), "src");
> - writer.endElement("img");
> + 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();
> + }
> + }
> +
> /*
> - * This helper method is used for appending the parameters to the
> - * CAPTCHA servlet.
> + * This method is used for rendering the CAPTCHA component.
> */
> - private String appendParameters(CAPTCHAComponent component) {
> - return CAPTCHAComponent.ATTRIBUTE_CAPTCHASESSIONKEYNAME + "="
> - + component.getCaptchaSessionKeyName();
> - }
> + 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 = null;
> + 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();
> + }
> + }
> }
>
> Added: 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=640864&view=auto
> ==============================================================================
> --- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java (added)
> +++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAConstants.java Tue Mar 25 08:40:50 2008
> @@ -0,0 +1,36 @@
> +/*
> + * 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;
> +
> +/**
> + * 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 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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
> @@ -35,16 +35,10 @@
>
>
> /**
> - * This class is responsible for generating the
> - * CAPTCHA image.
> + * 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;
> +
> private static final Color startingColor = new Color(150, 50, 150);
> private static final Color endingColor = new Color(255, 255, 255);
>
> @@ -53,8 +47,6 @@
> */
> private void drawTextOnImage(Graphics2D graphics, String captchaText) {
>
> -
> -
> Font font = null;
> TextLayout textLayout = null;
> double currentFontStatus = Math.random();
> @@ -71,7 +63,8 @@
> textLayout = new TextLayout(captchaText, font, graphics
> .getFontRenderContext());
>
> - textLayout.draw(graphics, TEXT_X_COORDINATE, TEXT_Y_COORDINATE);
> + textLayout.draw(graphics, CAPTCHAConstants.TEXT_X_COORDINATE,
> + CAPTCHAConstants.TEXT_Y_COORDINATE);
> }
>
> /*
> @@ -116,8 +109,9 @@
>
>
> // Create the CAPTCHA Image.
> - bufferedImage = new BufferedImage(CAPTCHA_WIDTH,
> - CAPTCHA_HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
> + bufferedImage = new BufferedImage(CAPTCHAConstants.CAPTCHA_WIDTH,
> + CAPTCHAConstants.CAPTCHA_HEIGHT,
> + BufferedImage.TYPE_BYTE_INDEXED);
>
> // Setup the graphics object.
> graphics = bufferedImage.createGraphics();
> @@ -200,20 +194,21 @@
> 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);
> + 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, 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);
> + 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);
> }
>
> /*
> @@ -223,7 +218,8 @@
> 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);
> + + (2 * CAPTCHAConstants.PI * (double) phase)
> + / (double) frames);
> }
>
> /*
>
> Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java
> URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java?rev=640864&view=auto
> ==============================================================================
> --- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java (added)
> +++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAResponseStream.java Tue Mar 25 08:40:50 2008
> @@ -0,0 +1,56 @@
> +/*
> + * 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.io.IOException;
> +import java.io.OutputStream;
> +import javax.faces.context.ResponseStream;
> +
> +/**
> + * This class is responsible for wrapping the CAPTCHA Image
> + * response stream.
> + */
> +public final class CAPTCHAResponseStream extends ResponseStream {
> + private final OutputStream _out;
> +
> + public CAPTCHAResponseStream(OutputStream out) {
> + _out = out;
> + }
> +
> + public void close() throws IOException {
> + _out.flush();
> + _out.close();
> + }
> +
> + public void flush() throws IOException {
> + _out.flush();
> + }
> +
> + public void write(byte[] b, int off, int len) throws IOException {
> + _out.write(b, off, len);
> + }
> +
> + public void write(byte[] b) throws IOException {
> + _out.write(b);
> + }
> +
> + public void write(int b) throws IOException {
> + _out.write(b);
> + }
> +}
>
> Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java
> URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java?rev=640864&r1=640863&r2=640864&view=diff
> ==============================================================================
> --- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java (original)
> +++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java Tue Mar 25 08:40:50 2008
> @@ -20,9 +20,16 @@
> package org.apache.myfaces.custom.util;
>
> import javax.faces.component.UIComponent;
> +import javax.faces.component.UIParameter;
> import javax.faces.component.html.HtmlMessages;
> import javax.faces.context.FacesContext;
> +import javax.faces.lifecycle.LifecycleFactory;
> +import javax.faces.webapp.FacesServlet;
> +import javax.servlet.ServletContext;
> +
> +import java.util.HashMap;
> import java.util.Iterator;
> +import java.util.Map;
>
> /**
> * User: treeder
> @@ -32,7 +39,7 @@
> public final class ComponentUtils
> {
>
> - private ComponentUtils(){
> + private ComponentUtils() {
>
> }
>
> @@ -44,111 +51,140 @@
> * @param clientId
> * @return component referenced by clientId or null if not found
> */
> - public static UIComponent findComponentByClientId(FacesContext context, UIComponent root, String clientId)
> - {
> - UIComponent component = null;
> - for (int i = 0; i < root.getChildCount() && component == null; i++)
> - {
> - UIComponent child = (UIComponent) root.getChildren().get(i);
> - component = findComponentByClientId(context, child, clientId);
> - }
> - if (root.getId() != null)
> - {
> - if (component == null && root.getClientId(context).equals(clientId))
> - {
> - component = root;
> - }
> - }
> - return component;
> - }
> + public static UIComponent findComponentByClientId(FacesContext context,
> + UIComponent root, String clientId) {
> + UIComponent component = null;
> + for (int i = 0; i < root.getChildCount() && component == null; i++) {
> + UIComponent child = (UIComponent) root.getChildren().get(i);
> + component = findComponentByClientId(context, child, clientId);
> + }
> + if (root.getId() != null) {
> + if (component == null && root.getClientId(context).equals(clientId)) {
> + component = root;
> + }
> + }
> + return component;
> + }
>
> /**
> - * Useful if you don't know the clientId
> - * <p/>
> - * TR- This was moved from AjaxPhaseListenere on checkin 344383
> - * Seems like this could be made more efficient
> - *
> - * @param context
> - * @param root
> - * @param id
> - * @return component referenced by id or null if not found
> - */
> - public static UIComponent findComponentById(FacesContext context, UIComponent root, String id)
> - {
> - UIComponent component = null;
> - for (int i = 0; i < root.getChildCount() && component == null; i++)
> - {
> - UIComponent child = (UIComponent) root.getChildren().get(i);
> - component = findComponentById(context, child, id);
> - }
> - //System.out.println("component looking for: " + id + " - rootid: " + root.getId() + " " + root);
> - if (root.getId() != null)
> - {
> - if (component == null && root.getId().equals(id))
> - {
> - component = root;
> - }
> - }
> - return component;
> - }
> -
> - public static UIComponent findFirstMessagesComponent(FacesContext context, UIComponent base)
> - {
> - if (base == null)
> - {
> - return null;
> - }
> -
> - if (base instanceof HtmlMessages)
> - {
> - return base;
> - }
> -
> - Iterator iterChildren = base.getFacetsAndChildren();
> - while (iterChildren.hasNext())
> - {
> - UIComponent child = (UIComponent) iterChildren.next();
> -
> - UIComponent found = findFirstMessagesComponent(context, child);
> - if (found != null)
> - {
> - return found;
> - }
> - }
> + * Useful if you don't know the clientId <p/> TR- This was moved from
> + * AjaxPhaseListenere on checkin 344383 Seems like this could be made more
> + * efficient
> + *
> + * @param context
> + * @param root
> + * @param id
> + * @return component referenced by id or null if not found
> + */
> + public static UIComponent findComponentById(FacesContext context,
> + UIComponent root, String id) {
> + UIComponent component = null;
> + for (int i = 0; i < root.getChildCount() && component == null; i++) {
> + UIComponent child = (UIComponent) root.getChildren().get(i);
> + component = findComponentById(context, child, id);
> + }
> + // System.out.println("component looking for: " + id + " - rootid: " +
> + // root.getId() + " " + root);
> + if (root.getId() != null) {
> + if (component == null && root.getId().equals(id)) {
> + component = root;
> + }
> + }
> + return component;
> + }
>
> - return null;
> - }
> -
> -
> - private static boolean isDecorated(UIComponent component, String attribute, String value) {
> - String attributeValue = (String) component.getAttributes().get(attribute);
> -
> - if(attributeValue == null || attributeValue.indexOf(value) == -1)
> + public static UIComponent findFirstMessagesComponent(FacesContext context,
> + UIComponent base) {
> + if (base == null) {
> + return null;
> + }
> +
> + if (base instanceof HtmlMessages) {
> + return base;
> + }
> +
> + Iterator iterChildren = base.getFacetsAndChildren();
> + while (iterChildren.hasNext()) {
> + UIComponent child = (UIComponent) iterChildren.next();
> +
> + UIComponent found = findFirstMessagesComponent(context, child);
> + if (found != null) {
> + return found;
> + }
> + }
> +
> + return null;
> + }
> +
> + private static boolean isDecorated(UIComponent component, String attribute,
> + String value) {
> + String attributeValue = (String) component.getAttributes().get(
> + attribute);
> +
> + if (attributeValue == null || attributeValue.indexOf(value) == -1)
> return false;
> else
> return true;
> }
>
> /**
> - * Changes the event attributes like onclick by appending the given value
> - *
> - * @param component UIComponent instance that the attribute belongs to
> - * @param attribute Attribute to be changed
> - * @param value Value to be appended
> - */
> - public static void decorateEventAttribute(UIComponent component, String attribute, String value) {
> - if(isDecorated(component, attribute, value))
> + * Changes the event attributes like onclick by appending the given value
> + *
> + * @param component
> + * UIComponent instance that the attribute belongs to
> + * @param attribute
> + * Attribute to be changed
> + * @param value
> + * Value to be appended
> + */
> + public static void decorateEventAttribute(UIComponent component,
> + String attribute, String value) {
> + if (isDecorated(component, attribute, value))
> return;
> -
> - String attributeValue = (String) component.getAttributes().get(attribute);
> -
> - if(attributeValue == null)
> +
> + String attributeValue = (String) component.getAttributes().get(
> + attribute);
> +
> + if (attributeValue == null)
> component.getAttributes().put(attribute, value);
> + else if (attributeValue.endsWith(";"))
> + component.getAttributes().put(attribute, attributeValue + value);
> else
> - if( attributeValue.endsWith(";"))
> - component.getAttributes().put(attribute, attributeValue + value);
> - else
> - component.getAttributes().put(attribute, attributeValue + ";" + value);
> + component.getAttributes().put(attribute,
> + attributeValue + ";" + value);
> }
> -
> +
> + /**
> + * The getParameterMap() is used for getting the parameters
> + * of a specific component.
> + * @param component
> + * @return the Map of the component.
> + */
> + public static Map getParameterMap(UIComponent component) {
> + Map result = new HashMap();
> + for (Iterator iter = component.getChildren().iterator(); iter.hasNext();) {
> + UIComponent child = (UIComponent) iter.next();
> + if (child instanceof UIParameter) {
> + UIParameter uiparam = (UIParameter) child;
> + Object value = uiparam.getValue();
> + if (value != null) {
> + result.put(uiparam.getName(), value);
> + }
> + }
> + }
> + return result;
> + }
> +
> + /**
> + * The getLifecycleId() is used for getting the id of
> + * the Lifecycle from the ServletContext.
> + * @param context
> + * @return the id of the life cycle.
> + */
> + public static String getLifecycleId(ServletContext context) {
> + String lifecycleId = context
> + .getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
> + return lifecycleId != null ? lifecycleId
> + : LifecycleFactory.DEFAULT_LIFECYCLE;
> + }
> }
>
> 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=640864&r1=640863&r2=640864&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 Tue Mar 25 08:40:50 2008
> @@ -177,12 +177,6 @@
> <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>
> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
> @@ -201,11 +195,7 @@
> <servlet-mapping>
> <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>
> + </servlet-mapping>
> <welcome-file-list>
> <welcome-file>index.html</welcome-file>
> <welcome-file>index.htm</welcome-file>
>
>
>
>