com.us.servlet.AuthCode.java Source code

Java tutorial

Introduction

Here is the source code for com.us.servlet.AuthCode.java

Source

/*
 * @(#)AuthCode.java 2011-9-29
 * ?us??  2008-2011, Inc. All rights reserved.
 * s.server. Use is subject to license terms.
 */

package com.us.servlet;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.math.RandomUtils;

import com.us.config.AppHelper;
import com.us.model.CodeAuth;
import com.us.util.ColorHelper;
import com.us.util.StringUtil;

/**
 * @author <a href="mailto:monlyu.hong@gmail.com">monlyu</a>
 * @version --0.0.0--, 2011-9-29
 * @since JDK6.0
 */
public class AuthCode implements Servlet {

    @Override
    public void execute(HttpServletRequest req, HttpServletResponse resp) {
        service(req, resp);
    }

    private final static char[] BIG_LETTERS = { 'A', 'B', 'C', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    private final static char[] SMALL_LETTER = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    private final static char[] NUMBERS = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

    protected void service(HttpServletRequest request, HttpServletResponse response) {
        final CodeAuth bean = AppHelper.CODE_AUTH;
        int width = NumberUtils.toInt(request.getParameter("width"), bean.getWidth());
        int height = NumberUtils.toInt(request.getParameter("height"), bean.getHeight());
        int x = width / (bean.getLength() + 1);
        int codeY = height - 4;
        int fontHeight = height - 2;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = image.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        if (StringUtil.hasText(request.getParameter("bgcolor"))) {
            graphics.setBackground(ColorHelper.hex2RGB(request.getParameter("bgcolor")));
        }
        graphics.fillRect(0, 0, width, height);
        graphics.setFont(new Font(bean.getFont(), Font.BOLD, fontHeight));
        graphics.drawRect(0, 0, width - 1, height - 1);
        // 
        if (bean.isBreakLine()) {
            for (int i = 0; i < 15; i++) {
                int x1 = RandomUtils.nextInt(width);
                int y1 = RandomUtils.nextInt(height);
                int x2 = RandomUtils.nextInt(12);
                int y2 = RandomUtils.nextInt(12);
                graphics.drawLine(x1, y1, x + x2, y1 + y2);
            }
        }
        char[] CHARSET_AREA = null;
        if (bean.getType().charAt(0) == '1') {
            CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, BIG_LETTERS);
        }
        if (bean.getType().charAt(1) == '1') {
            CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, SMALL_LETTER);
        }
        if (bean.getType().charAt(2) == '1') {
            CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, NUMBERS);
        }
        StringBuilder randomCode = new StringBuilder();
        for (int i = 0; i < bean.getLength(); i++) {
            String rand = String.valueOf(CHARSET_AREA[RandomUtils.nextInt(CHARSET_AREA.length)]);
            graphics.setColor(ColorHelper.color(RandomUtils.nextInt(255), RandomUtils.nextInt(255),
                    RandomUtils.nextInt(255)));
            graphics.drawString(rand, (i + 1) * x, codeY);
            randomCode.append(rand);
        }
        HttpSession session = request.getSession();
        session.setAttribute(bean.getSessionKey(), randomCode.toString());
        // ?
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/png");
        try {
            // Servlet?
            ServletOutputStream sos = response.getOutputStream();
            ImageIO.write(image, "png", sos);
            sos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}