KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > Captcha


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Nov 13, 2004 / 17:17:09
40  * The JForum Project
41  * http://www.jforum.net
42  */

43
44 package net.jforum.util;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.image.BufferedImage JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53
54 import javax.imageio.ImageIO JavaDoc;
55
56 import net.jforum.JForumExecutionContext;
57 import net.jforum.SessionFacade;
58 import net.jforum.util.preferences.ConfigKeys;
59 import net.jforum.util.preferences.SystemGlobals;
60
61 import org.apache.log4j.Logger;
62
63 import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
64 import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
65 import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
66 import com.octo.captcha.component.image.fontgenerator.FontGenerator;
67 import com.octo.captcha.component.image.fontgenerator.TwistedAndShearedRandomFontGenerator;
68 import com.octo.captcha.component.image.textpaster.RandomTextPaster;
69 import com.octo.captcha.component.image.textpaster.TextPaster;
70 import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
71 import com.octo.captcha.component.image.wordtoimage.WordToImage;
72 import com.octo.captcha.component.wordgenerator.RandomWordGenerator;
73 import com.octo.captcha.component.wordgenerator.WordGenerator;
74 import com.octo.captcha.engine.image.ListImageCaptchaEngine;
75 import com.octo.captcha.image.ImageCaptchaFactory;
76 import com.octo.captcha.image.gimpy.GimpyFactory;
77
78 /**
79  * @author James Yong
80  * @version $Id: Captcha.java,v 1.8 2006/01/29 15:07:17 rafaelsteil Exp $
81  */

82 public class Captcha extends ListImageCaptchaEngine
83 {
84     private static final Logger logger = Logger.getLogger(Captcha.class);
85     
86     private static Captcha classInstance = new Captcha();
87     private List JavaDoc backgroundGeneratorList;
88     private List JavaDoc textPasterList;
89     private List JavaDoc fontGeneratorList;
90
91     private static final String JavaDoc charsInUse = "123456789ABCDEFGHJLKMNPRSTWXYZabcdefghijlmnopkrstuvxzyk@#%^";
92
93     /**
94      * Gets the singleton
95      *
96      * @return Instance of Captcha class
97      */

98     public static Captcha getInstance()
99     {
100         return classInstance;
101     }
102
103     protected void buildInitialFactories()
104     {
105         this.backgroundGeneratorList = new ArrayList JavaDoc();
106         this.textPasterList = new ArrayList JavaDoc();
107         this.fontGeneratorList = new ArrayList JavaDoc();
108         
109         int width = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_WIDTH);
110         int height = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_HEIGHT);
111         int minWords = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MIN_WORDS);
112         int maxWords = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MAX_WORDS);
113         int minFontSize = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MIN_FONT_SIZE);
114         int maxFontSize = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MAX_FONT_SIZE);
115
116         this.backgroundGeneratorList.add(new GradientBackgroundGenerator(new Integer JavaDoc(width),
117                 new Integer JavaDoc(height), Color.BLACK, Color.GRAY));
118         this.backgroundGeneratorList.add(new FunkyBackgroundGenerator(new Integer JavaDoc(250), new Integer JavaDoc(50)));
119
120         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.RED));
121         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.ORANGE));
122         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.BLUE));
123         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.WHITE));
124         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.GREEN));
125         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.GRAY));
126         this.textPasterList.add(new RandomTextPaster(new Integer JavaDoc(minWords), new Integer JavaDoc(maxWords), Color.YELLOW));
127
128         this.fontGeneratorList.add(new TwistedAndShearedRandomFontGenerator(new Integer JavaDoc(minFontSize), new Integer JavaDoc(maxFontSize)));
129
130         // Create a random word generator
131
WordGenerator words = new RandomWordGenerator(charsInUse);
132
133         for (Iterator JavaDoc fontIter = this.fontGeneratorList.iterator(); fontIter.hasNext();) {
134             FontGenerator fontGeny = (FontGenerator) fontIter.next();
135
136             for (Iterator JavaDoc backIter = this.backgroundGeneratorList.iterator(); backIter.hasNext();) {
137                 BackgroundGenerator bkgdGeny = (BackgroundGenerator) backIter.next();
138
139                 for (Iterator JavaDoc textIter = this.textPasterList.iterator(); textIter.hasNext();) {
140                     TextPaster textPaster = (TextPaster) textIter.next();
141
142                     WordToImage word2image = new ComposedWordToImage(fontGeny, bkgdGeny, textPaster);
143                     
144                     // Creates a ImageCaptcha Factory
145
ImageCaptchaFactory factory = new GimpyFactory(words, word2image);
146                     
147                     // Add a factory to the gimpy list (A Gimpy is a ImagCaptcha)
148
addFactory(factory);
149                 }
150             }
151         }
152     }
153
154     public void writeCaptchaImage()
155     {
156         BufferedImage JavaDoc image = SessionFacade.getUserSession().getCaptchaImage();
157         
158         if (image == null) {
159             return;
160         }
161
162         OutputStream JavaDoc outputStream = null;
163         
164         try {
165             outputStream = JForumExecutionContext.getResponse().getOutputStream();
166             ImageIO.write(image, "jpg", outputStream);
167         }
168         catch (IOException JavaDoc ex) {
169             logger.error(ex);
170         }
171         finally {
172             if (outputStream != null) {
173                 try {
174                     outputStream.close();
175                 }
176                 catch (IOException JavaDoc ex) {}
177             }
178         }
179     }
180 }
181
Popular Tags