/*
 * $Id$
 * $URL$
 */

package org.subethamail.web.servlet;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.entity.i.Permission;
import org.subethamail.web.action.GetMessage;

/**
 * Servlet generates and returns a reasonably nice PNG image
 * that describes the permission.  This is needed so that we can
 * display vertical text.  Unfortunately there is not yet any
 * web-standard way of doing so.
 * 
 * There are only a few permissions so we cache the PNG images
 * in memory.
 * 
 * Parameters expected:
 * 
 * perm: the permission name
 */
public class PermissionImageServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/** */
	private final static Logger log = LoggerFactory.getLogger(GetMessage.class);

	/** */
	public static final String PERMISSION_PARAM = "perm";
	
	/** TODO:  figure out the img size dynamically */
	public static final int IMG_WIDTH = 35;
	public static final int IMG_HEIGHT = 140;
	
	/** */
	public static final double TEXT_ANGLE_RADIANS = -Math.PI/2.2;
	
	/**
	 */
	protected Map<Permission, ByteArrayOutputStream> imageCache =
		new ConcurrentHashMap<Permission, ByteArrayOutputStream>();
	
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if (log.isDebugEnabled())
			log.debug("Servicing " + request.getRequestURI());

		String permString = request.getParameter(PERMISSION_PARAM);
		Permission perm = Permission.valueOf(permString);
		
		ByteArrayOutputStream cachedStream = this.imageCache.get(perm);
		if (cachedStream == null)
		{
			if (log.isInfoEnabled())
				log.info("Cache miss; building image for " + perm);
			
			Image img = this.buildImage(perm);

			cachedStream = new ByteArrayOutputStream(2048);	// is 2k enough?

			ImageIO.write((RenderedImage)img, "png", cachedStream);
			
			this.imageCache.put(perm, cachedStream);
		}

		// And send back the results!
		response.setContentType("image/png");
		response.setHeader("Cache-Control", "public, max-age=360000");

		cachedStream.writeTo(response.getOutputStream());
	}
	
	/**
	 * Build the permission image.
	 */
	protected Image buildImage(Permission perm) throws ServletException, IOException
	{
		Graphics2D g = null;

		try
		{
			Image img = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);

			g = (Graphics2D)img.getGraphics();

			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
			g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
			g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

			// TODO:  why doesn't this work?
			// Lay down the background as transparent (or at least try to)
			//g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
			
			// Just white background instead
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
			
			// Now draw text at a diagonal angle from bottom left to top right.
			// TODO:  remove the black magic constants
			g.setColor(Color.BLACK);
			g.translate(IMG_WIDTH-25, IMG_HEIGHT);
			g.rotate(TEXT_ANGLE_RADIANS);
			g.drawString(perm.getPretty(), 0, 0);
			
			return img;
		}
		finally
		{
			if (g != null)
				g.dispose();
		}
	}
}

@@ -387,16 +387,49 @@
 ashMap;%0D
+%0Aimport java.util.logging.Level;%0D
 %0A%0D%0Aimpor
@@ -642,60 +642,35 @@
 ort 
-org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
+lombok.extern.java.Log
 ;%0D%0A
+%0D%0A
 impo
@@ -713,55 +713,8 @@
 on;%0D
-%0Aimport org.subethamail.web.action.GetMessage;%0D
 %0A%0D%0A/
@@ -1096,16 +1096,22 @@
 e%0D%0A */%0D%0A
+@Log%0D%0A
 public c
@@ -1219,98 +1219,8 @@
 %0D%0A%0D%0A
-%09/** */%0D%0A%09private final static Logger log = LoggerFactory.getLogger(GetMessage.class);%0D%0A%0D%0A
 %09/**
@@ -1932,17 +1932,20 @@
 n%0D%0A%09%7B%0D%0A%09
-%09
+    
 if (log.
@@ -1950,38 +1950,59 @@
 g.is
-DebugEnabled())%0D%0A%09%09%09log.debug(
+Loggable(Level.FINE))%0D%0A%09        log.log(Level.FINE,
 %22Ser
@@ -2008,19 +2008,21 @@
 rvicing 
-%22 +
+%7B0%7D%22,
  request
@@ -2262,25 +2262,28 @@
 ll)%0D%0A%09%09%7B%0D%0A%09%09
-%09
+    
 if (log.isIn
@@ -2284,37 +2284,60 @@
 g.is
-InfoEnabled())%0D%0A%09%09%09%09log.info(
+Loggable(Level.INFO))%0D%0A%09%09        log.log(Level.INFO,
 %22Cac
@@ -2368,11 +2368,13 @@
 for 
-%22 +
+%7B0%7D%22,
  per

switch logging framework