001/* 002 * #%L 003 * Netarchivesuite - common 004 * %% 005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.common.utils; 025 026import java.util.Date; 027 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Delivers NetarchiveSuite notifications by email. 033 */ 034public class EMailNotifications extends Notifications { 035 036 /** The error logger we notify about error messages on. */ 037 private static final Logger log = LoggerFactory.getLogger(EMailNotifications.class); 038 039 /** The default place in classpath where the settings file can be found. */ 040 private static String DEFAULT_SETTINGS_CLASSPATH = "dk/netarkivet/common/utils/EMailNotificationsSettings.xml"; 041 042 /* 043 * The static initialiser is called when the class is loaded. It will add default values for all settings defined in 044 * this class, by loading them from a settings.xml file in classpath. 045 */ 046 static { 047 Settings.addDefaultClasspathSettings(DEFAULT_SETTINGS_CLASSPATH); 048 } 049 050 /** 051 * <b>settings.common.notifications.receiver</b>: <br> 052 * The setting for the receiver of email notifications. 053 */ 054 public static String MAIL_RECEIVER_SETTING = "settings.common.notifications.receiver"; 055 056 /** 057 * <b>settings.common.notifications.sender</b>: <br> 058 * The setting for the sender of email notifications (and receiver of bounces). 059 */ 060 public static String MAIL_SENDER_SETTING = "settings.common.notifications.sender"; 061 062 /** 063 * <b>settings.common.notifications.subjectPrefix</b>: <br> 064 * The subject prefix for the mail-notifications. 065 */ 066 public static String MAIL_SUBJECTPREFIX_SETTING = "settings.common.notifications.subjectPrefix"; 067 068 /** The email receiver of the errors. */ 069 private static final String MAIL_RECEIVER = Settings.get(MAIL_RECEIVER_SETTING); 070 /** The email sender of the errors. */ 071 private static final String MAIL_SENDER = Settings.get(MAIL_SENDER_SETTING); 072 073 /** Subject prefix for notifications by mail. */ 074 private static String SUBJECT_PREFIX = Settings.get(MAIL_SUBJECTPREFIX_SETTING); 075 076 /** 077 * Sends a notification including an exception. 078 * 079 * @param message The message to notify about. 080 * @param eventType The type of notification 081 * @param e The exception to notify about. 082 */ 083 public void notify(String message, NotificationType eventType, Throwable e) { 084 if (message == null) { 085 message = ""; 086 } 087 sendMailNotifications(message, eventType, e); 088 } 089 090 /** 091 * Send mailNotications. 092 * 093 * @param message The message body itself 094 * @param eventType Type of notification 095 * @param e An exception (can be null) 096 */ 097 private void sendMailNotifications(String message, NotificationType eventType, Throwable e) { 098 String subjectPrefix = SUBJECT_PREFIX + "-" + eventType + ": "; 099 100 // Subject is a specified string + first line of error message 101 String subject = subjectPrefix + message.split("\n")[0]; 102 103 // Body consists of four parts. 104 StringBuffer body = new StringBuffer(); 105 106 // 1: The host of the message 107 body.append("Host: " + SystemUtils.getLocalHostName() + "\n"); 108 body.append("Date: " + new Date().toString() + "\n"); 109 110 // 2: The origin of the message, found by inspecting stack trace 111 for (StackTraceElement elm : Thread.currentThread().getStackTrace()) { 112 if (!elm.toString().startsWith(getClass().getName()) 113 && !elm.toString().startsWith(Notifications.class.getName()) 114 && !elm.toString().startsWith(Thread.class.getName())) { 115 body.append(elm.toString() + "\n"); 116 break; 117 } 118 } 119 120 // 3: The given message 121 body.append(message + "\n"); 122 123 // 4: Optionally the exception 124 if (e != null) { 125 body.append(ExceptionUtils.getStackTrace(e)); 126 } 127 128 try { 129 // Send the mail 130 EMailUtils.sendEmail(MAIL_RECEIVER, MAIL_SENDER, subject, body.toString()); 131 132 // Log as error 133 134 log.error("Mailing {}{}", subjectPrefix, message, e); 135 } catch (Exception e1) { 136 // On trouble: Log and print it to system out, it's the best we can 137 // do! 138 139 String msg = "Could not send email on " + eventType.toString().toLowerCase() + " notification:\n" 140 + body.toString() + "\n"; 141 System.err.println(msg); 142 e1.printStackTrace(System.err); 143 log.error(msg, e1); 144 } 145 } 146 147}