Search in sources :

Example 1 with PNGRenderer

use of org.b3log.latke.servlet.renderer.PNGRenderer in project solo by b3log.

the class CaptchaProcessor method get.

/**
     * Gets captcha.
     * 
     * @param context the specified context
     */
@RequestProcessing(value = "/captcha.do", method = HTTPRequestMethod.GET)
public void get(final HTTPRequestContext context) {
    final PNGRenderer renderer = new PNGRenderer();
    context.setRenderer(renderer);
    if (null == captchas) {
        loadCaptchas();
    }
    try {
        final HttpServletRequest request = context.getRequest();
        final HttpServletResponse response = context.getResponse();
        final Random random = new Random();
        final int index = random.nextInt(CAPTCHA_COUNT);
        final Image captchaImg = captchas[index];
        final String captcha = captchaImg.getName();
        final HttpSession httpSession = request.getSession(false);
        if (null != httpSession) {
            LOGGER.log(Level.DEBUG, "Captcha[{0}] for session[id={1}]", new Object[] { captcha, httpSession.getId() });
            httpSession.setAttribute(CAPTCHA, captcha);
        }
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        renderer.setImage(captchaImg);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PNGRenderer(org.b3log.latke.servlet.renderer.PNGRenderer) Random(java.util.Random) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) Image(org.b3log.latke.image.Image) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

Random (java.util.Random)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Image (org.b3log.latke.image.Image)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 PNGRenderer (org.b3log.latke.servlet.renderer.PNGRenderer)1