587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.core;
|
|
19 |
|
|
20 |
import java.util.Arrays;
|
|
21 |
import java.util.Properties;
|
|
22 |
import java.util.Hashtable;
|
|
23 |
import java.util.zip.GZIPOutputStream;
|
|
24 |
import java.io.BufferedInputStream;
|
|
25 |
import java.io.ByteArrayOutputStream;
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.FileInputStream;
|
|
28 |
import java.io.IOException;
|
|
29 |
import javax.mail.*;
|
|
30 |
import javax.mail.internet.*;
|
|
31 |
import javax.naming.*;
|
|
32 |
import javax.naming.directory.*;
|
|
33 |
|
|
34 |
import javax.activation.DataHandler;
|
|
35 |
import javax.mail.util.ByteArrayDataSource;
|
|
36 |
import org.apache.log4j.Logger;
|
|
37 |
import org.apache.commons.io.FileUtils;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Send compressed or uncompressed xml data with email.
|
|
41 |
*/
|
|
42 |
public class EmailDataSender {
|
|
43 |
|
|
44 |
// Logger
|
|
45 |
private static Logger log = Logger.getLogger(EmailDataSender.class);
|
|
46 |
|
|
47 |
// Address email is sent from
|
|
48 |
private String from;
|
|
49 |
|
|
50 |
// The target address
|
|
51 |
private String[] toAddressList;
|
|
52 |
|
|
53 |
// LDAP config
|
|
54 |
private String ldapURL;
|
|
55 |
// TODO all reference to nokia internals MUST be removed.
|
|
56 |
private String rootdn = "o=Nokia";
|
|
57 |
|
|
58 |
// Configured smtp server address
|
|
59 |
private String smtpServerAddress;
|
|
60 |
/**
|
|
61 |
* Constructor
|
|
62 |
*
|
|
63 |
* @param String
|
|
64 |
* comma separeted email recepients list
|
|
65 |
* @param String
|
|
66 |
* smtp server
|
|
67 |
* @param String
|
|
68 |
* ldap server
|
|
69 |
*/
|
|
70 |
public EmailDataSender(String toStrings, String smtpServer,
|
|
71 |
String ldapAddress) {
|
|
72 |
if (toStrings != null) {
|
|
73 |
String[] splitList = toStrings.split(",");
|
|
74 |
toAddressList = splitList;
|
|
75 |
}
|
|
76 |
smtpServerAddress = smtpServer;
|
|
77 |
ldapURL = ldapAddress;
|
|
78 |
}
|
|
79 |
/**
|
|
80 |
* Constructor
|
|
81 |
*
|
|
82 |
* @param String
|
|
83 |
* email recepient list in array
|
|
84 |
* @param String
|
|
85 |
* smtp server
|
|
86 |
* @param String
|
|
87 |
* ldap server
|
|
88 |
*/
|
|
89 |
public EmailDataSender(String[] toList, String smtpServer,
|
|
90 |
String ldapAddress) {
|
|
91 |
toAddressList = toList;
|
|
92 |
smtpServerAddress = smtpServer;
|
|
93 |
ldapURL = ldapAddress;
|
|
94 |
}
|
|
95 |
/**
|
|
96 |
* Constructor
|
|
97 |
*
|
|
98 |
* @param String
|
|
99 |
* email recepients list in array
|
|
100 |
* @param String
|
|
101 |
* smtp server
|
|
102 |
* @param String
|
|
103 |
* ldap server
|
|
104 |
* @param String
|
|
105 |
* root domain in ldap server
|
|
106 |
*/
|
|
107 |
public EmailDataSender(String[] toList, String smtpServer,
|
|
108 |
String ldapAddress, String rootdn) {
|
|
109 |
toAddressList = toList;
|
|
110 |
smtpServerAddress = smtpServer;
|
|
111 |
ldapURL = ldapAddress;
|
|
112 |
this.rootdn = rootdn;
|
|
113 |
}
|
|
114 |
/**
|
|
115 |
* Set sender address.
|
|
116 |
*
|
|
117 |
* @param String
|
|
118 |
* mail sender address
|
|
119 |
*/
|
|
120 |
public void setFrom(String from) {
|
|
121 |
this.from = from;
|
|
122 |
}
|
|
123 |
/**
|
|
124 |
* Add current user to recipient list.
|
|
125 |
*
|
|
126 |
*/
|
|
127 |
public void addCurrentUserToAddressList() {
|
|
128 |
// Create an empty array if needed
|
|
129 |
if (toAddressList == null)
|
|
130 |
toAddressList = new String[0];
|
|
131 |
try {
|
|
132 |
String userEmail;
|
|
133 |
userEmail = getUserEmail();
|
|
134 |
toAddressList = Arrays.copyOf(toAddressList,
|
|
135 |
toAddressList.length + 1);
|
|
136 |
toAddressList[toAddressList.length - 1] = userEmail;
|
|
137 |
} catch (javax.mail.internet.AddressException e) {
|
|
138 |
return; //does this so that it passes checkstyle and builds
|
|
139 |
} catch (javax.naming.NamingException e) {
|
|
140 |
return; //without it build complains that errors are thrown and not caught
|
|
141 |
//methods are used else where and are caught and exercised.
|
|
142 |
}
|
|
143 |
}
|
|
144 |
/**
|
|
145 |
* Get recipient address list.
|
|
146 |
*
|
|
147 |
* @return
|
|
148 |
* Recipient address list.
|
|
149 |
*/
|
|
150 |
private InternetAddress[] getToAddressList() {
|
|
151 |
int toListLength = 0;
|
|
152 |
if (toAddressList != null)
|
|
153 |
toListLength = toAddressList.length;
|
|
154 |
InternetAddress[] addressList = new InternetAddress[toListLength];
|
|
155 |
try {
|
|
156 |
log.debug("getToAddressList:length: "
|
|
157 |
+ toListLength);
|
|
158 |
for (int i = 0; i < toListLength; i++) {
|
|
159 |
log.debug("getToAddressList:address:"
|
|
160 |
+ toAddressList[i]);
|
|
161 |
addressList[i] = new InternetAddress(toAddressList[i]);
|
|
162 |
}
|
|
163 |
} catch (javax.mail.internet.AddressException aex) {
|
|
164 |
log.error("AddressException: " + aex);
|
|
165 |
}
|
|
166 |
return addressList;
|
|
167 |
}
|
|
168 |
/**
|
|
169 |
* Send xml data without compression
|
|
170 |
*
|
|
171 |
* @param String
|
|
172 |
* purpose of this email
|
|
173 |
* @param String
|
|
174 |
* file to send
|
|
175 |
* @param String
|
|
176 |
* mime type
|
|
177 |
* @param String
|
|
178 |
* subject of email
|
|
179 |
* @param String
|
|
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);
|
|
185 |
}
|
|
186 |
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Sending the XML data(compressed) through email.
|
|
190 |
*
|
|
191 |
* @param String
|
|
192 |
* purpose of this email
|
|
193 |
* @param String
|
|
194 |
* file to send
|
|
195 |
* @param String
|
|
196 |
* subject of email
|
|
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);
|
|
203 |
}
|
|
204 |
/**
|
|
205 |
* Send xml data
|
|
206 |
*
|
|
207 |
* @param String
|
|
208 |
* purpose of this email
|
|
209 |
* @param String
|
|
210 |
* file to send
|
|
211 |
* @param String
|
|
212 |
* mime type
|
|
213 |
* @param String
|
|
214 |
* subject of email
|
|
215 |
* @param String
|
|
216 |
* header of mail
|
|
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 {
|
|
223 |
log.debug("sendData:Send file: " + fileToSend + " and mimetype: " + mimeType);
|
|
224 |
if (fileToSend != null) {
|
|
225 |
InternetAddress[] toAddresses = getToAddressList();
|
|
226 |
Properties props = new Properties();
|
|
227 |
if (smtpServerAddress != null) {
|
|
228 |
log.debug("sendData:smtp address: " + smtpServerAddress);
|
|
229 |
props.setProperty("mail.smtp.host", smtpServerAddress);
|
|
230 |
}
|
|
231 |
Session mailSession = Session.getDefaultInstance(props, null);
|
|
232 |
MimeMessage message = new MimeMessage(mailSession);
|
|
233 |
String subjectToSend = subject;
|
|
234 |
if (subject == null) {
|
|
235 |
subjectToSend = "";
|
|
236 |
}
|
|
237 |
message.setSubject(subjectToSend);
|
|
238 |
MimeMultipart multipart = new MimeMultipart("related");
|
|
239 |
BodyPart messageBodyPart = new MimeBodyPart();
|
|
240 |
ByteArrayDataSource dataSrc = null;
|
|
241 |
String fileName = new File(fileToSend).getName();
|
|
242 |
if (compressData) {
|
|
243 |
log.debug("Sending compressed data");
|
|
244 |
dataSrc = compressFile(fileToSend);
|
|
245 |
dataSrc.setName(fileName + ".gz");
|
|
246 |
messageBodyPart.setFileName(fileName + ".gz");
|
|
247 |
} else {
|
|
248 |
log.debug("Sending uncompressed data:");
|
|
249 |
dataSrc = new ByteArrayDataSource(new FileInputStream(
|
|
250 |
new File(fileToSend)), mimeType);
|
|
251 |
|
|
252 |
message.setContent(FileUtils.readFileToString(new File(
|
|
253 |
fileToSend)), "text/html");
|
|
254 |
multipart = null;
|
|
255 |
}
|
|
256 |
String headerToSend = null;
|
|
257 |
if (header == null) {
|
|
258 |
headerToSend = "";
|
|
259 |
}
|
|
260 |
messageBodyPart.setHeader("helium-bld-data", headerToSend);
|
|
261 |
messageBodyPart.setDataHandler(new DataHandler(dataSrc));
|
|
262 |
|
|
263 |
if (multipart != null) {
|
|
264 |
multipart.addBodyPart(messageBodyPart); // add to the
|
|
265 |
// multipart
|
|
266 |
message.setContent(multipart);
|
|
267 |
}
|
|
268 |
try {
|
|
269 |
InternetAddress fromAddress = getFromAddress();
|
|
270 |
message.setFrom(fromAddress);
|
618
|
271 |
} catch (HlmAntLibException e) {
|
|
272 |
// We are Ignoring the errors as no need to fail the build.
|
|
273 |
log.debug("Error retrieving current user email address: " + e.getMessage(), e);
|
587
|
274 |
} catch (javax.mail.internet.AddressException 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 |
}
|
|
281 |
message.addRecipients(Message.RecipientType.TO, toAddresses);
|
|
282 |
log.info("Sending email alert: " + subject);
|
|
283 |
Transport.send(message);
|
|
284 |
}
|
|
285 |
} catch (javax.mail.MessagingException e) {
|
|
286 |
String errorMessage = e.getMessage();
|
|
287 |
String fullErrorMessage = "Failed sending e-mail: " + purpose;
|
|
288 |
if (errorMessage != null) {
|
|
289 |
fullErrorMessage += " " + errorMessage;
|
|
290 |
}
|
|
291 |
} catch (java.io.IOException e) {
|
|
292 |
String errorMessage = e.getMessage();
|
|
293 |
String fullErrorMessage = "Failed sending e-mail: " + purpose;
|
|
294 |
if (errorMessage != null) {
|
|
295 |
fullErrorMessage += " " + errorMessage;
|
|
296 |
}
|
|
297 |
// We are Ignoring the errors as no need to fail the build.
|
|
298 |
log.info(fullErrorMessage);
|
|
299 |
}
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* GZipping a string.
|
|
304 |
*
|
|
305 |
* @param data
|
|
306 |
* the content to be gzipped.
|
|
307 |
* @param filename
|
|
308 |
* the name for the file.
|
|
309 |
* @return a ByteArrayDataSource.
|
|
310 |
*/
|
|
311 |
protected ByteArrayDataSource compressFile(String fileName)
|
|
312 |
throws IOException {
|
|
313 |
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
314 |
GZIPOutputStream gz = new GZIPOutputStream(out);
|
|
315 |
BufferedInputStream bufferedInputStream = new BufferedInputStream(
|
|
316 |
new FileInputStream(new File(fileName)));
|
|
317 |
byte[] dataBuffer = new byte[512];
|
|
318 |
while ((bufferedInputStream.read(dataBuffer)) != -1) {
|
|
319 |
gz.write(dataBuffer);
|
|
320 |
}
|
|
321 |
gz.close();
|
|
322 |
bufferedInputStream.close();
|
|
323 |
ByteArrayDataSource dataSrc = new ByteArrayDataSource(
|
|
324 |
out.toByteArray(), "application/x-gzip");
|
|
325 |
return dataSrc;
|
|
326 |
}
|
|
327 |
/**
|
|
328 |
* Get sender address.
|
|
329 |
*
|
|
330 |
* @return
|
|
331 |
* sender address.
|
|
332 |
*/
|
|
333 |
private InternetAddress getFromAddress() throws javax.mail.internet.AddressException, javax.naming.NamingException {
|
|
334 |
if (from != null) {
|
|
335 |
return new InternetAddress(from);
|
|
336 |
}
|
|
337 |
return new InternetAddress(getUserEmail());
|
|
338 |
}
|
|
339 |
|
|
340 |
/**
|
|
341 |
* Getting user email.
|
|
342 |
*
|
|
343 |
* @return
|
|
344 |
* the user email address.
|
|
345 |
*/
|
|
346 |
protected String getUserEmail() throws javax.mail.internet.AddressException, javax.naming.NamingException {
|
|
347 |
String username = System.getProperty("user.name");
|
|
348 |
log.debug("EmailDataSender:getUserEmail:username: " + username);
|
|
349 |
|
|
350 |
// Set up environment for creating initial context
|
|
351 |
Hashtable<String, String> env = new Hashtable<String, String>(11);
|
|
352 |
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
|
353 |
"com.sun.jndi.ldap.LdapCtxFactory");
|
|
354 |
env.put(Context.PROVIDER_URL, ldapURL + "/" + rootdn);
|
|
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();
|
618
|
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 |
}
|
587
|
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 |
}
|
|
375 |
}
|
|
376 |
|
|
377 |
|
|
378 |
|