//# 0 errors, 17 messages
//#
package net.sourceforge.pebble.confirmation;
    //#imagecaptchaservlet.java:1:1: class: net.sourceforge.pebble.confirmation.ImageCaptchaServlet
    //#imagecaptchaservlet.java:1:1: method: net.sourceforge.pebble.confirmation.ImageCaptchaServlet.net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init

import com.octo.captcha.service.CaptchaServiceException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Servlet that serves up the JCaptcha image captcha.
 *
 * @author Simon Brown
 */
public class ImageCaptchaServlet extends HttpServlet {
    //#imagecaptchaservlet.java:20: method: void net.sourceforge.pebble.confirmation.ImageCaptchaServlet.net.sourceforge.pebble.confirmation.ImageCaptchaServlet()
    //#input(void net.sourceforge.pebble.confirmation.ImageCaptchaServlet()): this
    //#imagecaptchaservlet.java:20: end of method: void net.sourceforge.pebble.confirmation.ImageCaptchaServlet.net.sourceforge.pebble.confirmation.ImageCaptchaServlet()

  private static final long serialVersionUID = -6227490839816434342L;

  private static final String JPG_FORMAT = "JPG";
  
  /**
   * Called to initialise the servlet.
   *
   * @param servletConfig
   * @throws ServletException
   */
  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    //#imagecaptchaservlet.java:33: method: void net.sourceforge.pebble.confirmation.ImageCaptchaServlet.init(ServletConfig)
    //#input(void init(ServletConfig)): servletConfig
    //#input(void init(ServletConfig)): this
  }
    //#imagecaptchaservlet.java:34: end of method: void net.sourceforge.pebble.confirmation.ImageCaptchaServlet.init(ServletConfig)

  /**
   * Called when a HTTP GET request is made to the servlet.
   *
   * @param httpServletRequest
   * @param httpServletResponse
   * @throws ServletException
   * @throws IOException
   */
  protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    byte[] captchaChallengeAsJpeg;
    // the output stream to render the captcha image as jpeg into
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    try {
      // get the session id that will identify the generated captcha.
      // the same id must be used to validate the response, the session id is a good candidate!
      String captchaId = httpServletRequest.getSession().getId();
      // call the CaptchaService getChallenge method
      BufferedImage challenge =
          CaptchaService.getInstance().getImageChallengeForID(captchaId,
              httpServletRequest.getLocale());

      javax.imageio.ImageIO.write(challenge, JPG_FORMAT, jpegOutputStream);
      
    } catch (IllegalArgumentException e) {
      httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    } catch (CaptchaServiceException e) {
      httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    }

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    // flush it in the response
    httpServletResponse.setHeader("Cache-Control", "no-store");
    httpServletResponse.setHeader("Pragma", "no-cache");
    httpServletResponse.setDateHeader("Expires", 0);
    httpServletResponse.setContentType("image/jpeg");
    ServletOutputStream responseOutputStream =
        httpServletResponse.getOutputStream();
    responseOutputStream.write(captchaChallengeAsJpeg);
    responseOutputStream.flush();
    responseOutputStream.close();
  }

}    //#output(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Descendant_Table[net/sourceforge/pebble/confirmation/ImageCaptchaServlet]
    //#output(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Dispatch_Table.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V
    //#output(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Dispatch_Table.init(Ljavax/servlet/ServletConfig;)V
    //#post(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Descendant_Table[net/sourceforge/pebble/confirmation/ImageCaptchaServlet] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Dispatch_Table.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V == &doGet
    //#post(net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init): __Dispatch_Table.init(Ljavax/servlet/ServletConfig;)V == &init
    //#imagecaptchaservlet.java:: end of method: net.sourceforge.pebble.confirmation.ImageCaptchaServlet.net.sourceforge.pebble.confirmation.ImageCaptchaServlet__static_init
    //#imagecaptchaservlet.java:: end of class: net.sourceforge.pebble.confirmation.ImageCaptchaServlet
