001: package com.Yasna.servlet;
002:
003: import javax.servlet.http.HttpServlet;
004: import javax.servlet.http.HttpServletResponse;
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.ServletConfig;
007: import javax.servlet.ServletException;
008: import javax.imageio.ImageIO;
009: import java.awt.*;
010: import java.awt.font.FontRenderContext;
011: import java.awt.geom.Rectangle2D;
012: import java.awt.image.BufferedImage;
013: import java.io.OutputStream;
014: import java.io.FileInputStream;
015: import java.io.File;
016: import java.io.InputStream;
017:
018: /**
019: * Copyright (C) 2001 Yasna.com. All rights reserved.
020: *
021: * ===================================================================
022: * The Apache Software License, Version 1.1
023: *
024: * Redistribution and use in source and binary forms, with or without
025: * modification, are permitted provided that the following conditions
026: * are met:
027: *
028: * 1. Redistributions of source code must retain the above copyright
029: * notice, this list of conditions and the following disclaimer.
030: *
031: * 2. Redistributions in binary form must reproduce the above copyright
032: * notice, this list of conditions and the following disclaimer in
033: * the documentation and/or other materials provided with the
034: * distribution.
035: *
036: * 3. The end-user documentation included with the redistribution,
037: * if any, must include the following acknowledgment:
038: * "This product includes software developed by
039: * Yasna.com (http://www.yasna.com)."
040: * Alternately, this acknowledgment may appear in the software itself,
041: * if and wherever such third-party acknowledgments normally appear.
042: *
043: * 4. The names "Yazd" and "Yasna.com" must not be used to
044: * endorse or promote products derived from this software without
045: * prior written permission. For written permission, please
046: * contact yazd@yasna.com.
047: *
048: * 5. Products derived from this software may not be called "Yazd",
049: * nor may "Yazd" appear in their name, without prior written
050: * permission of Yasna.com.
051: *
052: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
053: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
054: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
055: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
056: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
057: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
058: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
059: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
060: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
061: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
062: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
063: * SUCH DAMAGE.
064: * ====================================================================
065: *
066: * This software consists of voluntary contributions made by many
067: * individuals on behalf of Yasna.com. For more information
068: * on Yasna.com, please see <http://www.yasna.com>.
069: */
070:
071: /**
072: * This servlet serves the images for the random code. This is to fight spamming new accounts with software tools.
073: */
074: public class ImageCodeGenerator extends HttpServlet {
075: private Font font;
076: private BufferedImage buffer;
077: private Graphics2D g2;
078:
079: public void init(ServletConfig config) throws ServletException {
080: System.setProperty("java.awt.headless", "true");
081: buffer = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
082: g2 = buffer.createGraphics();
083: try {
084: font = Font.createFont(Font.TRUETYPE_FONT, getClass()
085: .getResourceAsStream("Teenick.ttf")); //part of the jar file relative path to the class
086: font = font.deriveFont(29.0f);
087: System.err.println("Yazd Initialized Font");
088: } catch (Exception e) {
089: System.err.println("Exception in Yazd Servlet 0: "
090: + e.getMessage());
091: e.printStackTrace();
092: }
093: super .init(config);
094: }
095:
096: public void service(HttpServletRequest request,
097: HttpServletResponse response) {
098: String code = (String) request.getSession().getAttribute(
099: "YazdCode");
100:
101: sendImage(response, code);
102:
103: }
104:
105: private void sendImage(HttpServletResponse response, String code) {
106: try {
107:
108: // prepare some output
109: buffer = new BufferedImage(90, 50,
110: BufferedImage.TYPE_INT_RGB);
111: g2 = buffer.createGraphics();
112: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
113: RenderingHints.VALUE_ANTIALIAS_ON);
114: g2.setFont(font);
115: // actually do the drawing
116: g2.setColor(Color.BLUE);
117: g2.fillRect(0, 0, 90, 50);
118: g2.setColor(Color.WHITE);
119: g2.drawString(code, 3, 35);
120:
121: // set the content type and get the output stream
122: response.setContentType("image/png");
123: OutputStream os = response.getOutputStream();
124:
125: // output the image as png
126: ImageIO.write(buffer, "png", os);
127: os.close();
128:
129: } catch (Exception e) {
130: System.err.println("Exception in Yazd Servlet: "
131: + e.getMessage());
132: e.printStackTrace();
133: }
134: }
135: }
|