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

package org.subethamail.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tagonist.AbstractAction;

/**
 * Adds some handy methods.
 *
 * @author Jeff Schnitzer
 */
abstract public class SubEthaAction extends AbstractAction
{
	/** */
	private final static Logger log = LoggerFactory.getLogger(SubEthaAction.class);

	/**
	 * @return the action param specified by the key, as a String
	 */
	protected String getActionParam(String key)
	{
		Object value = this.getCtx().getActionParams().get(key);
		if (value == null)
			return null;
		else
			return value.toString();
	}

	/**
	 * @return null if cookie is not present
	 */
	protected Cookie getCookie(String name)
	{
		Cookie[] cookies;
		try {
			cookies = this.getCtx().getRequest().getCookies();
		} catch (NullPointerException ex) {
			log.error("Weird error from within Resin", ex);
			return null;
		}

		if (cookies != null)
		{
			for (Cookie cookie : cookies)
			{
				if (cookie.getName().equals(name))
					return cookie;
			}
		}

		return null;
	}

	/**
	 * @return null if cookie is not present
	 */
	protected String getCookieValue(String name)
	{
		Cookie cook = this.getCookie(name);
		if (cook == null)
			return null;
		else
			return cook.getValue();
	}

	/**
	 */
	protected void setCookie(String name, String value, int maxAge)
	{
		Cookie cook = new Cookie(name, value);
		cook.setMaxAge(maxAge);

		this.getCtx().getResponse().addCookie(cook);
	}

	/**
	 * @return the request URI without the context path.
	 */
	protected String getContextlessRequestURI()
	{
		String contextPath = this.getCtx().getRequest().getContextPath();

		// Irritatingly, if we have an error the getRequestURI() method
		// returns the URI of the error page.  The original page is saved
		// as an attribute in the request.
		String requestURI = (String)this.getCtx().getRequest().getAttribute("javax.servlet.error.request_uri");
		if (requestURI == null)
			requestURI = this.getCtx().getRequest().getRequestURI();

		if (log.isDebugEnabled())
			log.debug("Getting contextless request URI.  contextPath is " + contextPath + ", requestURI is " + requestURI);

		return requestURI.substring(contextPath.length());
	}

	/**
	 * @return the contextless URI plus the query string.  Works even
	 *  if an http POST was submitted.  This is what you can safely
	 *  use <c:redirect> with.
	 */
	public String getUsefulRequestURI()
	{
		if ("POST".equals(this.getCtx().getRequest().getMethod().toUpperCase()))
		{
			Map<String, String[]> params = this.getCtx().getRequest().getParameterMap();

			if (params.isEmpty())
			{
				return this.getContextlessRequestURI();
			}
			else
			{
				// We have to build the query string from the parameters by hand
				StringBuffer queryBuf = new StringBuffer(1024);
				queryBuf.append(this.getContextlessRequestURI());

				boolean afterFirst = false;
				for (Map.Entry<String, String[]> entry: params.entrySet())
				{
					for (String value: entry.getValue())
					{
						if (afterFirst)
						{
							queryBuf.append('&');
						}
						else
						{
							queryBuf.append('?');
							afterFirst = true;
						}

						try
						{
							queryBuf.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
							queryBuf.append('=');
							queryBuf.append(URLEncoder.encode(value, "UTF-8"));
						}
						catch (UnsupportedEncodingException ex)
						{
							// Should be impossible
							throw new RuntimeException(ex);
						}
					}
				}

				return queryBuf.toString();
			}
		}
		else
		{
			String query = this.getCtx().getRequest().getQueryString();
			if (query == null)
				return this.getContextlessRequestURI();
			else
				return this.getContextlessRequestURI() + "?" + query;
		}
	}
}

@@ -159,16 +159,49 @@
 il.Map;%0D
+%0Aimport java.util.logging.Level;%0D
 %0A%0D%0Aimpor
@@ -243,60 +243,35 @@
 ort 
-org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
+lombok.extern.java.Log
 ;%0D%0A
+%0D%0A
 impo
@@ -375,16 +375,22 @@
 r%0D%0A */%0D%0A
+@Log%0D%0A
 abstract
@@ -445,104 +445,11 @@
 on%0D%0A
+
 %7B%0D%0A
-%09/** */%0D%0A%09private final static Logger log = LoggerFactory.getLogger(SubEthaAction.class);%0D%0A%0D%0A
 %09/**
@@ -948,14 +948,25 @@
 log.
-error(
+log(Level.SEVERE,
 %22Wei
@@ -2196,38 +2196,53 @@
 g.is
-DebugEnabled())%0D%0A%09%09%09log.debug(
+Loggable(Level.FINE))%0D%0A%09%09%09log.log(Level.FINE,
 %22Get
@@ -2291,47 +2291,58 @@
  is 
-%22 + contextPath + %22, requestURI is %22 + 
+%7B0%7D, requestURI is %7B1%7D%22, new Object%5B%5D%7BcontextPath,
 requ
@@ -2347,16 +2347,17 @@
 questURI
+%7D
 );%0D%0A%0D%0A%09%09

switch logging framework