{
// getting CAPTCHA attributes.
HttpServletResponse response = (HttpServletResponse) facesContext
.getExternalContext().getResponse();
ResponseStream out = facesContext.getResponseStream();
Map requestMap = facesContext.getExternalContext()
.getRequestParameterMap();
String captchaSessionKeyName = requestMap.get(
CAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME).toString();
// construct the CAPTCHA image generator object.
CAPTCHAImageGenerator captchaImageGenerator = new CAPTCHAImageGenerator();
try
{
String captchaText;
Color endingColor = ColorGenerator.generateRandomColor(null);
Color startingColor = ColorGenerator
.generateRandomColor(endingColor);
// Generate random CAPTCHA text.
captchaText = CAPTCHATextGenerator.generateRandomText();
TomahawkConfig config = TomahawkConfig.getCurrentInstance(facesContext);
// Set the generated text in the user session.
facesContext.getExternalContext().getSessionMap().put(
config.isPrefixCaptchaSessionKey() ?
AbstractCAPTCHAComponent.ATTRIBUTE_CAPTCHA_SESSION_KEY_NAME+
"_"+captchaSessionKeyName : captchaSessionKeyName, captchaText);
// Generate the image, the BG color is randomized from starting to ending colors.
captchaImageGenerator.generateImage(response, captchaText,
startingColor, endingColor);
}
finally
{
out.close();
facesContext.responseComplete();
}
}