001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.login;
020:
021: import java.awt.image.BufferedImage;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024:
025: import javax.security.auth.login.LoginException;
026: import javax.servlet.ServletConfig;
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import com.nabhinc.core.AbstractServlet;
032: import com.nabhinc.util.ServletUtil;
033: import com.nabhinc.util.jcaptcha.SimpleListImageCaptchaEngine;
034: import com.octo.captcha.Captcha;
035: import com.octo.captcha.engine.image.ImageCaptchaEngine;
036: import com.octo.captcha.image.ImageCaptcha;
037: import com.sun.image.codec.jpeg.JPEGCodec;
038: import com.sun.image.codec.jpeg.JPEGImageEncoder;
039:
040: /**
041: *
042: *
043: * @author Padmanabh Dabke
044: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
045: */
046: public class JCaptchaServlet extends AbstractServlet {
047: private static final long serialVersionUID = -114403114573862213L;
048: public static final String IMAGE_ATTRIBUTE_PARAM = "imageAttribute";
049: public static final String IMAGE_CAPTCHA_ENGINE_PARAM = "imageCaptchaEngine";
050: private ImageCaptchaEngine jcsCaptchaEngine = new SimpleListImageCaptchaEngine();
051:
052: public void init(ServletConfig config) throws ServletException {
053: super .init(config);
054:
055: String captchaEngineClass = config
056: .getInitParameter(IMAGE_CAPTCHA_ENGINE_PARAM);
057: if (captchaEngineClass != null) {
058: try {
059: jcsCaptchaEngine = (com.octo.captcha.engine.image.ListImageCaptchaEngine) Class
060: .forName(captchaEngineClass).newInstance();
061: } catch (Exception ex) {
062: error("Failed to instantiate captcha engine: "
063: + captchaEngineClass, ex);
064: }
065: }
066: }
067:
068: public void doGet(HttpServletRequest request,
069: HttpServletResponse response) throws ServletException,
070: IOException {
071: byte[] jpegImageArray = null;
072: ImageCaptcha captcha = (ImageCaptcha) jcsCaptchaEngine
073: .getNextCaptcha(request.getLocale());
074: BufferedImage image = captcha.getImageChallenge();
075: ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
076: JPEGImageEncoder jpegImageEncoder = JPEGCodec
077: .createJPEGEncoder(jpegOutputStream);
078: jpegImageEncoder.encode(image);
079: jpegImageArray = jpegOutputStream.toByteArray();
080:
081: request.getSession().setAttribute(
082: LoginConstants.CAPTCHA_ANSWER_PARAM, captcha);
083:
084: ServletUtil.setNoCacheHeaders(request, response);
085:
086: response.setContentType("image/jpeg");
087: response.getOutputStream().write(jpegImageArray);
088:
089: }
090:
091: public static void verifyCaptchaAnswer(HttpServletRequest req)
092: throws LoginException {
093: String answer = req
094: .getParameter(LoginConstants.CAPTCHA_ANSWER_PARAM);
095: if (answer == null || "".equals(answer))
096: throw new LoginException(
097: "sb.portal.error.invalid_captcha_answer_msg");
098: Captcha expectedAnswer = (Captcha) req.getSession()
099: .getAttribute(LoginConstants.CAPTCHA_ANSWER_PARAM);
100: if (expectedAnswer == null) { // Session expired ??
101: throw new LoginException(
102: "sb.portal.error.login_internal_error_msg");
103: }
104: Boolean result = expectedAnswer.validateResponse(answer);
105: if (result.equals(Boolean.FALSE))
106: throw new LoginException(
107: "sb.portal.error.invalid_captcha_answer_msg");
108:
109: }
110:
111: }
|