KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > servlet > ImageCodeGenerator


1 package com.Yasna.servlet;
2
3 import javax.servlet.http.HttpServlet JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.ServletConfig JavaDoc;
7 import javax.servlet.ServletException JavaDoc;
8 import javax.imageio.ImageIO JavaDoc;
9 import java.awt.*;
10 import java.awt.font.FontRenderContext JavaDoc;
11 import java.awt.geom.Rectangle2D JavaDoc;
12 import java.awt.image.BufferedImage JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.InputStream JavaDoc;
17
18 /**
19  * Copyright (C) 2001 Yasna.com. All rights reserved.
20  *
21  * ===================================================================
22  * The Apache Software License, Version 1.1
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  *
31  * 2. Redistributions in binary form must reproduce the above copyright
32  * notice, this list of conditions and the following disclaimer in
33  * the documentation and/or other materials provided with the
34  * distribution.
35  *
36  * 3. The end-user documentation included with the redistribution,
37  * if any, must include the following acknowledgment:
38  * "This product includes software developed by
39  * Yasna.com (http://www.yasna.com)."
40  * Alternately, this acknowledgment may appear in the software itself,
41  * if and wherever such third-party acknowledgments normally appear.
42  *
43  * 4. The names "Yazd" and "Yasna.com" must not be used to
44  * endorse or promote products derived from this software without
45  * prior written permission. For written permission, please
46  * contact yazd@yasna.com.
47  *
48  * 5. Products derived from this software may not be called "Yazd",
49  * nor may "Yazd" appear in their name, without prior written
50  * permission of Yasna.com.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
53  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
56  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
59  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
62  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  * ====================================================================
65  *
66  * This software consists of voluntary contributions made by many
67  * individuals on behalf of Yasna.com. For more information
68  * on Yasna.com, please see <http://www.yasna.com>.
69  */

70
71 /**
72  * This servlet serves the images for the random code. This is to fight spamming new accounts with software tools.
73  */

74 public class ImageCodeGenerator extends HttpServlet JavaDoc{
75     private Font font;
76     private BufferedImage JavaDoc buffer;
77     private Graphics2D g2;
78     public void init (ServletConfig JavaDoc config)
79         throws ServletException JavaDoc {
80         System.setProperty( "java.awt.headless", "true" );
81         buffer = new BufferedImage JavaDoc(1,1,BufferedImage.TYPE_INT_RGB);
82         g2 = buffer.createGraphics();
83         try{
84             font = Font.createFont(Font.TRUETYPE_FONT,getClass().getResourceAsStream("Teenick.ttf")); //part of the jar file relative path to the class
85
font = font.deriveFont(29.0f);
86             System.err.println("Yazd Initialized Font");
87         } catch (Exception JavaDoc e){
88             System.err.println("Exception in Yazd Servlet 0: " + e.getMessage());
89             e.printStackTrace();
90         }
91         super.init (config);
92     }
93
94
95     public void service (HttpServletRequest JavaDoc request,
96                          HttpServletResponse JavaDoc response){
97         String JavaDoc code = (String JavaDoc)request.getSession().getAttribute("YazdCode");
98         
99         sendImage(response,code);
100
101     }
102
103     private void sendImage(HttpServletResponse JavaDoc response,String JavaDoc code){
104         try{
105
106
107 // prepare some output
108
buffer = new BufferedImage JavaDoc(90, 50,
109                            BufferedImage.TYPE_INT_RGB);
110 g2 = buffer.createGraphics();
111 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
112                     RenderingHints.VALUE_ANTIALIAS_ON);
113 g2.setFont(font);
114 // actually do the drawing
115
g2.setColor(Color.BLUE);
116             g2.fillRect(0,0,90,50);
117             g2.setColor(Color.WHITE);
118             g2.drawString(code,3,35);
119
120
121 // set the content type and get the output stream
122
response.setContentType("image/png");
123             OutputStream JavaDoc os = response.getOutputStream();
124
125 // output the image as png
126
ImageIO.write(buffer, "png", os);
127             os.close();
128
129
130         } catch (Exception JavaDoc e){
131             System.err.println("Exception in Yazd Servlet: " + e.getMessage());
132             e.printStackTrace();
133         }
134     }
135 }
136
Popular Tags