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.zip.GZIPOutputStream;
|
|
23 |
import java.io.BufferedInputStream;
|
|
24 |
import java.io.ByteArrayOutputStream;
|
|
25 |
import java.io.File;
|
|
26 |
import java.io.FileInputStream;
|
|
27 |
import java.io.IOException;
|
628
|
28 |
|
|
29 |
import javax.mail.BodyPart;
|
|
30 |
import javax.mail.Message;
|
|
31 |
import javax.mail.MessagingException;
|
|
32 |
import javax.mail.Session;
|
|
33 |
import javax.mail.Transport;
|
|
34 |
import javax.mail.internet.InternetAddress;
|
|
35 |
import javax.mail.internet.MimeBodyPart;
|
|
36 |
import javax.mail.internet.MimeMessage;
|
|
37 |
import javax.mail.internet.MimeMultipart;
|
587
|
38 |
|
|
39 |
import javax.activation.DataHandler;
|
|
40 |
import javax.mail.util.ByteArrayDataSource;
|
628
|
41 |
import javax.mail.internet.AddressException;
|
|
42 |
|
587
|
43 |
import org.apache.log4j.Logger;
|
|
44 |
import org.apache.commons.io.FileUtils;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Send compressed or uncompressed xml data with email.
|
|
48 |
*/
|
|
49 |
public class EmailDataSender {
|
|
50 |
|
|
51 |
// Logger
|
|
52 |
private static Logger log = Logger.getLogger(EmailDataSender.class);
|
|
53 |
|
|
54 |
// Address email is sent from
|
|
55 |
private String from;
|
628
|
56 |
|
587
|
57 |
// The target address
|
|
58 |
private String[] toAddressList;
|
|
59 |
|
|
60 |
// LDAP config
|
|
61 |
private String ldapURL;
|
|
62 |
// TODO all reference to nokia internals MUST be removed.
|
|
63 |
private String rootdn = "o=Nokia";
|
|
64 |
|
|
65 |
// Configured smtp server address
|
|
66 |
private String smtpServerAddress;
|
628
|
67 |
|
587
|
68 |
/**
|
628
|
69 |
* Constructor
|
|
70 |
*
|
|
71 |
* @param String comma separeted email recepients list
|
|
72 |
* @param String smtp server
|
|
73 |
* @param String ldap server
|
|
74 |
*/
|
|
75 |
public EmailDataSender(String toStrings, String smtpServer, String ldapAddress) {
|
587
|
76 |
if (toStrings != null) {
|
|
77 |
String[] splitList = toStrings.split(",");
|
|
78 |
toAddressList = splitList;
|
|
79 |
}
|
|
80 |
smtpServerAddress = smtpServer;
|
|
81 |
ldapURL = ldapAddress;
|
|
82 |
}
|
628
|
83 |
|
587
|
84 |
/**
|
628
|
85 |
* Constructor
|
|
86 |
*
|
|
87 |
* @param String email recepient list in array
|
|
88 |
* @param String smtp server
|
|
89 |
* @param String ldap server
|
|
90 |
*/
|
|
91 |
public EmailDataSender(String[] toList, String smtpServer, String ldapAddress) {
|
587
|
92 |
toAddressList = toList;
|
|
93 |
smtpServerAddress = smtpServer;
|
|
94 |
ldapURL = ldapAddress;
|
|
95 |
}
|
628
|
96 |
|
587
|
97 |
/**
|
628
|
98 |
* Constructor
|
|
99 |
*
|
|
100 |
* @param String email recepients list in array
|
|
101 |
* @param String smtp server
|
|
102 |
* @param String ldap server
|
|
103 |
* @param String root domain in ldap server
|
|
104 |
*/
|
|
105 |
public EmailDataSender(String[] toList, String smtpServer, String ldapAddress, String rootdn) {
|
587
|
106 |
toAddressList = toList;
|
|
107 |
smtpServerAddress = smtpServer;
|
|
108 |
ldapURL = ldapAddress;
|
|
109 |
this.rootdn = rootdn;
|
|
110 |
}
|
628
|
111 |
|
587
|
112 |
/**
|
628
|
113 |
* Set sender address.
|
|
114 |
*
|
|
115 |
* @param String mail sender address
|
|
116 |
*/
|
587
|
117 |
public void setFrom(String from) {
|
|
118 |
this.from = from;
|
|
119 |
}
|
628
|
120 |
|
587
|
121 |
/**
|
628
|
122 |
* Add current user to recipient list.
|
|
123 |
*
|
|
124 |
*/
|
|
125 |
public void addCurrentUserToAddressList() throws EmailSendException {
|
587
|
126 |
// Create an empty array if needed
|
628
|
127 |
if (toAddressList == null) {
|
587
|
128 |
toAddressList = new String[0];
|
628
|
129 |
}
|
587
|
130 |
try {
|
628
|
131 |
String[] tmpToAddressList = Arrays.copyOf(toAddressList, toAddressList.length + 1);
|
|
132 |
tmpToAddressList[tmpToAddressList.length - 1] = getUserEmail();
|
|
133 |
toAddressList = tmpToAddressList;
|
|
134 |
} catch (LDAPException ex) {
|
|
135 |
throw new EmailSendException(ex.getMessage(), ex);
|
587
|
136 |
}
|
|
137 |
}
|
628
|
138 |
|
587
|
139 |
/**
|
628
|
140 |
* Get recipient address list.
|
|
141 |
*
|
|
142 |
* @return Recipient address list.
|
|
143 |
*/
|
587
|
144 |
private InternetAddress[] getToAddressList() {
|
|
145 |
int toListLength = 0;
|
628
|
146 |
if (toAddressList != null) {
|
587
|
147 |
toListLength = toAddressList.length;
|
628
|
148 |
}
|
587
|
149 |
InternetAddress[] addressList = new InternetAddress[toListLength];
|
|
150 |
try {
|
628
|
151 |
log.debug("getToAddressList:length: " + toListLength);
|
587
|
152 |
for (int i = 0; i < toListLength; i++) {
|
628
|
153 |
log.debug("getToAddressList:address:" + toAddressList[i]);
|
587
|
154 |
addressList[i] = new InternetAddress(toAddressList[i]);
|
|
155 |
}
|
628
|
156 |
}
|
|
157 |
catch (AddressException aex) {
|
587
|
158 |
log.error("AddressException: " + aex);
|
|
159 |
}
|
|
160 |
return addressList;
|
|
161 |
}
|
628
|
162 |
|
587
|
163 |
/**
|
628
|
164 |
* Send xml data without compression
|
|
165 |
*
|
|
166 |
* @param String purpose of this email
|
|
167 |
* @param String file to send
|
|
168 |
* @param String mime type
|
|
169 |
* @param String subject of email
|
|
170 |
* @param String header of email
|
|
171 |
*/
|
|
172 |
public void sendData(String purpose, File fileToSend, String mimeType,
|
|
173 |
String subject, String header) throws EmailSendException {
|
587
|
174 |
sendData(purpose, fileToSend, mimeType, subject, header, false);
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
628
|
178 |
* Sending the XML data(compressed) through email.
|
|
179 |
*
|
|
180 |
* @param String purpose of this email
|
|
181 |
* @param String file to send
|
|
182 |
* @param String subject of email
|
|
183 |
* @param String header of email
|
|
184 |
*/
|
|
185 |
public void compresseAndSendData(String purpose, File fileToSend,
|
|
186 |
String subject, String header) throws EmailSendException {
|
587
|
187 |
sendData(purpose, fileToSend, null, subject, header, true);
|
|
188 |
}
|
628
|
189 |
|
587
|
190 |
/**
|
628
|
191 |
* Send xml data
|
|
192 |
*
|
|
193 |
* @param String purpose of this email
|
|
194 |
* @param String file to send
|
|
195 |
* @param String mime type
|
|
196 |
* @param String subject of email
|
|
197 |
* @param String header of mail
|
|
198 |
* @param boolean compress data if true
|
|
199 |
*/
|
|
200 |
public void sendData(String purpose, File fileToSend, String mimeType,
|
|
201 |
String subject, String header, boolean compressData) throws EmailSendException {
|
587
|
202 |
try {
|
|
203 |
log.debug("sendData:Send file: " + fileToSend + " and mimetype: " + mimeType);
|
628
|
204 |
if (fileToSend != null && fileToSend.exists()) {
|
587
|
205 |
InternetAddress[] toAddresses = getToAddressList();
|
|
206 |
Properties props = new Properties();
|
|
207 |
if (smtpServerAddress != null) {
|
|
208 |
log.debug("sendData:smtp address: " + smtpServerAddress);
|
|
209 |
props.setProperty("mail.smtp.host", smtpServerAddress);
|
|
210 |
}
|
|
211 |
Session mailSession = Session.getDefaultInstance(props, null);
|
|
212 |
MimeMessage message = new MimeMessage(mailSession);
|
628
|
213 |
message.setSubject(subject == null ? "" : subject);
|
587
|
214 |
MimeMultipart multipart = new MimeMultipart("related");
|
|
215 |
BodyPart messageBodyPart = new MimeBodyPart();
|
|
216 |
ByteArrayDataSource dataSrc = null;
|
628
|
217 |
String fileName = fileToSend.getName();
|
587
|
218 |
if (compressData) {
|
|
219 |
log.debug("Sending compressed data");
|
|
220 |
dataSrc = compressFile(fileToSend);
|
|
221 |
dataSrc.setName(fileName + ".gz");
|
|
222 |
messageBodyPart.setFileName(fileName + ".gz");
|
628
|
223 |
}
|
|
224 |
else {
|
587
|
225 |
log.debug("Sending uncompressed data:");
|
628
|
226 |
dataSrc = new ByteArrayDataSource(new FileInputStream(fileToSend), mimeType);
|
587
|
227 |
|
628
|
228 |
message.setContent(FileUtils.readFileToString(fileToSend), "text/html");
|
587
|
229 |
multipart = null;
|
|
230 |
}
|
|
231 |
String headerToSend = null;
|
|
232 |
if (header == null) {
|
|
233 |
headerToSend = "";
|
|
234 |
}
|
|
235 |
messageBodyPart.setHeader("helium-bld-data", headerToSend);
|
|
236 |
messageBodyPart.setDataHandler(new DataHandler(dataSrc));
|
|
237 |
|
|
238 |
if (multipart != null) {
|
|
239 |
multipart.addBodyPart(messageBodyPart); // add to the
|
|
240 |
// multipart
|
|
241 |
message.setContent(multipart);
|
|
242 |
}
|
|
243 |
try {
|
628
|
244 |
message.setFrom(getFromAddress());
|
|
245 |
}
|
|
246 |
catch (AddressException e) {
|
|
247 |
throw new EmailSendException("Error retrieving the from address: " + e.getMessage(), e);
|
|
248 |
} catch (LDAPException e) {
|
|
249 |
throw new EmailSendException("Error retrieving the from address: " + e.getMessage(), e);
|
587
|
250 |
}
|
|
251 |
message.addRecipients(Message.RecipientType.TO, toAddresses);
|
|
252 |
log.info("Sending email alert: " + subject);
|
|
253 |
Transport.send(message);
|
|
254 |
}
|
628
|
255 |
} catch (MessagingException e) {
|
587
|
256 |
String fullErrorMessage = "Failed sending e-mail: " + purpose;
|
628
|
257 |
if (e.getMessage() != null) {
|
|
258 |
fullErrorMessage += ": " + e.getMessage();
|
587
|
259 |
}
|
628
|
260 |
throw new EmailSendException(fullErrorMessage, e);
|
|
261 |
} catch (IOException e) {
|
587
|
262 |
String fullErrorMessage = "Failed sending e-mail: " + purpose;
|
628
|
263 |
if (e.getMessage() != null) {
|
|
264 |
fullErrorMessage += ": " + e.getMessage();
|
587
|
265 |
}
|
|
266 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
267 |
throw new EmailSendException(fullErrorMessage, e);
|
587
|
268 |
}
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* GZipping a string.
|
|
273 |
*
|
628
|
274 |
* @param data the content to be gzipped.
|
|
275 |
* @param filename the name for the file.
|
587
|
276 |
* @return a ByteArrayDataSource.
|
|
277 |
*/
|
628
|
278 |
protected ByteArrayDataSource compressFile(File fileName) throws IOException {
|
587
|
279 |
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
280 |
GZIPOutputStream gz = new GZIPOutputStream(out);
|
628
|
281 |
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));
|
587
|
282 |
byte[] dataBuffer = new byte[512];
|
|
283 |
while ((bufferedInputStream.read(dataBuffer)) != -1) {
|
|
284 |
gz.write(dataBuffer);
|
|
285 |
}
|
|
286 |
gz.close();
|
|
287 |
bufferedInputStream.close();
|
628
|
288 |
ByteArrayDataSource dataSrc = new ByteArrayDataSource(out.toByteArray(), "application/x-gzip");
|
587
|
289 |
return dataSrc;
|
|
290 |
}
|
628
|
291 |
|
587
|
292 |
/**
|
628
|
293 |
* Get sender address.
|
|
294 |
*
|
|
295 |
* @return sender address.
|
|
296 |
* @throws AddressException
|
|
297 |
* @throws LDAPException
|
|
298 |
*/
|
|
299 |
private InternetAddress getFromAddress() throws AddressException, LDAPException {
|
587
|
300 |
if (from != null) {
|
|
301 |
return new InternetAddress(from);
|
|
302 |
}
|
|
303 |
return new InternetAddress(getUserEmail());
|
|
304 |
}
|
628
|
305 |
|
587
|
306 |
/**
|
|
307 |
* Getting user email.
|
|
308 |
*
|
628
|
309 |
* @return the user email address.
|
587
|
310 |
*/
|
628
|
311 |
protected String getUserEmail() throws LDAPException {
|
|
312 |
if (ldapURL != null) {
|
|
313 |
LDAPHelper helper = new LDAPHelper(this.ldapURL, this.rootdn);
|
|
314 |
String email = helper.getUserAttributeAsString(LDAPHelper.EMAIL_ATTRIBUTE_NAME);
|
|
315 |
if (email == null) {
|
|
316 |
throw new LDAPException("Could not find email for current user. (" + System.getProperty("user.name") + ")");
|
587
|
317 |
}
|
628
|
318 |
return email;
|
587
|
319 |
}
|
628
|
320 |
throw new LDAPException("LDAP url is not defined.");
|
587
|
321 |
}
|
|
322 |
}
|