/*
Written by Aaron Porter on 12/4/1998
This class is designed to make it easy to create images
from servlets. Feel free to modify this class to suit
your own needs. Please let me know if you find it useful!
aaron_porter@hotmail.com
*/
package com.mongus.servlet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.mongus.awt.image.*;
import com.mongus.util.*;
import Acme.JPM.Encoders.GifEncoder;
abstract public class GifServlet extends HttpServlet
{
protected Frame frame;
protected Color transparentColor;
protected Cache cache;
public void init(ServletConfig servletConfig) throws ServletException
{
super.init(servletConfig);
frame = new Frame();
frame.addNotify();
long cacheLifespan = getCacheLifespan();
if (cacheLifespan > 0)
cache = new Cache(cacheLifespan);
}
private Image createImage(int width, int height)
{
// Ask the component for an image
return frame.createImage(width, height);
}
private void writeGif(HttpServletRequest request, HttpServletResponse response, Image image) throws IOException
{
OutputStream out = null;
if (cache == null)
// Get the binary stream headed for the browser
out = response.getOutputStream();
else
// Save the resulting byte array
out = new ByteArrayOutputStream();
// Set the content-type header
response.setContentType("image/gif");
// Encode the image as a GIF (Thanks to Jef Poskanzer! www.acme.com)
GifEncoder encoder;
if (transparentColor != null)
encoder = new GifEncoder(new FilteredImageSource(image.getSource(), new TransparentFilter(transparentColor)), out);
else
encoder = new GifEncoder(image, out);
encoder.encode();
if (cache != null)
{
byte[] gif = ((ByteArrayOutputStream) out).toByteArray();
out = response.getOutputStream();
cache.put(getQueryString(request), gif);
out.write(gif, 0, gif.length);
}
// Send the GIF to the browser
out.flush();
}
protected void setTransparentColor(Color transparentColor)
{
this.transparentColor = transparentColor;
}
protected Color getTransparentColor()
{
return transparentColor;
}
protected long getLastModified(HttpServletRequest request)
{
if (cache != null)
{
long createDate = cache.getCreateDate(getQueryString(request));
if (createDate != 0)
return createDate;
}
return -1;
}
private String getQueryString(HttpServletRequest request)
{
String queryString = request.getQueryString();
if (queryString == null)
queryString = "(null queryString)";
return queryString;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
if (cache != null)
{
byte[] gif = (byte[]) cache.get(getQueryString(request));
if (gif != null)
{
// Set the content-type header
response.setContentType("image/gif");
response.setContentLength(gif.length);
OutputStream out = response.getOutputStream();
out.write(gif, 0, gif.length);
out.flush();
return;
}
}
// Ask the subclass what size of image to create
Dimension imageSize = getImageSize(request);
Image image = createImage(imageSize.width, imageSize.height);
// Get a graphics object to draw on the image
paint(image.getGraphics(), request);
// Send the GIF to the browser remapping all magenta pixels to transparent
writeGif(request, response, image);
}
catch (Exception e)
{
e.printStackTrace();
response.setStatus(500);
response.setContentType("text/html");
try
{
PrintWriter writer = response.getWriter();
writer.println("An exception was encountered!
");
e.printStackTrace(writer);
writer.println("");
writer.flush();
}
catch (IOException ignore)
{
}
log(e.toString());
}
}
protected long getCacheLifespan()
{
return 0;
}
abstract protected void paint(Graphics graphics, HttpServletRequest request);
abstract protected Dimension getImageSize(HttpServletRequest request);
}