package servlet;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;

public class CapchaServlet extends HttpServlet {

    private int height = 0;
    private int width = 0;

    public static final String CAPTCHA_KEY = "captcha_key_name";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        height = Integer.parseInt(getServletConfig().getInitParameter("height"));
        width = Integer.parseInt(getServletConfig().getInitParameter("width"));
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse response)
            throws IOException, ServletException {
        //Expire response
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Max-Age", 0);

        // generate word
        Random r = new Random();
//        String token = Long.toString(Math.abs(r.nextLong()), 36);
        String token = Long.toString(r.nextLong(), 36);
        String ch = token.substring(0, 6);

        // draw image
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.setPaint(new Color(255, 255, 255));
        graphics2D.fillRect(0, 0, image.getWidth(), image.getHeight());
        graphics2D.setColor(new Color(0, 176, 240));
        Font font = new Font("Cambria", Font.CENTER_BASELINE, 26);
        graphics2D.setFont(font);

        graphics2D.drawString(ch, 2, 20);
        graphics2D.dispose();
        HttpSession session = req.getSession(true);
        session.setAttribute(CAPTCHA_KEY, ch);

        OutputStream outputStream = response.getOutputStream();
        ImageIO.write(image, "jpeg", outputStream);
        outputStream.close();
    }

}
