001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.service.impl; 017 018import javax.mail.MessagingException; 019 020import org.kuali.rice.core.api.CoreApiServiceLocator; 021import org.kuali.rice.core.api.CoreConstants; 022import org.kuali.rice.core.api.mail.MailMessage; 023import org.kuali.rice.core.api.mail.Mailer; 024import org.kuali.rice.krad.exception.InvalidAddressException; 025import org.kuali.rice.krad.service.MailService; 026import org.kuali.rice.krad.util.KRADConstants; 027 028/** 029 * Default implementation of mail service 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 */ 033public class MailServiceImpl implements MailService { 034 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailServiceImpl.class); 035 036 private String batchMailingList; 037 private Mailer mailer; 038 039 private String nonProductionNotificationMailingList; 040 private boolean realNotificationsEnabled = true; 041 042 /** 043 * The injected Mailer. 044 */ 045 public void setMailer(Mailer mailer) { 046 this.mailer = mailer; 047 } 048 049 /** 050 * 051 */ 052 public MailServiceImpl() { 053 super(); 054 } 055 056 /** 057 * Sets the batchMailingList attribute value. 058 * @param batchMailingList The batchMailingList to set. 059 */ 060 public void setBatchMailingList(String batchMailingList) { 061 this.batchMailingList = batchMailingList; 062 } 063 064 /** 065 * @see org.kuali.rice.krad.service.MailService#getBatchMailingList() 066 */ 067 public String getBatchMailingList() { 068 return batchMailingList; 069 } 070 071 /** 072 * This overridden method ... 073 * @throws MessagingException 074 * 075 * @see org.kuali.rice.krad.service.MailService#sendMessage(org.kuali.rice.core.api.mail.MailMessage) 076 */ 077 @Override 078 public void sendMessage(MailMessage message) throws InvalidAddressException, MessagingException { 079 mailer.sendEmail(composeMessage(message)); 080 } 081 082 protected MailMessage composeMessage(MailMessage message){ 083 084 MailMessage mm = new MailMessage(); 085 086 // If realNotificationsEnabled is false, mails will be sent to nonProductionNotificationMailingList 087 if(!isRealNotificationsEnabled()){ 088 getNonProductionMessage(message); 089 } 090 091 String app = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(CoreConstants.Config.APPLICATION_ID); 092 String env = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY); 093 094 mm.setToAddresses(message.getToAddresses()); 095 mm.setBccAddresses(message.getBccAddresses()); 096 mm.setCcAddresses(message.getCcAddresses()); 097 mm.setSubject(app + " " + env + ": " + message.getSubject()); 098 mm.setMessage(message.getMessage()); 099 mm.setFromAddress(message.getFromAddress()); 100 return mm; 101 } 102 103 public String getNonProductionNotificationMailingList() { 104 return this.nonProductionNotificationMailingList; 105 } 106 107 /** 108 * @param nonProductionNotificationMailingList the nonProductionNotificationMailingList to set 109 */ 110 public void setNonProductionNotificationMailingList( 111 String nonProductionNotificationMailingList) { 112 this.nonProductionNotificationMailingList = nonProductionNotificationMailingList; 113 } 114 115 /** 116 * @return the realNotificationsEnabled 117 */ 118 public boolean isRealNotificationsEnabled() { 119 return this.realNotificationsEnabled; 120 } 121 122 /** 123 * @param realNotificationsEnabled the realNotificationsEnabled to set 124 */ 125 public void setRealNotificationsEnabled(boolean realNotificationsEnabled) { 126 this.realNotificationsEnabled = realNotificationsEnabled; 127 } 128 129 protected MailMessage getNonProductionMessage(MailMessage message){ 130 StringBuilder buf = new StringBuilder(); 131 buf.append("Email To: ").append(message.getToAddresses()).append("\n"); 132 buf.append("Email CC: ").append(message.getCcAddresses()).append("\n"); 133 buf.append("Email BCC: ").append(message.getBccAddresses()).append("\n\n"); 134 buf.append(message.getMessage()); 135 136 message.getToAddresses().clear(); 137 //Note: If the non production notification mailing list is blank, sending this message will throw an exception 138 message.addToAddress(getNonProductionNotificationMailingList()); 139 message.getBccAddresses().clear(); 140 message.getCcAddresses().clear(); 141 message.setMessage(buf.toString()); 142 143 return message; 144 } 145}