1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.custom.captcha;
20
21 import java.io.IOException;
22
23 import javax.faces.application.Resource;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.faces.render.Renderer;
28
29 import org.apache.myfaces.custom.captcha.util.CAPTCHAConstants;
30 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
31
32 /**
33 *
34 * @JSFRenderer
35 * renderKitId = "HTML_BASIC"
36 * family = "org.apache.myfaces.CAPTCHA"
37 * type = "org.apache.myfaces.CAPTCHA"
38 * @since 1.1.7
39 * @author Hazem Saleh
40 *
41 */
42 public class CAPTCHARenderer extends Renderer/* implements ResourceLoader*/
43 {
44
45 public void encodeBegin(FacesContext context, UIComponent component)
46 throws IOException
47 {
48
49 CAPTCHAComponent captchaComponent = (CAPTCHAComponent) component;
50
51 generateImageTag(context, captchaComponent);
52 }
53
54 public void encodeEnd(FacesContext context, UIComponent component)
55 throws IOException
56 {
57 super.encodeEnd(context, component);
58 }
59
60 /*
61 * This helper method is used for generating the img tag that will
62 * use the (AddResource) to generate the url of the generated image.
63 */
64 private void generateImageTag(FacesContext context,
65 CAPTCHAComponent component) throws IOException
66 {
67
68 //AddResource addResource;
69 ResponseWriter writer = context.getResponseWriter();
70 //Map params = HtmlComponentUtils.getParameterMap(component);
71 String url;
72 String captchaSessionKeyName = component.getCaptchaSessionKeyName();
73 String width = component.getImageWidth();
74 String height = component.getImageHeight();
75
76 // determine width and height of the generated image.
77 if (width == null)
78 {
79 width = CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH + "";
80 }
81
82 if (height == null)
83 {
84 height = CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT + "";
85 }
86
87 writer.startElement(HTML.IMG_ELEM, component);
88
89 // constructing the parameter map to be passed to the (AddResource).
90 //if (captchaSessionKeyName != null)
91 //{
92 // params.put(CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME,
93 // captchaSessionKeyName);
94 //}
95
96 // write the url to trigger the (AddResource).
97 //addResource = AddResourceFactory.getInstance(context);
98
99 //url = context.getExternalContext().encodeResourceURL(
100 // addResource.getResourceUri(context,
101 // new ParameterResourceHandler(this.getClass(), params)));
102
103 // Use facesContext attribute map to pass params to createResource. I would like to do
104 // that in a cleaner way but we don't have choice.
105 context.getAttributes().put(CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME,
106 captchaSessionKeyName);
107 Resource resource = context.getApplication().getResourceHandler().createResource(
108 "captcha", CAPTCHAResourceHandlerWrapper.CAPTCHA_LIBRARY);
109 context.getAttributes().remove(CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME);
110 url = resource.getRequestPath();
111
112 writer.writeAttribute(HTML.SRC_ATTR, url, null);
113
114 // write rest of attributes.
115 writer.writeAttribute(HTML.WIDTH_ATTR, width, null);
116
117 writer.writeAttribute(HTML.HEIGHT_ATTR, height, null);
118
119 writer.endElement(HTML.IMG_ELEM);
120 }
121
122 /*
123 * This method is implemented to be called from the (AddResource).
124 * It wraps the CAPTCHA image generation.
125 */
126 /*
127 public void serveResource(ServletContext servletContext,
128 HttpServletRequest request, HttpServletResponse response,
129 String resourceUri) throws IOException
130 {
131
132 // get the FacesContext from the ServletContext.
133 FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder
134 .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
135 LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
136 .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
137 Lifecycle lifecycle = lifecycleFactory.getLifecycle(HtmlComponentUtils
138 .getLifecycleId(servletContext));
139 FacesContext facesContext = facesContextFactory.getFacesContext(
140 servletContext, request, response, lifecycle);
141 facesContext.setResponseStream(new CAPTCHAResponseStream(response
142 .getOutputStream()));
143
144 // render the CAPTCHA.
145 try
146 {
147 try
148 {
149 renderCAPTCHA(facesContext);
150 }
151 catch (IOException e)
152 {
153 throw new FacesException("Could not render the CAPTCHA : "
154 + e.getMessage(), e);
155 }
156 facesContext.getResponseStream().close();
157 }
158 finally
159 {
160 facesContext.release();
161 }
162 }*/
163
164 /*
165 * This method is used for rendering the CAPTCHA component.
166 */
167 /*
168 protected void renderCAPTCHA(FacesContext facesContext) throws IOException
169 {
170
171 // getting CAPTCHA attributes.
172 HttpServletResponse response = (HttpServletResponse) facesContext
173 .getExternalContext().getResponse();
174 ResponseStream out = facesContext.getResponseStream();
175 Map requestMap = facesContext.getExternalContext()
176 .getRequestParameterMap();
177 String captchaSessionKeyName = requestMap.get(
178 CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME).toString();
179
180 // construct the CAPTCHA image generator object.
181 CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
182
183 try
184 {
185 String captchaText;
186 Color endingColor = ColorGenerator.generateRandomColor(null);
187 Color startingColor = ColorGenerator
188 .generateRandomColor(endingColor);
189
190 // Generate random CAPTCHA text.
191 captchaText = CAPTCHATextGenerator.generateRandomText();
192
193 // Generate the image, the BG color is randomized from starting to ending colors.
194 captchaImageGenerator.generateImage(response, captchaText,
195 startingColor, endingColor);
196
197 // Set the generated text in the user session.
198 facesContext.getExternalContext().getSessionMap().put(
199 captchaSessionKeyName, captchaText);
200 }
201 finally
202 {
203 out.close();
204 facesContext.responseComplete();
205 }
206 }*/
207 }