Coverage Report - net.admin4j.util.notify.HtmlEmailNotifier
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlEmailNotifier
41%
7/17
50%
3/6
2
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.util.notify;
 15  
 
 16  
 
 17  
 import javax.servlet.FilterConfig;
 18  
 import javax.servlet.ServletConfig;
 19  
 import javax.servlet.ServletException;
 20  
 
 21  
 import net.admin4j.deps.commons.mail.HtmlEmail;
 22  
 import net.admin4j.util.Admin4jRuntimeException;
 23  
 import net.admin4j.util.HostUtils;
 24  
 
 25  
 /**
 26  
  * Will provide an email notification in HTML format.
 27  
  * <p> See BaseEmailNotifier for initialization parameters.</p>
 28  
  * <p>Note: If the logger for this class is DEBUG, the debug setting will
 29  
  * be passed onto the java mail api.</p>
 30  
  * @author D. Ashmore
 31  
  * @since 1.0
 32  
  * @see BaseEmailNotifier
 33  
  */
 34  
 public class HtmlEmailNotifier extends BaseEmailNotifier {
 35  
         
 36  27
         public HtmlEmailNotifier()  {}
 37  0
         public HtmlEmailNotifier(FilterConfig config) throws ServletException {
 38  0
                 configure(config);
 39  0
         }
 40  
         
 41  0
     public HtmlEmailNotifier(ServletConfig config) throws ServletException {
 42  0
                 configure(config);
 43  0
         }
 44  
         
 45  
     public void notify(String subject, String message) {
 46  
                 try {
 47  
                         
 48  3
                     if (this.isSuppressEmail()) {
 49  0
                         logger.debug("Email Notification surpressed: {}", message);
 50  0
                         return;
 51  
                     }
 52  
                         HtmlEmail email = new HtmlEmail();
 53  
                         email.setHostName(this.getMailServerHost());
 54  
 
 55  3
                         if (message != null && message.indexOf("html") > 0) {
 56  
                             email.setHtmlMsg(message);
 57  
                         }
 58  
                         else email.setHtmlMsg("<html><body>" + message + "</body></html>");
 59  
 
 60  
                 //  Will invoke logging on javamail if true.
 61  
             email.setDebug(logger.isDebugEnabled()); 
 62  
             
 63  
                         email.setSubject(subject);
 64  
                         email.setFrom(this.getFromEmailAddress());
 65  
                         email.addTo(this.getToEmailAddress(), HostUtils.getHostName());
 66  
                         email.send();
 67  3
                 } catch (Exception e) {
 68  3
                         throw new Admin4jRuntimeException(e);
 69  0
                 }
 70  0
         }
 71  
         
 72  
         public boolean supportsHtml() {
 73  3
             return true;
 74  
         }
 75  
     /* (non-Javadoc)
 76  
      * @see net.admin4j.util.notify.Notifier#supportsSMS()
 77  
      */
 78  
     public boolean supportsSMS() {
 79  3
         return false;
 80  
     }
 81  
 
 82  
 }