Package org.b3log.latke.servlet.renderer

Examples of org.b3log.latke.servlet.renderer.PNGRenderer


     *
     * @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);
        }
    }
View Full Code Here


     *
     * @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 (CAPTCHAS.isEmpty()) {
            loadCaptchas();
        }

        try {
            final String row = String.valueOf(RANDOM.nextInt(MAX_CAPTCHA_ROW));
            String captcha = "";
            final List<Image> images = new ArrayList<Image>();
            for (int i = 0; i < LENGTH; i++) {
                final String column = String.valueOf(RANDOM.nextInt(MAX_CAPTCHA_COLUM));
                captcha += column;
                final String imageName = row + "/" + column + ".png";
                final Image captchaChar = CAPTCHAS.get(imageName);

                images.add(captchaChar);
            }

            final Image captchaImage = IMAGE_SERVICE.makeImage(images);

            final HttpServletRequest request = context.getRequest();
            final HttpServletResponse response = context.getResponse();

            final HttpSession httpSession = request.getSession(false);
            if (null != httpSession) {
                LOGGER.log(Level.FINER, "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(captchaImage);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
    }
View Full Code Here

TOP

Related Classes of org.b3log.latke.servlet.renderer.PNGRenderer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.