Coverage Report - org.mule.transport.email.SmtpMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
SmtpMessageDispatcher
0%
0/57
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: SmtpMessageDispatcher.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.email;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleEvent;
 15  
 import org.mule.api.MuleMessage;
 16  
 import org.mule.api.endpoint.EndpointException;
 17  
 import org.mule.api.endpoint.EndpointURI;
 18  
 import org.mule.api.endpoint.OutboundEndpoint;
 19  
 import org.mule.api.transport.DispatchException;
 20  
 import org.mule.config.i18n.CoreMessages;
 21  
 import org.mule.config.i18n.MessageFactory;
 22  
 import org.mule.transport.AbstractMessageDispatcher;
 23  
 import org.mule.transport.NullPayload;
 24  
 
 25  
 import com.sun.mail.smtp.SMTPTransport;
 26  
 
 27  
 import java.net.URLDecoder;
 28  
 import java.util.Calendar;
 29  
 
 30  
 import javax.mail.Message;
 31  
 import javax.mail.MessagingException;
 32  
 import javax.mail.Transport;
 33  
 
 34  
 /**
 35  
  * <code>SmtpMessageDispatcher</code> will dispatch Mule events as Mime email
 36  
  * messages over an SMTP gateway.
 37  
  * 
 38  
  * This contains a reference to a transport (and endpoint and connector, via superclasses)
 39  
  */
 40  
 public class SmtpMessageDispatcher extends AbstractMessageDispatcher
 41  
 {
 42  
     private volatile Transport transport;
 43  
 
 44  
     public SmtpMessageDispatcher(OutboundEndpoint endpoint)
 45  
     {
 46  0
         super(endpoint);
 47  0
     }
 48  
 
 49  
     private SmtpConnector castConnector()
 50  
     {
 51  0
         return (SmtpConnector) getConnector();
 52  
     }
 53  
 
 54  
     @Override
 55  
     protected void doConnect() throws Exception
 56  
     {
 57  0
         if (transport == null)
 58  
         {
 59  
             try
 60  
             {
 61  
 
 62  0
                 transport = castConnector().getSessionDetails(endpoint).newTransport();
 63  0
                 EndpointURI uri = endpoint.getEndpointURI();
 64  0
                 String encoding = endpoint.getEncoding();
 65  0
                 String user = (uri.getUser()!=null ? URLDecoder.decode(uri.getUser(), encoding) : null);
 66  0
                 String pass = (uri.getPassword()!=null ? URLDecoder.decode(uri.getPassword(), encoding) : null);
 67  0
                 transport.connect(uri.getHost(), uri.getPort(),  user, pass);
 68  
             }
 69  0
             catch (Exception e)
 70  
             {
 71  0
                 throw new EndpointException(
 72  
                     MessageFactory.createStaticMessage("Unable to connect to mail transport."), e);
 73  0
             }
 74  
         }
 75  0
     }
 76  
 
 77  
     @Override
 78  
     protected void doDisconnect() throws Exception
 79  
     {
 80  0
         if (null != transport)
 81  
         {
 82  
             try
 83  
             {
 84  0
                 transport.close();
 85  
             }
 86  
             finally
 87  
             {
 88  0
                 transport = null;
 89  0
             }
 90  
         }
 91  0
     }
 92  
 
 93  
     @Override
 94  
     protected void doDispatch(MuleEvent event) throws Exception
 95  
     {
 96  0
         Object data = event.getMessage().getPayload();
 97  
 
 98  0
         if (!(data instanceof Message))
 99  
         {
 100  0
             throw new DispatchException(
 101  
                 CoreMessages.transformUnexpectedType(data.getClass(), Message.class),
 102  
                 event, this);
 103  
         }
 104  
         else
 105  
         {
 106  
             // Check the message for any unset data and use defaults
 107  0
             sendMailMessage((Message) data);
 108  
         }
 109  0
     }
 110  
 
 111  
     @Override
 112  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 113  
     {
 114  0
         doDispatch(event);
 115  0
         return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext());
 116  
     }
 117  
 
 118  
     protected void sendMailMessage(Message message) throws MessagingException
 119  
     {
 120  
         // sent date
 121  0
         message.setSentDate(Calendar.getInstance().getTime());
 122  
 
 123  
          // Double check that the transport is still connected as some SMTP servers may 
 124  
          // disconnect idle connections.
 125  0
         if (isTransportConnected() == false)
 126  
         {
 127  0
             EndpointURI uri = endpoint.getEndpointURI();
 128  0
             if (logger.isInfoEnabled())
 129  
             {
 130  0
                 logger.info("Connection closed by remote server. Reconnecting.");
 131  
             }
 132  0
             transport.connect(uri.getHost(), uri.getPort(), uri.getUser(), uri.getPassword());
 133  
         }
 134  
 
 135  0
         transport.sendMessage(message, message.getAllRecipients());
 136  
 
 137  0
         if (logger.isDebugEnabled())
 138  
         {
 139  0
             StringBuffer msg = new StringBuffer();
 140  0
             msg.append("Email message sent with subject'").append(message.getSubject()).append("' sent- ");
 141  0
             msg.append(", From: ").append(MailUtils.mailAddressesToString(message.getFrom())).append(" ");
 142  0
             msg.append(", To: ").append(
 143  
                 MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))).append(" ");
 144  0
             msg.append(", Cc: ").append(
 145  
                 MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))).append(" ");
 146  0
             msg.append(", Bcc: ")
 147  
             .append(MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC)))
 148  
             .append(" ");
 149  0
             msg.append(", ReplyTo: ").append(MailUtils.mailAddressesToString(message.getReplyTo()));
 150  
 
 151  0
             logger.debug(msg.toString());
 152  
         }
 153  
 
 154  0
     }
 155  
     
 156  
     // Fix incompatibility between JavaMail and Exchange 
 157  
     // see http://forums.sun.com/thread.jspa?threadID=5409031&tstart=1
 158  
     protected boolean isTransportConnected()
 159  
     {
 160  0
         boolean isConnected = false;
 161  
         
 162  0
         isConnected = transport.isConnected();
 163  0
         if (isConnected)
 164  
         {
 165  0
             SMTPTransport smtpTransport = (SMTPTransport) transport;
 166  
             
 167  0
             String lastServerResponse = smtpTransport.getLastServerResponse();
 168  0
             if (lastServerResponse.startsWith("250") == false) 
 169  
             {
 170  0
                 isConnected = false;
 171  
                 try
 172  
                 {
 173  0
                     smtpTransport.close();
 174  
                 }
 175  0
                 catch (MessagingException me)
 176  
                 {
 177  0
                     if (logger.isInfoEnabled())
 178  
                     {
 179  0
                         logger.info("Unable to close SMTP Transport", me);
 180  
                     }
 181  0
                 }
 182  
             }
 183  
         }
 184  
         
 185  0
         return isConnected;
 186  
     }
 187  
 
 188  
     @Override
 189  
     protected void doDispose()
 190  
     {
 191  
         // nothing doing
 192  0
     }
 193  
 
 194  
 }