View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.module.servlet.image;
8   
9   import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
10  import com.octo.captcha.service.image.ImageCaptchaService;
11  import com.octo.captcha.service.CaptchaServiceException;
12  
13  import javax.imageio.ImageIO;
14  import javax.servlet.Servlet;
15  import javax.servlet.ServletException;
16  import javax.servlet.ServletOutputStream;
17  import javax.servlet.http.HttpServlet;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  import java.awt.image.BufferedImage;
21  import java.io.IOException;
22  
23  /***
24   * @author mag
25   * @Date 14 févr. 2009
26   */
27  public class SimpleImageCaptchaServlet extends HttpServlet implements Servlet
28   {
29      public static ImageCaptchaService service= new DefaultManageableImageCaptchaService();
30  
31  
32  
33       @Override
34       protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
35                // Set to expire far in the past.
36                      httpServletResponse.setDateHeader("Expires", 0);
37                      // Set standard HTTP/1.1 no-cache headers.
38                      httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
39                      // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
40                      httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
41                      // Set standard HTTP/1.0 no-cache header.
42                      httpServletResponse.setHeader("Pragma", "no-cache");
43  
44                      // return a jpeg
45                      httpServletResponse.setContentType("image/jpeg");
46  
47  
48                      // create the image with the text
49                      BufferedImage bi = service.getImageChallengeForID(httpServletRequest.getSession(true).getId());
50  
51                      ServletOutputStream out = httpServletResponse.getOutputStream();
52  
53                      // write the data out
54                      ImageIO.write(bi, "jpg", out);
55                      try
56                      {
57                              out.flush();
58                      }
59                      finally
60                      {
61                              out.close();
62                      }
63      }
64  
65  
66       public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse){
67           //if no session found
68           if(request.getSession(false)==null)return false;
69           //else use service and session id to validate
70           boolean validated = false;
71           try {
72               validated = service.validateResponseForID(request.getSession().getId(),userCaptchaResponse);
73           } catch (CaptchaServiceException e) {
74             //do nothing.. false
75           }
76           return validated;
77       }
78   }