imagecaptchaservlet.java

### File Source: imagecaptchaservlet.java

             /*
        P/P   *  Method: net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init
              */
         1  package net.sourceforge.pebble.confirmation;
         2
             /*
        P/P   *  Method: void net.sourceforge.pebble.confirmation.ImageCaptchaServlet()
              */
         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    public void init(ServletConfig servletConfig) throws ServletException {
                 /*
        P/P       *  Method: void init(ServletConfig)
                  */
        33      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    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        45      byte[] captchaChallengeAsJpeg;
        46      // the output stream to render the captcha image as jpeg into
        47      ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        48      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        String captchaId = httpServletRequest.getSession().getId();
        52        // call the CaptchaService getChallenge method
        53        BufferedImage challenge =
        54            CaptchaService.getInstance().getImageChallengeForID(captchaId,
        55                httpServletRequest.getLocale());
        56
        57        javax.imageio.ImageIO.write(challenge, JPG_FORMAT, jpegOutputStream);
        58
        59      } catch (IllegalArgumentException e) {
        60        httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        61        return;
        62      } catch (CaptchaServiceException e) {
        63        httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        64        return;
        65      }
        66
        67      captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
        68
        69      // flush it in the response
        70      httpServletResponse.setHeader("Cache-Control", "no-store");
        71      httpServletResponse.setHeader("Pragma", "no-cache");
        72      httpServletResponse.setDateHeader("Expires", 0);
        73      httpServletResponse.setContentType("image/jpeg");
        74      ServletOutputStream responseOutputStream =
        75          httpServletResponse.getOutputStream();
        76      responseOutputStream.write(captchaChallengeAsJpeg);
        77      responseOutputStream.flush();
        78      responseOutputStream.close();
        79    }
        80
        81  }

SofCheck Inspector Build Version : 2.22510

imagecaptchaservlet.java | 2010-Jun-25 19:40:32
imagecaptchaservlet.class | 2010-Jul-19 20:23:40