Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
20   81   4   10
0   44   0,2   2
2     2  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  ImageCaptchaServlet       Line # 20 20 0% 4 22 0% 0.0
 
No Tests
 
1    package net.sourceforge.pebble.confirmation;
2   
3    import com.octo.captcha.service.CaptchaServiceException;
4   
5    import javax.servlet.ServletConfig;
6    import javax.servlet.ServletException;
7    import javax.servlet.ServletOutputStream;
8    import javax.servlet.http.HttpServlet;
9    import javax.servlet.http.HttpServletRequest;
10    import javax.servlet.http.HttpServletResponse;
11    import java.awt.image.BufferedImage;
12    import java.io.ByteArrayOutputStream;
13    import java.io.IOException;
14   
15    /**
16    * Servlet that serves up the JCaptcha image captcha.
17    *
18    * @author Simon Brown
19    */
 
20    public class ImageCaptchaServlet extends HttpServlet {
21   
22    private static final long serialVersionUID = -6227490839816434342L;
23   
24    private static final String JPG_FORMAT = "JPG";
25   
26    /**
27    * Called to initialise the servlet.
28    *
29    * @param servletConfig
30    * @throws ServletException
31    */
 
32  0 toggle public void init(ServletConfig servletConfig) throws ServletException {
33  0 super.init(servletConfig);
34    }
35   
36    /**
37    * Called when a HTTP GET request is made to the servlet.
38    *
39    * @param httpServletRequest
40    * @param httpServletResponse
41    * @throws ServletException
42    * @throws IOException
43    */
 
44  0 toggle protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
45  0 byte[] captchaChallengeAsJpeg;
46    // the output stream to render the captcha image as jpeg into
47  0 ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
48  0 try {
49    // get the session id that will identify the generated captcha.
50    // the same id must be used to validate the response, the session id is a good candidate!
51  0 String captchaId = httpServletRequest.getSession().getId();
52    // call the CaptchaService getChallenge method
53  0 BufferedImage challenge =
54    CaptchaService.getInstance().getImageChallengeForID(captchaId,
55    httpServletRequest.getLocale());
56   
57  0 javax.imageio.ImageIO.write(challenge, JPG_FORMAT, jpegOutputStream);
58   
59    } catch (IllegalArgumentException e) {
60  0 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
61  0 return;
62    } catch (CaptchaServiceException e) {
63  0 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
64  0 return;
65    }
66   
67  0 captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
68   
69    // flush it in the response
70  0 httpServletResponse.setHeader("Cache-Control", "no-store");
71  0 httpServletResponse.setHeader("Pragma", "no-cache");
72  0 httpServletResponse.setDateHeader("Expires", 0);
73  0 httpServletResponse.setContentType("image/jpeg");
74  0 ServletOutputStream responseOutputStream =
75    httpServletResponse.getOutputStream();
76  0 responseOutputStream.write(captchaChallengeAsJpeg);
77  0 responseOutputStream.flush();
78  0 responseOutputStream.close();
79    }
80   
81    }