1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
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 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
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 | |
|
110 | |
|
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 | |
|
188 | |
|
189 | |
|
190 | |
public boolean supportsSMS() { |
191 | 3 | return false; |
192 | |
} |
193 | |
|
194 | |
} |