Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4609 phani.kuma 1
package in.shop2020.serving.controllers;
2
 
3
import static nl.captcha.Captcha.NAME;
4
import nl.captcha.servlet.CaptchaServletUtil;
5
 
6
import java.awt.Color;
7
import java.awt.Font;
8
import java.io.IOException;
9
import java.util.ArrayList;
10
import java.util.List;
11
 
12
import javax.servlet.http.Cookie;
13
 
14
import nl.captcha.Captcha;
4613 phani.kuma 15
import nl.captcha.backgrounds.TransparentBackgroundProducer;
4609 phani.kuma 16
import nl.captcha.text.renderer.DefaultWordRenderer;
17
import in.shop2020.serving.utils.DesEncrypter;
18
 
19
public class CaptchaController extends BaseController {
20
 
21
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
22
 
23
	private static final long serialVersionUID = 1L;
24
 
25
    private static int _width = 200;
26
    private static int _height = 57;
27
 
28
    private static final List<Color> COLORS = new ArrayList<Color>(2);
29
    private static final List<Font> FONTS = new ArrayList<Font>(3);
30
 
31
    static {
32
        COLORS.add(Color.BLACK);
33
        COLORS.add(Color.BLUE);
34
 
35
        FONTS.add(new Font("Geneva", Font.ITALIC, 48));
36
        FONTS.add(new Font("Courier", Font.BOLD, 48));
37
        FONTS.add(new Font("Arial", Font.BOLD, 48));
38
    }
39
 
40
	public String index() throws IOException {
41
		DefaultWordRenderer wordRenderer = new DefaultWordRenderer(COLORS, FONTS);
42
        Captcha captcha = new Captcha.Builder(_width, _height).addText(wordRenderer)
43
                .gimp()
44
                .addNoise()
4613 phani.kuma 45
                .addBackground(new TransparentBackgroundProducer())
4609 phani.kuma 46
                .addBorder()
47
                .build();
48
 
49
        String answer = captcha.getAnswer();
50
        String encryptAnswer = desEncrypter.encrypt(answer);
51
        Cookie answerCookie = createCookie(NAME, "", encryptAnswer, null);
52
        response.addCookie(answerCookie);
53
        CaptchaServletUtil.writeImage(response, captcha.getImage());
54
		return "index";
55
	}
56
 
57
	private Cookie createCookie(String name, String domain, String value, Integer age) {
58
        Cookie cookie = new Cookie(name, value);
59
        if (age != null) {
60
            cookie.setMaxAge(age);
61
        }
62
        cookie.setPath("/");
63
        if (!domain.isEmpty()) {
64
            cookie.setDomain(domain);
65
        }
66
        return cookie;
67
    }
68
 
69
}