Subversion Repositories SmartDukaan

Rev

Rev 4609 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import static nl.captcha.Captcha.NAME;
import nl.captcha.servlet.CaptchaServletUtil;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import nl.captcha.Captcha;
import nl.captcha.backgrounds.TransparentBackgroundProducer;
import nl.captcha.text.renderer.DefaultWordRenderer;
import in.shop2020.serving.utils.DesEncrypter;

public class CaptchaController extends BaseController {

        private DesEncrypter desEncrypter = new DesEncrypter("saholic");
        
        private static final long serialVersionUID = 1L;

    private static int _width = 200;
    private static int _height = 57;
    
    private static final List<Color> COLORS = new ArrayList<Color>(2);
    private static final List<Font> FONTS = new ArrayList<Font>(3);
    
    static {
        COLORS.add(Color.BLACK);
        COLORS.add(Color.BLUE);

        FONTS.add(new Font("Geneva", Font.ITALIC, 48));
        FONTS.add(new Font("Courier", Font.BOLD, 48));
        FONTS.add(new Font("Arial", Font.BOLD, 48));
    }
    
        public String index() throws IOException {
                DefaultWordRenderer wordRenderer = new DefaultWordRenderer(COLORS, FONTS);
        Captcha captcha = new Captcha.Builder(_width, _height).addText(wordRenderer)
                .gimp()
                .addNoise()
                .addBackground(new TransparentBackgroundProducer())
                .addBorder()
                .build();

        String answer = captcha.getAnswer();
        String encryptAnswer = desEncrypter.encrypt(answer);
        Cookie answerCookie = createCookie(NAME, "", encryptAnswer, null);
        response.addCookie(answerCookie);
        CaptchaServletUtil.writeImage(response, captcha.getImage());
                return "index";
        }

        private Cookie createCookie(String name, String domain, String value, Integer age) {
        Cookie cookie = new Cookie(name, value);
        if (age != null) {
            cookie.setMaxAge(age);
        }
        cookie.setPath("/");
        if (!domain.isEmpty()) {
            cookie.setDomain(domain);
        }
        return cookie;
    }

}