Coverage Report - org.kuali.rice.kew.mail.service.impl.DefaultEmailService
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEmailService
0%
0/38
0%
0/16
2.857
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.mail.service.impl;
 18  
 
 19  
 import java.util.Properties;
 20  
 
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.core.config.ConfigContext;
 23  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 24  
 import org.kuali.rice.kew.mail.EmailBody;
 25  
 import org.kuali.rice.kew.mail.EmailFrom;
 26  
 import org.kuali.rice.kew.mail.EmailSubject;
 27  
 import org.kuali.rice.kew.mail.EmailTo;
 28  
 import org.kuali.rice.kew.mail.Mailer;
 29  
 import org.kuali.rice.kew.mail.service.EmailService;
 30  
 import org.springframework.beans.factory.InitializingBean;
 31  
 
 32  
 
 33  
 
 34  
 import org.kuali.rice.kew.mail.service.impl.EmailBcList;
 35  
 import org.kuali.rice.kew.mail.service.impl.EmailCcList;
 36  
 import org.kuali.rice.kew.mail.service.impl.EmailToList;
 37  
 
 38  
 
 39  0
 public class DefaultEmailService implements EmailService, InitializingBean {
 40  0
     private static final Logger LOG = Logger.getLogger(DefaultEmailService.class);
 41  
 
 42  
         public static final String USERNAME_PROPERTY = "mail.smtp.username";
 43  
         public static final String PASSWORD_PROPERTY = "mail.smtp.password";
 44  
         
 45  
         protected Mailer mailer;
 46  
         
 47  
         /**
 48  
          * This method is called by Spring on initialization.  
 49  
          */
 50  
         public void afterPropertiesSet() throws Exception {
 51  0
                 if(getMailer() == null){
 52  0
                         mailer = createMailer();
 53  
                 }
 54  0
         }
 55  
 
 56  
         /**
 57  
          * Retrieves the Properties used to configure the Mailer.  The property names configured in the 
 58  
          * Workflow configuration should match those of Java Mail.
 59  
          * @return
 60  
          */
 61  
         private Properties getConfigProperties() {
 62  0
                 return ConfigContext.getCurrentContextConfig().getProperties();
 63  
         }
 64  
         
 65  
         public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) {
 66  0
         if (to.getToAddress() == null) {
 67  0
             LOG.warn("No To address specified; refraining from sending mail");
 68  0
             return;
 69  
         }
 70  
                 try {
 71  0
                         getMailer().sendMessage(
 72  
                         from.getFromAddress(),
 73  
                         to.getToAddress(),
 74  
                         subject.getSubject(),
 75  
                         body.getBody(),
 76  
                         htmlMessage);
 77  0
                 } catch (Exception e) {
 78  0
                         throw new WorkflowRuntimeException(e);
 79  0
                 }
 80  0
         }
 81  
 
 82  
 
 83  
         public void sendEmail(EmailFrom from, EmailToList to, EmailSubject subject, EmailBody body, EmailCcList cc, EmailBcList bc, boolean htmlMessage) {
 84  0
                 if(getMailer() == null){
 85  
                         try {
 86  0
                                 mailer = createMailer();
 87  0
                         } catch (Exception e) {
 88  0
                                 LOG.error("Error initializing mailer for multi-recipient email.", e);
 89  0
                         }
 90  
                 }
 91  0
                 if (to.getToAddresses().isEmpty()) {
 92  0
                         LOG.error("List of To addresses must contain at least one entry.");
 93  
                 } else {
 94  
                         try {
 95  0
                                 getMailer().sendMessage(
 96  
                                                 from.getFromAddress(), 
 97  
                                                 to.getToAddressesAsAddressArray(), 
 98  
                                                 subject.getSubject(), 
 99  
                                                 body.getBody(), 
 100  
                                                 (cc == null ? null : cc.getToAddressesAsAddressArray()), 
 101  
                                                 (bc == null ? null : bc.getToAddressesAsAddressArray()), 
 102  
                                                 htmlMessage);
 103  0
                         } catch (Exception e) {
 104  0
                                 LOG.error("Error sending email to multiple recipients.", e);
 105  0
                         }
 106  
                 }
 107  0
         }
 108  
 
 109  
         /**
 110  
          * @return the mailer
 111  
          */
 112  
         public Mailer getMailer() {
 113  0
                 return mailer;
 114  
         }
 115  
 
 116  
         /**
 117  
          * @param mailer the mailer to set
 118  
          * 
 119  
          * Note: If you want to test locally and not have a smtp server, you can, via spring,
 120  
          * inject the MockMailer. That class just prints the email via Log.info
 121  
          */
 122  
         public void setMailer(Mailer mailer) {
 123  0
                 this.mailer = mailer;
 124  0
         }
 125  
         
 126  
         protected Mailer createMailer(){
 127  0
                 Mailer cMailer = null;
 128  0
                 String username = ConfigContext.getCurrentContextConfig().getProperty(USERNAME_PROPERTY);
 129  0
                 String password = ConfigContext.getCurrentContextConfig().getProperty(PASSWORD_PROPERTY);
 130  0
                 if (username != null && password != null) {
 131  0
                         cMailer = new Mailer(getConfigProperties(), username, password);
 132  0
                         LOG.info("Rice Mailer being used. Username and Pass were found");
 133  
                 } else {
 134  0
                         cMailer = new Mailer(getConfigProperties());
 135  0
                         LOG.info("Rice Mailer being used. Username and Pass were not found");
 136  
                 }
 137  0
                 return cMailer;
 138  
         }
 139  
 
 140  
 }