Coverage Report - org.mule.providers.email.SmtpMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
SmtpMessageDispatcher
0%
0/46
0%
0/7
2.222
 
 1  
 /*
 2  
  * $Id: SmtpMessageDispatcher.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.email;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.providers.AbstractMessageDispatcher;
 15  
 import org.mule.umo.UMOEvent;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.endpoint.EndpointException;
 18  
 import org.mule.umo.endpoint.UMOEndpointURI;
 19  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 20  
 import org.mule.umo.provider.DispatchException;
 21  
 
 22  
 import java.util.Calendar;
 23  
 
 24  
 import javax.mail.Message;
 25  
 import javax.mail.MessagingException;
 26  
 import javax.mail.Transport;
 27  
 
 28  
 /**
 29  
  * <code>SmtpMessageDispatcher</code> will dispatch Mule events as Mime email
 30  
  * messages over an SMTP gateway.
 31  
  * 
 32  
  * This contains a reference to a transport (and endpoint and connector, via superclasses)
 33  
  */
 34  
 public class SmtpMessageDispatcher extends AbstractMessageDispatcher
 35  
 {
 36  
     private volatile Transport transport;
 37  
 
 38  
     public SmtpMessageDispatcher(UMOImmutableEndpoint endpoint)
 39  
     {
 40  0
         super(endpoint);
 41  0
     }
 42  
 
 43  
     private SmtpConnector castConnector()
 44  
     {
 45  0
         return (SmtpConnector) getConnector();
 46  
     }
 47  
 
 48  
     protected void doConnect() throws Exception
 49  
     {
 50  0
         if (transport == null)
 51  
         {
 52  
             try
 53  
             {
 54  0
                 transport = castConnector().getSessionDetails(endpoint).newTransport();
 55  0
                 UMOEndpointURI uri = endpoint.getEndpointURI();
 56  0
                 transport.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword());
 57  
             }
 58  0
             catch (Exception e)
 59  
             {
 60  0
                 throw new EndpointException(
 61  
                     org.mule.config.i18n.MessageFactory.createStaticMessage("Unable to connect to mail transport."),
 62  
                     e);
 63  0
             }
 64  
         }
 65  0
     }
 66  
 
 67  
     protected void doDisconnect() throws Exception
 68  
     {
 69  0
         if (null != transport)
 70  
         {
 71  
             try
 72  
             {
 73  0
                 transport.close();
 74  
             }
 75  
             finally
 76  
             {
 77  0
                 transport = null;
 78  0
             }
 79  
         }
 80  0
     }
 81  
 
 82  
     protected void doDispatch(UMOEvent event)
 83  
     {
 84  
         try
 85  
         {
 86  0
             Object data = event.getTransformedMessage();
 87  
 
 88  0
             if (!(data instanceof Message))
 89  
             {
 90  0
                 throw new DispatchException(
 91  0
                     CoreMessages.transformUnexpectedType(data.getClass(), Message.class),
 92  
                     event.getMessage(), event.getEndpoint());
 93  
             }
 94  
             else
 95  
             {
 96  
                 // Check the message for any unset data and use defaults
 97  0
                 sendMailMessage((Message) data);
 98  
             }
 99  
         }
 100  0
         catch (Exception e)
 101  
         {
 102  0
             connector.handleException(e);
 103  0
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Make a specific request to the underlying transport
 108  
      * 
 109  
      * @param timeout the maximum time the operation should block before returning.
 110  
      *            The call should return immediately if there is data available. If
 111  
      *            no data becomes available before the timeout elapses, null will be
 112  
      *            returned
 113  
      * @return the result of the request wrapped in a UMOMessage object. Null will be
 114  
      *         returned if no data was avaialable
 115  
      * @throws Exception if the call to the underlying protocal cuases an exception
 116  
      */
 117  
     protected UMOMessage doReceive(long timeout) throws Exception
 118  
     {
 119  0
         throw new UnsupportedOperationException("doReceive");
 120  
     }
 121  
 
 122  
     protected UMOMessage doSend(UMOEvent event) throws Exception
 123  
     {
 124  0
         doDispatch(event);
 125  0
         return null;
 126  
     }
 127  
 
 128  
     protected void sendMailMessage(Message message) throws MessagingException
 129  
     {
 130  
         // sent date
 131  0
         message.setSentDate(Calendar.getInstance().getTime());
 132  
 
 133  
         /*
 134  
          * Double check that the transport is still connected as some SMTP servers may 
 135  
          * disconnect idle connections.
 136  
          */
 137  0
         if (!transport.isConnected())
 138  
         {
 139  0
             UMOEndpointURI uri = endpoint.getEndpointURI();
 140  0
             if (logger.isInfoEnabled())
 141  
             {
 142  0
                 logger.info("Connection closed by remote server. Reconnecting.");
 143  
             }
 144  0
             transport.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword());
 145  
         }
 146  
 
 147  0
         transport.sendMessage(message, message.getAllRecipients());
 148  
 
 149  0
         if (logger.isDebugEnabled())
 150  
         {
 151  0
             StringBuffer msg = new StringBuffer();
 152  0
             msg.append("Email message sent with subject'").append(message.getSubject()).append("' sent- ");
 153  0
             msg.append(", From: ").append(MailUtils.mailAddressesToString(message.getFrom())).append(" ");
 154  0
             msg.append(", To: ").append(
 155  
                 MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))).append(" ");
 156  0
             msg.append(", Cc: ").append(
 157  
                 MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))).append(" ");
 158  0
             msg.append(", Bcc: ")
 159  
             .append(MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC)))
 160  
             .append(" ");
 161  0
             msg.append(", ReplyTo: ").append(MailUtils.mailAddressesToString(message.getReplyTo()));
 162  
 
 163  0
             logger.debug(msg.toString());
 164  
         }
 165  
 
 166  0
     }
 167  
 
 168  
     protected void doDispose()
 169  
     {
 170  
         // nothing doing
 171  0
     }
 172  
 
 173  
 }