Coverage Report - net.admin4j.util.notify.BaseEmailNotifier
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseEmailNotifier
60%
45/74
57%
24/42
3
 
 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  
 import java.util.HashSet;
 17  
 import java.util.Properties;
 18  
 import java.util.Set;
 19  
 import java.util.StringTokenizer;
 20  
 
 21  
 import javax.servlet.FilterConfig;
 22  
 import javax.servlet.ServletConfig;
 23  
 import javax.servlet.ServletException;
 24  
 
 25  
 import net.admin4j.util.Admin4jRuntimeException;
 26  
 import net.admin4j.util.HostUtils;
 27  
 import org.slf4j.Logger;
 28  
 import org.slf4j.LoggerFactory;
 29  
 
 30  
 /**
 31  
  * Base class for email notifiers.
 32  
  * <p>Initialization parameters for this notifier are as follows:</p>
 33  
  * <li>mail.server.host -- Required.  For example 'mail.admin4j.org'</li>
 34  
  * <li>from.email.address -- Required.  For example 'appName@admin4j.org'</li>
 35  
  * <li>to.email.address -- Required.  For example 'App_Support@admin4j.org'</li>
 36  
  * <li>allowed.servers -- Optional.  Comma delimited list of servers that can send email.  
 37  
  * For example 'server1, server2'</li>
 38  
  * @author D. Ashmore
 39  
  * @since 1.0
 40  
  *
 41  
  */
 42  33
 public abstract class BaseEmailNotifier implements Notifier {
 43  
 
 44  
     private String mailServerHost;
 45  
     private String fromEmailAddress;
 46  
     private String toEmailAddress;
 47  33
     private Set<String> allowedServerSet = new HashSet<String>();
 48  33
     protected Logger logger = LoggerFactory.getLogger(HtmlEmailNotifier.class);
 49  33
     private boolean suppressEmail = false;
 50  
 
 51  
     public boolean supportsHtml() {
 52  6
         return false;
 53  
     }
 54  
 
 55  
     public void configure(FilterConfig config) throws ServletException {
 56  21
         this.mailServerHost = config.getInitParameter("mail.server.host");
 57  21
             if (this.mailServerHost ==null)  throw new ServletException("Missing mail.server.host init parameter");
 58  
             
 59  18
             this.fromEmailAddress = config.getInitParameter("from.email.address");
 60  18
             if (this.fromEmailAddress ==null)  throw new ServletException("Missing from.email.address init parameter");
 61  
             
 62  15
             this.toEmailAddress = config.getInitParameter("to.email.address");
 63  15
             if (this.toEmailAddress ==null)  throw new ServletException("Missing to.email.address init parameter");
 64  
             
 65  15
             boolean allowAllServers = true;
 66  15
             String allowedServerStr = config.getInitParameter("allowed.servers");
 67  15
             if (allowedServerStr != null) {
 68  3
                     allowAllServers = false;
 69  3
                     StringTokenizer tokenizer = new StringTokenizer(allowedServerStr, ",");
 70  12
                     while (tokenizer.hasMoreTokens()) {
 71  9
                             this.allowedServerSet.add(HostUtils.deriveServerName(tokenizer.nextToken()));
 72  
                     }
 73  
             }
 74  
             
 75  
             
 76  
             
 77  15
             if (!allowAllServers && !this.allowedServerSet.contains(HostUtils.getHostName())) {
 78  3
                     logger.info("Host not in allowed server set. host=" +HostUtils.getHostName());
 79  3
                     this.suppressEmail = true;
 80  
             }
 81  12
             else logger.info("Emails will be provided by host=" +HostUtils.getHostName());
 82  15
     }
 83  
 
 84  
     public void configure(ServletConfig config) throws ServletException {
 85  12
         this.mailServerHost = config.getInitParameter("mail.server.host");
 86  12
             if (this.mailServerHost ==null)  throw new ServletException("Missing mail.server.host init parameter");
 87  
             
 88  9
             this.fromEmailAddress = config.getInitParameter("from.email.address");
 89  9
             if (this.fromEmailAddress ==null)  throw new ServletException("Missing from.email.address init parameter");
 90  
             
 91  6
             this.toEmailAddress = config.getInitParameter("to.email.address");
 92  6
             if (this.toEmailAddress ==null)  throw new ServletException("Missing to.email.address init parameter");
 93  
             
 94  6
             boolean allowAllServers = true;
 95  6
             String allowedServerStr = config.getInitParameter("allowed.servers");
 96  6
             if (allowedServerStr != null) {
 97  3
                     allowAllServers = false;
 98  3
                     StringTokenizer tokenizer = new StringTokenizer(allowedServerStr, ",");
 99  12
                     while (tokenizer.hasMoreTokens()) {
 100  9
                             this.allowedServerSet.add(HostUtils.deriveServerName(tokenizer.nextToken()));
 101  
                     }
 102  
             }
 103  
             
 104  6
             if (!allowAllServers && !this.allowedServerSet.contains(HostUtils.getHostName())) {
 105  3
                     this.suppressEmail = true;
 106  
             }
 107  6
     }
 108  
     
 109  
     /* (non-Javadoc)
 110  
      * @see net.admin4j.util.notify.Notifier#configure(java.lang.String, java.util.Properties)
 111  
      */
 112  
     public void configure(String namePrefix, Properties config) {
 113  0
         this.mailServerHost = config.getProperty(namePrefix + ".mail.server.host");
 114  0
         if (this.mailServerHost ==null)  {
 115  0
             throw new Admin4jRuntimeException("Missing property")
 116  
                 .addContextValue("property name", namePrefix + ".mail.server.host");
 117  
         }
 118  
         
 119  0
         this.fromEmailAddress = config.getProperty(namePrefix + ".from.email.address");
 120  0
         if (this.fromEmailAddress ==null)  {
 121  0
             throw new Admin4jRuntimeException("Missing property")
 122  
             .addContextValue("property name", namePrefix + ".from.email.address");
 123  
         }
 124  
         
 125  0
         this.toEmailAddress = config.getProperty(namePrefix + ".to.email.address");
 126  0
         if (this.toEmailAddress ==null)  {
 127  0
             throw new Admin4jRuntimeException("Missing property")
 128  
             .addContextValue("property name", namePrefix + ".to.email.address");
 129  
         }
 130  
         
 131  0
         boolean allowAllServers = true;
 132  0
         String allowedServerStr = config.getProperty(namePrefix + ".allowed.servers");
 133  0
         if (allowedServerStr != null) {
 134  0
             allowAllServers = false;
 135  0
             StringTokenizer tokenizer = new StringTokenizer(allowedServerStr, ",");
 136  0
             while (tokenizer.hasMoreTokens()) {
 137  0
                 this.allowedServerSet.add(HostUtils.deriveServerName(tokenizer.nextToken()));
 138  
             }
 139  
         }
 140  
         
 141  0
         if (!allowAllServers && !this.allowedServerSet.contains(HostUtils.getHostName())) {
 142  0
             this.suppressEmail = true;
 143  
         }
 144  
         
 145  0
     }
 146  
 
 147  
     public Set<String> getAllowedServerSet() {
 148  12
             return allowedServerSet;
 149  
     }
 150  
 
 151  
     public void setAllowedServerSet(Set<String> allowedServerSet) {
 152  0
             this.allowedServerSet = allowedServerSet;
 153  0
     }
 154  
 
 155  
     public String getFromEmailAddress() {
 156  21
             return fromEmailAddress;
 157  
     }
 158  
 
 159  
     public void setFromEmailAddress(String fromEmailAddress) {
 160  0
             this.fromEmailAddress = fromEmailAddress;
 161  0
     }
 162  
 
 163  
     public String getMailServerHost() {
 164  21
             return mailServerHost;
 165  
     }
 166  
 
 167  
     public void setMailServerHost(String mailServerHost) {
 168  0
             this.mailServerHost = mailServerHost;
 169  0
     }
 170  
 
 171  
     public boolean isSuppressEmail() {
 172  9
             return suppressEmail;
 173  
     }
 174  
 
 175  
     public void setSuppressEmail(boolean suppressEmail) {
 176  0
             this.suppressEmail = suppressEmail;
 177  0
     }
 178  
 
 179  
     public String getToEmailAddress() {
 180  21
             return toEmailAddress;
 181  
     }
 182  
 
 183  
     public void setToEmailAddress(String toEmailAddress) {
 184  0
             this.toEmailAddress = toEmailAddress;
 185  0
     }
 186  
 
 187  
     /* (non-Javadoc)
 188  
      * @see net.admin4j.util.notify.Notifier#supportsSMS()
 189  
      */
 190  
     public boolean supportsSMS() {
 191  3
         return false;
 192  
     }
 193  
 
 194  
 }