package org.jboss.net.axis.transport.mailto.client;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.addressing.AddressingHeaders;
import org.apache.axis.message.addressing.Constants;
import org.apache.axis.message.addressing.EndpointReference;
import org.apache.axis.message.addressing.RelatesTo;
import org.apache.log4j.Logger;
import org.jboss.net.axis.transport.mailto.MailConstants;
import org.jboss.net.axis.transport.mailto.MailMessage;
import org.jboss.net.axis.transport.mailto.SOAPDataSource;
public class BaseMailSender extends BasicHandler implements MailConstants
{
public static final String MAIL_PROPS = "MailTransportClient.properties";
public static final String TRANS_PROPS = "transport.properties";
public static final String SESSION_NAME = "SessionName";
protected Logger log = Logger.getLogger(getClass());
static protected Properties mailProps = null;
static protected String mailSessionName = null;
public void invoke(MessageContext ctx) throws AxisFault
{
if (mailSessionName == null && mailProps == null)
{
if ((mailSessionName = (String) getOption(SESSION_NAME)) == null)
{
mailProps = getJavaMailProperties();
}
}
String msgID = sendMail(ctx);
archiveMessage(msgID, ctx);
checkResponse(ctx);
}
protected String sendMail(MessageContext ctx) throws AxisFault
{
MailMessage mailMsg;
org.apache.axis.Message soapMsg = ctx.getRequestMessage();
Session session = getMailSession();
String msgID = null;
AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS);
try
{
mailMsg = new MailMessage(session);
mailMsg.setRecipients(Message.RecipientType.TO, new Address[]{new InternetAddress(headers.getTo().getPath())});
try
{
EndpointReference from = headers.getFrom();
if (from != null && !Constants.NS_URI_ANONYMOUS.equals(from.getAddress().toString()))
{
mailMsg.setFrom(new InternetAddress(from.getAddress().getPath()));
}
else
{
mailMsg.setFrom(InternetAddress.getLocalAddress(session));
}
}
catch (AddressException ae)
{
mailMsg.setFrom(InternetAddress.getLocalAddress(session));
}
if (headers.getMessageID() != null)
{
mailMsg.setMessageID(headers.getMessageID().toString());
}
if (headers.getRelatesTo() != null)
{
List relatesTo = headers.getRelatesTo();
for (Iterator iter = relatesTo.iterator(); iter.hasNext();)
{
RelatesTo rt = (RelatesTo) iter.next();
if (Constants.QNAME_RESPONSE.equals(rt.getType()))
{
mailMsg.setHeader(HEADER_IN_REPLY_TO, rt.getURI().toString());
}
}
}
mailMsg.setDataHandler(new DataHandler(new SOAPDataSource(soapMsg)));
Transport.send(mailMsg);
msgID = mailMsg.getMessageID();
} catch (MessagingException e)
{
log.fatal("There was a problem creating the request email message", e);
throw new AxisFault("Unable to create request message");
}
if (log.isDebugEnabled())
log.debug("message-id: " + msgID);
return msgID;
}
protected void archiveMessage(String msgID, MessageContext msgCtx)
{
}
protected void checkResponse(MessageContext ctx) throws AxisFault
{
}
protected Properties getJavaMailProperties() throws AxisFault
{
Properties props = null;
Hashtable opts = getOptions();
if (opts != null)
{
for (Iterator iter = opts.keySet().iterator(); iter.hasNext();)
{
String key = (String) iter.next();
if (key != null && key.startsWith("mail."))
{
if (props == null)
props = new Properties();
props.setProperty(key, (String) opts.get(key));
}
}
}
if (props == null)
{
String propfileName = (String) getOption(TRANS_PROPS);
propfileName = propfileName == null ? MAIL_PROPS : propfileName;
InputStream is = getClass().getClassLoader().getResourceAsStream(propfileName);
if (is == null)
throw new AxisFault(propfileName + " not found.");
props = new Properties();
try
{
props.load(is);
} catch (IOException e)
{
throw new AxisFault("Unable to load mail properties.", e);
}
}
return props;
}
protected Session getMailSession() throws AxisFault
{
if (log.isDebugEnabled())
log.debug("Entering: getMailSession()");
Context ctx = null;
Session mail = null;
if (mailSessionName == null)
{
mail = Session.getInstance(mailProps);
} else
{
try
{
ctx = new InitialContext();
mail = (Session) ctx.lookup(mailSessionName);
} catch (NamingException ne)
{
log.fatal("NamingException: getMailSession()\n", ne);
throw new AxisFault("Unable to find Email Session.");
} finally
{
if (ctx != null)
try
{
ctx.close();
} catch (NamingException ne)
{
}
}
}
if (log.isDebugEnabled())
log.debug("Leaving: getMailSession()");
return mail;
}
}