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.web.image;
8   
9   import java.awt.image.BufferedImage;
10  import java.io.ByteArrayOutputStream;
11  import java.io.IOException;
12  import java.util.Locale;
13  
14  import javax.imageio.ImageIO;
15  import javax.servlet.ServletOutputStream;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  import org.slf4j.Logger;
20  
21  import com.octo.captcha.service.CaptchaServiceException;
22  import com.octo.captcha.service.image.ImageCaptchaService;
23  
24  /***
25   * Helper class
26   *
27   * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
28   * @version 1.0
29   */
30  public class ImageToJpegHelper {
31  
32  
33      /***
34       * retrieve a new ImageCaptcha using ImageCaptchaService and flush it to the response.<br/> Captcha are localized
35       * using request locale.<br/>
36       * <p/>
37       * This method returns a 404 to the client instead of the image if the request isn't correct (missing parameters,
38       * etc...)..<br/> The log may be null.<br/>
39       *
40       * @param theRequest  the request
41       * @param theResponse the response
42       * @param log         a commons logger
43       * @param service     an ImageCaptchaService instance
44       *
45       * @throws java.io.IOException if a problem occurs during the jpeg generation process
46       */
47      public static void flushNewCaptchaToResponse(HttpServletRequest theRequest,
48                                                   HttpServletResponse theResponse,
49                                                   Logger log,
50                                                   ImageCaptchaService service,
51                                                   String id,
52                                                   Locale locale)
53              throws IOException {
54  
55          // call the ImageCaptchaService method to retrieve a captcha
56          byte[] captchaChallengeAsJpeg = null;
57          ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
58          try {
59              BufferedImage challenge =
60                      service.getImageChallengeForID(id, locale);
61              // the output stream to render the captcha image as jpeg into
62             ImageIO.write(challenge, "png",jpegOutputStream);
63          } catch (IllegalArgumentException e) {
64              //    log a security warning and return a 404...
65              if (log != null && log.isWarnEnabled()) {
66                  log.warn(
67                          "There was a try from "
68                                  + theRequest.getRemoteAddr()
69                                  + " to render an captcha with invalid ID :'" + id
70                                  + "' or with a too long one");
71                  theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
72                  return;
73              }
74          } catch (CaptchaServiceException e) {
75              // log and return a 404 instead of an image...
76              if (log != null && log.isWarnEnabled()) {
77                  log.warn(
78  
79                          "Error trying to generate a captcha and "
80                                  + "render its challenge as JPEG",
81                          e);
82              }
83              theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
84              return;
85          }
86          captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
87  
88          // render the captcha challenge as a JPEG image in the response
89          theResponse.setHeader("Cache-Control", "no-store");
90          theResponse.setHeader("Pragma", "no-cache");
91          theResponse.setDateHeader("Expires", 0);
92  
93          theResponse.setContentType("image/jpeg");
94          ServletOutputStream responseOutputStream =
95                  theResponse.getOutputStream();
96          responseOutputStream.write(captchaChallengeAsJpeg);
97          responseOutputStream.flush();
98          responseOutputStream.close();
99      }
100 
101 
102 }