KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > jee > authentication > http > CaptchaChallengeBuilder


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.jee.authentication.http;
29
30 import java.awt.image.BufferedImage JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Locale JavaDoc;
34
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39
40 import net.sf.jguard.ext.SecurityConstants;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import com.octo.captcha.module.config.CaptchaModuleConfig;
46 import com.octo.captcha.service.CaptchaService;
47 import com.octo.captcha.service.EhcacheManageableCaptchaService;
48 import com.sun.image.codec.jpeg.JPEGCodec;
49 import com.sun.image.codec.jpeg.JPEGImageEncoder;
50
51 /**
52  *
53  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
54  * @since 1.0
55  */

56 public class CaptchaChallengeBuilder {
57
58     private static final Log logger = LogFactory.getLog(CaptchaChallengeBuilder.class);
59     
60     public static void buildCaptchaChallenge(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
61         HttpSession JavaDoc session = request.getSession();
62         CaptchaService service = (CaptchaService) session.getServletContext().getAttribute(SecurityConstants.CAPTCHA_SERVICE);
63         if (service == null) {
64             logger.debug("captcha service should be defined ");
65             try {
66                 service = (CaptchaService)Thread.currentThread().getContextClassLoader().loadClass(CaptchaModuleConfig.getInstance().getServiceClass()).newInstance();
67                 session.getServletContext().setAttribute(SecurityConstants.CAPTCHA_SERVICE, service);
68             } catch (InstantiationException JavaDoc e) {
69                 logger.error(e.getMessage());
70             } catch (IllegalAccessException JavaDoc e) {
71                 logger.error(e.getMessage());
72             } catch (ClassNotFoundException JavaDoc e) {
73                 logger.error(e.getMessage());
74             }
75             logger.debug(" CAPTCHA SERVICE=" + service.getClass().getName() + " will be defined");
76             
77             if (service.getClass().isAssignableFrom(EhcacheManageableCaptchaService.class)) {
78                 ((EhcacheManageableCaptchaService) service).emptyCaptchaStore();
79             }
80             
81         } else {
82             logger.debug(" CAPTCHA SERVICE=" + service.getClass().getName());
83         }
84         byte[] captchaChallengeAsJpeg = buildCaptchaChallenge(session.getId(),request.getLocale(), service);
85         
86         
87         // flush it in the response
88
response.setHeader("Cache-Control", "no-store");
89         response.setHeader("Pragma", "no-cache");
90         response.setDateHeader("Expires", 0);
91         response.setContentType("image/jpeg");
92         ServletOutputStream JavaDoc responseOutputStream;
93         try {
94             responseOutputStream = response.getOutputStream();
95             responseOutputStream.write(captchaChallengeAsJpeg);
96             responseOutputStream.flush();
97             responseOutputStream.close();
98         } catch (IOException JavaDoc e) {
99             logger.error(" captcha cannot be generated", e);
100         }
101     }
102     
103     
104     /**
105      * build captcha challenge and return it as a byte array.
106      * @param captchaId
107      * @param locale
108      * @param service
109      * @return
110      * @throws IOException
111      */

112     private static byte[] buildCaptchaChallenge(String JavaDoc captchaId,Locale JavaDoc locale,CaptchaService service) throws IOException JavaDoc {
113         
114         byte[] captchaChallengeAsJpeg = null;
115         // the output stream to render the captcha image as jpeg into
116
ByteArrayOutputStream JavaDoc jpegOutputStream = new ByteArrayOutputStream JavaDoc();
117         // get the session id that will identify the generated captcha.
118
// the same id must be used to validate the response, the session id is a good candidate!
119
logger.debug("sessionID=" + captchaId);
120         // call the ImageCaptchaService getChallenge method
121
BufferedImage JavaDoc challenge = (BufferedImage JavaDoc) service.getChallengeForID(captchaId, locale);
122         logger.debug("challenge=" + service.getQuestionForID(captchaId, locale));
123         logger.debug(" service=" + service);
124
125         // a jpeg encoder
126
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
127         jpegEncoder.encode(challenge);
128         captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
129         return captchaChallengeAsJpeg;
130     }
131
132 }
133
Popular Tags