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

package org.subethamail.web.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.subethamail.core.acct.i.AccountMgr;
import org.subethamail.core.injector.i.Injector;
import org.subethamail.web.security.SubEthaLogin;

/**
 * Servlet allows calling the injector with a very simple HTTP POST
 * rather than going through the SOAP stack.
 * 
 * Parameters expected:
 * 
 * authEmail: the email address of a site administrator
 * authPassword: the password for the site administrator account
 * from: the email address of the envelope sender
 * recipient: the email address of the envelope recipient
 * message: the rfc822 content of the message
 * 
 * The result will be 200 OK if accepted, 500 if an error occurred.
 * A 599 will be returned if the recipient is unknown.
 * 
 * TODO:  make this a lot more efficient by using the content body
 * as the raw message bytes instead of requiring www-form-urlencoded 
 */
public class InjectorServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	@Inject AccountMgr accMgr;
	@Inject Injector inj;
	
	@Inject SubEthaLogin resinLogin;
	
	/** */
	public static final String AUTH_EMAIL_PARAM = "authEmail";
	public static final String AUTH_PASS_PARAM = "authPassword";
	public static final String FROM_PARAM = "from";
	public static final String RECIPIENT_PARAM = "recipient";
	public static final String MESSAGE_PARAM = "message";
	
	/**
	 * Status code we return when we don't know what to do with the recipient.
	 */
	public static final int SC_ADDRESS_UNKNOWN = 599;

	/* (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
	{
		String authEmail = request.getParameter(AUTH_EMAIL_PARAM);
		String authPass = request.getParameter(AUTH_PASS_PARAM);
		String from = request.getParameter(FROM_PARAM);
		String recipient = request.getParameter(RECIPIENT_PARAM);
		String message = request.getParameter(MESSAGE_PARAM);
		
		if (authEmail == null || authPass == null || from == null || recipient == null || message == null)
			throw new ServletException("Missing parameter");
		
		try
		{
			if (!this.resinLogin.login(authEmail, authPass, request))
				throw new ServletException("Bad email or password");
			
			if (!inj.inject(from, recipient, new ByteArrayInputStream(message.getBytes())))
				response.sendError(SC_ADDRESS_UNKNOWN, "Recipient address unknown");
		}
		finally
		{
			this.resinLogin.logout(request);
		}
	}
}

@@ -129,16 +129,49 @@
 eption;%0D
+%0Aimport java.util.logging.Level;%0D
 %0A%0D%0Aimpor
@@ -367,24 +367,58 @@
 esponse;%0D%0A%0D%0A
+import lombok.extern.java.Log;%0D%0A%0D%0A
 import org.s
@@ -1270,16 +1270,22 @@
  %0D%0A */%0D%0A
+@Log%0D%0A
 public c
@@ -2436,32 +2436,32 @@
 IPIENT_PARAM);%0D%0A
-
 %09%09String message
@@ -2505,16 +2505,181 @@
 );%0D%0A%09%09%0D%0A
+%09%09log.log(Level.FINE,%22service authEmail=%7B0%7D, authPass=%7B1%7D, from=%7B2%7D, recipient=%7B3%7D, message=%7B4%7D%22, new Object%5B%5D%7BauthEmail, authPass, from, recipient, message%7D);%0D%0A%09%09%0D%0A
 %09%09if (au

debug logging