Search in sources :

Example 1 with Image

use of org.b3log.latke.image.Image 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)

Example 2 with Image

use of org.b3log.latke.image.Image in project solo by b3log.

the class CaptchaProcessor method loadCaptchas.

/**
     * Loads captcha.
     */
private synchronized void loadCaptchas() {
    LOGGER.debug("Loading captchas....");
    try {
        captchas = new Image[CAPTCHA_COUNT];
        ZipFile zipFile;
        if (RuntimeEnv.LOCAL == Latkes.getRuntimeEnv()) {
            final InputStream inputStream = SoloServletListener.class.getClassLoader().getResourceAsStream("captcha_static.zip");
            final File file = File.createTempFile("b3log_captcha_static", null);
            final OutputStream outputStream = new FileOutputStream(file);
            IOUtils.copy(inputStream, outputStream);
            zipFile = new ZipFile(file);
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        } else {
            final URL captchaURL = SoloServletListener.class.getClassLoader().getResource("captcha_static.zip");
            zipFile = new ZipFile(captchaURL.getFile());
        }
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        int i = 0;
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(entry));
            final byte[] captchaCharData = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(captchaCharData);
            bufferedInputStream.close();
            final Image image = IMAGE_SERVICE.makeImage(captchaCharData);
            image.setName(entry.getName().substring(0, entry.getName().lastIndexOf('.')));
            captchas[i] = image;
            i++;
        }
        zipFile.close();
    } catch (final Exception e) {
        LOGGER.error("Can not load captchs!");
        throw new IllegalStateException(e);
    }
    LOGGER.debug("Loaded captch images");
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Image(org.b3log.latke.image.Image) URL(java.net.URL) SoloServletListener(org.b3log.solo.SoloServletListener) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

Image (org.b3log.latke.image.Image)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 Random (java.util.Random)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 PNGRenderer (org.b3log.latke.servlet.renderer.PNGRenderer)1 SoloServletListener (org.b3log.solo.SoloServletListener)1