44 // Logger |
51 // Logger |
45 private static Logger log = Logger.getLogger(EmailDataSender.class); |
52 private static Logger log = Logger.getLogger(EmailDataSender.class); |
46 |
53 |
47 // Address email is sent from |
54 // Address email is sent from |
48 private String from; |
55 private String from; |
49 |
56 |
50 // The target address |
57 // The target address |
51 private String[] toAddressList; |
58 private String[] toAddressList; |
52 |
59 |
53 // LDAP config |
60 // LDAP config |
54 private String ldapURL; |
61 private String ldapURL; |
55 // TODO all reference to nokia internals MUST be removed. |
62 // TODO all reference to nokia internals MUST be removed. |
56 private String rootdn = "o=Nokia"; |
63 private String rootdn = "o=Nokia"; |
57 |
64 |
58 // Configured smtp server address |
65 // Configured smtp server address |
59 private String smtpServerAddress; |
66 private String smtpServerAddress; |
60 /** |
67 |
61 * Constructor |
68 /** |
62 * |
69 * Constructor |
63 * @param String |
70 * |
64 * comma separeted email recepients list |
71 * @param String comma separeted email recepients list |
65 * @param String |
72 * @param String smtp server |
66 * smtp server |
73 * @param String ldap server |
67 * @param String |
74 */ |
68 * ldap server |
75 public EmailDataSender(String toStrings, String smtpServer, String ldapAddress) { |
69 */ |
|
70 public EmailDataSender(String toStrings, String smtpServer, |
|
71 String ldapAddress) { |
|
72 if (toStrings != null) { |
76 if (toStrings != null) { |
73 String[] splitList = toStrings.split(","); |
77 String[] splitList = toStrings.split(","); |
74 toAddressList = splitList; |
78 toAddressList = splitList; |
75 } |
79 } |
76 smtpServerAddress = smtpServer; |
80 smtpServerAddress = smtpServer; |
77 ldapURL = ldapAddress; |
81 ldapURL = ldapAddress; |
78 } |
82 } |
79 /** |
83 |
80 * Constructor |
84 /** |
81 * |
85 * Constructor |
82 * @param String |
86 * |
83 * email recepient list in array |
87 * @param String email recepient list in array |
84 * @param String |
88 * @param String smtp server |
85 * smtp server |
89 * @param String ldap server |
86 * @param String |
90 */ |
87 * ldap server |
91 public EmailDataSender(String[] toList, String smtpServer, String ldapAddress) { |
88 */ |
|
89 public EmailDataSender(String[] toList, String smtpServer, |
|
90 String ldapAddress) { |
|
91 toAddressList = toList; |
92 toAddressList = toList; |
92 smtpServerAddress = smtpServer; |
93 smtpServerAddress = smtpServer; |
93 ldapURL = ldapAddress; |
94 ldapURL = ldapAddress; |
94 } |
95 } |
95 /** |
96 |
96 * Constructor |
97 /** |
97 * |
98 * Constructor |
98 * @param String |
99 * |
99 * email recepients list in array |
100 * @param String email recepients list in array |
100 * @param String |
101 * @param String smtp server |
101 * smtp server |
102 * @param String ldap server |
102 * @param String |
103 * @param String root domain in ldap server |
103 * ldap server |
104 */ |
104 * @param String |
105 public EmailDataSender(String[] toList, String smtpServer, String ldapAddress, String rootdn) { |
105 * root domain in ldap server |
|
106 */ |
|
107 public EmailDataSender(String[] toList, String smtpServer, |
|
108 String ldapAddress, String rootdn) { |
|
109 toAddressList = toList; |
106 toAddressList = toList; |
110 smtpServerAddress = smtpServer; |
107 smtpServerAddress = smtpServer; |
111 ldapURL = ldapAddress; |
108 ldapURL = ldapAddress; |
112 this.rootdn = rootdn; |
109 this.rootdn = rootdn; |
113 } |
110 } |
114 /** |
111 |
115 * Set sender address. |
112 /** |
116 * |
113 * Set sender address. |
117 * @param String |
114 * |
118 * mail sender address |
115 * @param String mail sender address |
119 */ |
116 */ |
120 public void setFrom(String from) { |
117 public void setFrom(String from) { |
121 this.from = from; |
118 this.from = from; |
122 } |
119 } |
123 /** |
120 |
124 * Add current user to recipient list. |
121 /** |
125 * |
122 * Add current user to recipient list. |
126 */ |
123 * |
127 public void addCurrentUserToAddressList() { |
124 */ |
|
125 public void addCurrentUserToAddressList() throws EmailSendException { |
128 // Create an empty array if needed |
126 // Create an empty array if needed |
129 if (toAddressList == null) |
127 if (toAddressList == null) { |
130 toAddressList = new String[0]; |
128 toAddressList = new String[0]; |
|
129 } |
131 try { |
130 try { |
132 String userEmail; |
131 String[] tmpToAddressList = Arrays.copyOf(toAddressList, toAddressList.length + 1); |
133 userEmail = getUserEmail(); |
132 tmpToAddressList[tmpToAddressList.length - 1] = getUserEmail(); |
134 toAddressList = Arrays.copyOf(toAddressList, |
133 toAddressList = tmpToAddressList; |
135 toAddressList.length + 1); |
134 } catch (LDAPException ex) { |
136 toAddressList[toAddressList.length - 1] = userEmail; |
135 throw new EmailSendException(ex.getMessage(), ex); |
137 } catch (javax.mail.internet.AddressException e) { |
136 } |
138 return; //does this so that it passes checkstyle and builds |
137 } |
139 } catch (javax.naming.NamingException e) { |
138 |
140 return; //without it build complains that errors are thrown and not caught |
139 /** |
141 //methods are used else where and are caught and exercised. |
140 * Get recipient address list. |
142 } |
141 * |
143 } |
142 * @return Recipient address list. |
144 /** |
143 */ |
145 * Get recipient address list. |
|
146 * |
|
147 * @return |
|
148 * Recipient address list. |
|
149 */ |
|
150 private InternetAddress[] getToAddressList() { |
144 private InternetAddress[] getToAddressList() { |
151 int toListLength = 0; |
145 int toListLength = 0; |
152 if (toAddressList != null) |
146 if (toAddressList != null) { |
153 toListLength = toAddressList.length; |
147 toListLength = toAddressList.length; |
|
148 } |
154 InternetAddress[] addressList = new InternetAddress[toListLength]; |
149 InternetAddress[] addressList = new InternetAddress[toListLength]; |
155 try { |
150 try { |
156 log.debug("getToAddressList:length: " |
151 log.debug("getToAddressList:length: " + toListLength); |
157 + toListLength); |
|
158 for (int i = 0; i < toListLength; i++) { |
152 for (int i = 0; i < toListLength; i++) { |
159 log.debug("getToAddressList:address:" |
153 log.debug("getToAddressList:address:" + toAddressList[i]); |
160 + toAddressList[i]); |
|
161 addressList[i] = new InternetAddress(toAddressList[i]); |
154 addressList[i] = new InternetAddress(toAddressList[i]); |
162 } |
155 } |
163 } catch (javax.mail.internet.AddressException aex) { |
156 } |
|
157 catch (AddressException aex) { |
164 log.error("AddressException: " + aex); |
158 log.error("AddressException: " + aex); |
165 } |
159 } |
166 return addressList; |
160 return addressList; |
167 } |
161 } |
168 /** |
162 |
169 * Send xml data without compression |
163 /** |
170 * |
164 * Send xml data without compression |
171 * @param String |
165 * |
172 * purpose of this email |
166 * @param String purpose of this email |
173 * @param String |
167 * @param String file to send |
174 * file to send |
168 * @param String mime type |
175 * @param String |
169 * @param String subject of email |
176 * mime type |
170 * @param String header of email |
177 * @param String |
171 */ |
178 * subject of email |
172 public void sendData(String purpose, File fileToSend, String mimeType, |
179 * @param String |
173 String subject, String header) throws EmailSendException { |
180 * header of email |
|
181 */ |
|
182 public void sendData(String purpose, String fileToSend, String mimeType, |
|
183 String subject, String header) { |
|
184 sendData(purpose, fileToSend, mimeType, subject, header, false); |
174 sendData(purpose, fileToSend, mimeType, subject, header, false); |
185 } |
175 } |
186 |
176 |
187 |
177 /** |
188 /** |
178 * Sending the XML data(compressed) through email. |
189 * Sending the XML data(compressed) through email. |
179 * |
190 * |
180 * @param String purpose of this email |
191 * @param String |
181 * @param String file to send |
192 * purpose of this email |
182 * @param String subject of email |
193 * @param String |
183 * @param String header of email |
194 * file to send |
184 */ |
195 * @param String |
185 public void compresseAndSendData(String purpose, File fileToSend, |
196 * subject of email |
186 String subject, String header) throws EmailSendException { |
197 * @param String |
|
198 * header of email |
|
199 */ |
|
200 public void compresseAndSendData(String purpose, String fileToSend, |
|
201 String subject, String header) { |
|
202 sendData(purpose, fileToSend, null, subject, header, true); |
187 sendData(purpose, fileToSend, null, subject, header, true); |
203 } |
188 } |
204 /** |
189 |
205 * Send xml data |
190 /** |
206 * |
191 * Send xml data |
207 * @param String |
192 * |
208 * purpose of this email |
193 * @param String purpose of this email |
209 * @param String |
194 * @param String file to send |
210 * file to send |
195 * @param String mime type |
211 * @param String |
196 * @param String subject of email |
212 * mime type |
197 * @param String header of mail |
213 * @param String |
198 * @param boolean compress data if true |
214 * subject of email |
199 */ |
215 * @param String |
200 public void sendData(String purpose, File fileToSend, String mimeType, |
216 * header of mail |
201 String subject, String header, boolean compressData) throws EmailSendException { |
217 * @param boolean |
|
218 * compress data if true |
|
219 */ |
|
220 public void sendData(String purpose, String fileToSend, String mimeType, |
|
221 String subject, String header, boolean compressData) { |
|
222 try { |
202 try { |
223 log.debug("sendData:Send file: " + fileToSend + " and mimetype: " + mimeType); |
203 log.debug("sendData:Send file: " + fileToSend + " and mimetype: " + mimeType); |
224 if (fileToSend != null) { |
204 if (fileToSend != null && fileToSend.exists()) { |
225 InternetAddress[] toAddresses = getToAddressList(); |
205 InternetAddress[] toAddresses = getToAddressList(); |
226 Properties props = new Properties(); |
206 Properties props = new Properties(); |
227 if (smtpServerAddress != null) { |
207 if (smtpServerAddress != null) { |
228 log.debug("sendData:smtp address: " + smtpServerAddress); |
208 log.debug("sendData:smtp address: " + smtpServerAddress); |
229 props.setProperty("mail.smtp.host", smtpServerAddress); |
209 props.setProperty("mail.smtp.host", smtpServerAddress); |
230 } |
210 } |
231 Session mailSession = Session.getDefaultInstance(props, null); |
211 Session mailSession = Session.getDefaultInstance(props, null); |
232 MimeMessage message = new MimeMessage(mailSession); |
212 MimeMessage message = new MimeMessage(mailSession); |
233 String subjectToSend = subject; |
213 message.setSubject(subject == null ? "" : subject); |
234 if (subject == null) { |
|
235 subjectToSend = ""; |
|
236 } |
|
237 message.setSubject(subjectToSend); |
|
238 MimeMultipart multipart = new MimeMultipart("related"); |
214 MimeMultipart multipart = new MimeMultipart("related"); |
239 BodyPart messageBodyPart = new MimeBodyPart(); |
215 BodyPart messageBodyPart = new MimeBodyPart(); |
240 ByteArrayDataSource dataSrc = null; |
216 ByteArrayDataSource dataSrc = null; |
241 String fileName = new File(fileToSend).getName(); |
217 String fileName = fileToSend.getName(); |
242 if (compressData) { |
218 if (compressData) { |
243 log.debug("Sending compressed data"); |
219 log.debug("Sending compressed data"); |
244 dataSrc = compressFile(fileToSend); |
220 dataSrc = compressFile(fileToSend); |
245 dataSrc.setName(fileName + ".gz"); |
221 dataSrc.setName(fileName + ".gz"); |
246 messageBodyPart.setFileName(fileName + ".gz"); |
222 messageBodyPart.setFileName(fileName + ".gz"); |
247 } else { |
223 } |
|
224 else { |
248 log.debug("Sending uncompressed data:"); |
225 log.debug("Sending uncompressed data:"); |
249 dataSrc = new ByteArrayDataSource(new FileInputStream( |
226 dataSrc = new ByteArrayDataSource(new FileInputStream(fileToSend), mimeType); |
250 new File(fileToSend)), mimeType); |
227 |
251 |
228 message.setContent(FileUtils.readFileToString(fileToSend), "text/html"); |
252 message.setContent(FileUtils.readFileToString(new File( |
|
253 fileToSend)), "text/html"); |
|
254 multipart = null; |
229 multipart = null; |
255 } |
230 } |
256 String headerToSend = null; |
231 String headerToSend = null; |
257 if (header == null) { |
232 if (header == null) { |
258 headerToSend = ""; |
233 headerToSend = ""; |
264 multipart.addBodyPart(messageBodyPart); // add to the |
239 multipart.addBodyPart(messageBodyPart); // add to the |
265 // multipart |
240 // multipart |
266 message.setContent(multipart); |
241 message.setContent(multipart); |
267 } |
242 } |
268 try { |
243 try { |
269 InternetAddress fromAddress = getFromAddress(); |
244 message.setFrom(getFromAddress()); |
270 message.setFrom(fromAddress); |
245 } |
271 } catch (HlmAntLibException e) { |
246 catch (AddressException e) { |
272 // We are Ignoring the errors as no need to fail the build. |
247 throw new EmailSendException("Error retrieving the from address: " + e.getMessage(), e); |
273 log.debug("Error retrieving current user email address: " + e.getMessage(), e); |
248 } catch (LDAPException e) { |
274 } catch (javax.mail.internet.AddressException e) { |
249 throw new EmailSendException("Error retrieving the from address: " + e.getMessage(), e); |
275 // We are Ignoring the errors as no need to fail the build. |
|
276 log.debug("Error retrieving current user email address: " + e.getMessage(), e); |
|
277 } catch (javax.naming.NamingException e) { |
|
278 // We are Ignoring the errors as no need to fail the build. |
|
279 log.debug("Error retrieving current user email address: " + e.getMessage(), e); |
|
280 } |
250 } |
281 message.addRecipients(Message.RecipientType.TO, toAddresses); |
251 message.addRecipients(Message.RecipientType.TO, toAddresses); |
282 log.info("Sending email alert: " + subject); |
252 log.info("Sending email alert: " + subject); |
283 Transport.send(message); |
253 Transport.send(message); |
284 } |
254 } |
285 } catch (javax.mail.MessagingException e) { |
255 } catch (MessagingException e) { |
286 String errorMessage = e.getMessage(); |
|
287 String fullErrorMessage = "Failed sending e-mail: " + purpose; |
256 String fullErrorMessage = "Failed sending e-mail: " + purpose; |
288 if (errorMessage != null) { |
257 if (e.getMessage() != null) { |
289 fullErrorMessage += " " + errorMessage; |
258 fullErrorMessage += ": " + e.getMessage(); |
290 } |
259 } |
291 } catch (java.io.IOException e) { |
260 throw new EmailSendException(fullErrorMessage, e); |
292 String errorMessage = e.getMessage(); |
261 } catch (IOException e) { |
293 String fullErrorMessage = "Failed sending e-mail: " + purpose; |
262 String fullErrorMessage = "Failed sending e-mail: " + purpose; |
294 if (errorMessage != null) { |
263 if (e.getMessage() != null) { |
295 fullErrorMessage += " " + errorMessage; |
264 fullErrorMessage += ": " + e.getMessage(); |
296 } |
265 } |
297 // We are Ignoring the errors as no need to fail the build. |
266 // We are Ignoring the errors as no need to fail the build. |
298 log.info(fullErrorMessage); |
267 throw new EmailSendException(fullErrorMessage, e); |
299 } |
268 } |
300 } |
269 } |
301 |
270 |
302 /** |
271 /** |
303 * GZipping a string. |
272 * GZipping a string. |
304 * |
273 * |
305 * @param data |
274 * @param data the content to be gzipped. |
306 * the content to be gzipped. |
275 * @param filename the name for the file. |
307 * @param filename |
|
308 * the name for the file. |
|
309 * @return a ByteArrayDataSource. |
276 * @return a ByteArrayDataSource. |
310 */ |
277 */ |
311 protected ByteArrayDataSource compressFile(String fileName) |
278 protected ByteArrayDataSource compressFile(File fileName) throws IOException { |
312 throws IOException { |
|
313 ByteArrayOutputStream out = new ByteArrayOutputStream(); |
279 ByteArrayOutputStream out = new ByteArrayOutputStream(); |
314 GZIPOutputStream gz = new GZIPOutputStream(out); |
280 GZIPOutputStream gz = new GZIPOutputStream(out); |
315 BufferedInputStream bufferedInputStream = new BufferedInputStream( |
281 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName)); |
316 new FileInputStream(new File(fileName))); |
|
317 byte[] dataBuffer = new byte[512]; |
282 byte[] dataBuffer = new byte[512]; |
318 while ((bufferedInputStream.read(dataBuffer)) != -1) { |
283 while ((bufferedInputStream.read(dataBuffer)) != -1) { |
319 gz.write(dataBuffer); |
284 gz.write(dataBuffer); |
320 } |
285 } |
321 gz.close(); |
286 gz.close(); |
322 bufferedInputStream.close(); |
287 bufferedInputStream.close(); |
323 ByteArrayDataSource dataSrc = new ByteArrayDataSource( |
288 ByteArrayDataSource dataSrc = new ByteArrayDataSource(out.toByteArray(), "application/x-gzip"); |
324 out.toByteArray(), "application/x-gzip"); |
|
325 return dataSrc; |
289 return dataSrc; |
326 } |
290 } |
327 /** |
291 |
328 * Get sender address. |
292 /** |
329 * |
293 * Get sender address. |
330 * @return |
294 * |
331 * sender address. |
295 * @return sender address. |
332 */ |
296 * @throws AddressException |
333 private InternetAddress getFromAddress() throws javax.mail.internet.AddressException, javax.naming.NamingException { |
297 * @throws LDAPException |
|
298 */ |
|
299 private InternetAddress getFromAddress() throws AddressException, LDAPException { |
334 if (from != null) { |
300 if (from != null) { |
335 return new InternetAddress(from); |
301 return new InternetAddress(from); |
336 } |
302 } |
337 return new InternetAddress(getUserEmail()); |
303 return new InternetAddress(getUserEmail()); |
338 } |
304 } |
339 |
305 |
340 /** |
306 /** |
341 * Getting user email. |
307 * Getting user email. |
342 * |
308 * |
343 * @return |
309 * @return the user email address. |
344 * the user email address. |
310 */ |
345 */ |
311 protected String getUserEmail() throws LDAPException { |
346 protected String getUserEmail() throws javax.mail.internet.AddressException, javax.naming.NamingException { |
312 if (ldapURL != null) { |
347 String username = System.getProperty("user.name"); |
313 LDAPHelper helper = new LDAPHelper(this.ldapURL, this.rootdn); |
348 log.debug("EmailDataSender:getUserEmail:username: " + username); |
314 String email = helper.getUserAttributeAsString(LDAPHelper.EMAIL_ATTRIBUTE_NAME); |
349 |
315 if (email == null) { |
350 // Set up environment for creating initial context |
316 throw new LDAPException("Could not find email for current user. (" + System.getProperty("user.name") + ")"); |
351 Hashtable<String, String> env = new Hashtable<String, String>(11); |
317 } |
352 env.put(Context.INITIAL_CONTEXT_FACTORY, |
318 return email; |
353 "com.sun.jndi.ldap.LdapCtxFactory"); |
319 } |
354 env.put(Context.PROVIDER_URL, ldapURL + "/" + rootdn); |
320 throw new LDAPException("LDAP url is not defined."); |
355 |
|
356 // Create initial context |
|
357 DirContext ctx = new InitialDirContext(env); |
|
358 SearchControls controls = new SearchControls(); |
|
359 controls.setSearchScope(SearchControls.SUBTREE_SCOPE); |
|
360 try { |
|
361 NamingEnumeration<SearchResult> en = ctx.search("", "uid=" + username, controls); |
|
362 if (en.hasMore()) { |
|
363 SearchResult sr = en.next(); |
|
364 if (sr.getAttributes().get("mail") != null) { |
|
365 String email = (String) sr.getAttributes().get("mail").get(); |
|
366 log.debug("getUserEmail:" + email); |
|
367 return email; |
|
368 } |
|
369 } |
|
370 } catch (javax.naming.NameNotFoundException ex) { |
|
371 throw new HlmAntLibException("Error finding user email for " + username ); |
|
372 } |
|
373 throw new HlmAntLibException("Could not find user email in LDAP."); |
|
374 } |
321 } |
375 } |
322 } |
376 |
|
377 |
|
378 |
|