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.internaldata.ant.listener;
|
|
19 |
|
|
20 |
import java.io.ByteArrayOutputStream;
|
|
21 |
import java.io.IOException;
|
628
|
22 |
import java.util.Hashtable;
|
|
23 |
import java.util.Properties;
|
|
24 |
import java.util.zip.GZIPOutputStream;
|
587
|
25 |
|
|
26 |
import javax.activation.DataHandler;
|
628
|
27 |
import javax.mail.BodyPart;
|
|
28 |
import javax.mail.Message;
|
|
29 |
import javax.mail.MessagingException;
|
|
30 |
import javax.mail.Session;
|
|
31 |
import javax.mail.Transport;
|
|
32 |
import javax.mail.internet.InternetAddress;
|
|
33 |
import javax.mail.internet.MimeBodyPart;
|
|
34 |
import javax.mail.internet.MimeMessage;
|
|
35 |
import javax.mail.internet.MimeMultipart;
|
587
|
36 |
import javax.mail.util.ByteArrayDataSource;
|
628
|
37 |
import javax.naming.Context;
|
|
38 |
import javax.naming.NamingEnumeration;
|
587
|
39 |
import javax.naming.NamingException;
|
628
|
40 |
import javax.naming.directory.DirContext;
|
|
41 |
import javax.naming.directory.InitialDirContext;
|
|
42 |
import javax.naming.directory.SearchControls;
|
|
43 |
import javax.naming.directory.SearchResult;
|
587
|
44 |
|
|
45 |
import org.apache.log4j.Logger;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Sends email of the internal data log in a zipped format.
|
|
49 |
*
|
|
50 |
*/
|
|
51 |
public class EmailDataSender {
|
|
52 |
|
|
53 |
|
|
54 |
// The target address
|
|
55 |
public static final String TO_EMAIL = "helium.internaldata@nokia.com";
|
|
56 |
|
|
57 |
// LDAP config
|
|
58 |
public static final String LDAP_URL = "ldap://nedi.europe.nokia.com:389/o=Nokia";
|
|
59 |
|
|
60 |
// Default SMTP server
|
|
61 |
public static final String SMTP_SERVER = "smtp.nokia.com";
|
|
62 |
|
|
63 |
// Configured smtp server address
|
|
64 |
private String smtpServer;
|
|
65 |
// Logger
|
|
66 |
private Logger log = Logger.getLogger(EmailDataSender.class);
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Set the smtp server address.
|
|
73 |
*/
|
|
74 |
public void setSMTPServer(String address) {
|
|
75 |
smtpServer = address;
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Get the smtp server address.
|
|
80 |
*/
|
|
81 |
public String getSMTPServer() {
|
628
|
82 |
if (smtpServer != null) {
|
587
|
83 |
return smtpServer;
|
628
|
84 |
}
|
587
|
85 |
return SMTP_SERVER;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Sending the XML data through email.
|
|
90 |
*/
|
|
91 |
public void sendData(String data) {
|
|
92 |
try {
|
|
93 |
String email = getUserEmail();
|
|
94 |
Properties props = new Properties();
|
|
95 |
props.setProperty("mail.smtp.host", getSMTPServer());
|
|
96 |
|
|
97 |
Session mailSession = Session.getDefaultInstance(props, null);
|
|
98 |
|
|
99 |
MimeMessage message = new MimeMessage(mailSession);
|
|
100 |
message.setSubject("[HELIUM]: internal data");
|
|
101 |
|
|
102 |
MimeMultipart multipart = new MimeMultipart("related");
|
|
103 |
|
|
104 |
// first part (the text html content)
|
|
105 |
BodyPart messageBodyPart = new MimeBodyPart();
|
|
106 |
String htmlText = "<H1>Internal data</H1>";
|
|
107 |
messageBodyPart.setContent(htmlText, "text/html");
|
|
108 |
multipart.addBodyPart(messageBodyPart); // add to the multipart
|
|
109 |
|
|
110 |
// second part (the data)
|
|
111 |
messageBodyPart = new MimeBodyPart();
|
|
112 |
ByteArrayDataSource dataSrc = gzip(data, "data.xml");
|
|
113 |
messageBodyPart.setFileName(dataSrc.getName());
|
|
114 |
messageBodyPart.setDataHandler(new DataHandler(dataSrc));
|
|
115 |
messageBodyPart.setHeader("Content-ID","<data>");
|
|
116 |
|
|
117 |
multipart.addBodyPart(messageBodyPart); // add to the multipart
|
|
118 |
|
|
119 |
message.setContent(multipart);
|
|
120 |
message.setFrom(new InternetAddress(email));
|
|
121 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_EMAIL));
|
|
122 |
|
|
123 |
log.debug("Sending data.");
|
|
124 |
Transport.send(message);
|
|
125 |
} catch (NamingException e) {
|
|
126 |
// We are Ignoring the errors as no need to fail the build.
|
|
127 |
log.debug("Internal data failure: NamingException " + e.getMessage(), e);
|
|
128 |
} catch (MessagingException e) {
|
|
129 |
// We are Ignoring the errors as no need to fail the build.
|
|
130 |
log.debug("Internal data failure: MessagingException " + e.getMessage(), e);
|
|
131 |
} catch (IOException e) {
|
|
132 |
// We are Ignoring the errors as no need to fail the build.
|
|
133 |
log.debug("Internal data failure: IOException " + e.getMessage(), e);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* GZipping a string.
|
|
139 |
* @param data the content to be gzipped.
|
|
140 |
* @param filename the name for the file.
|
|
141 |
* @return a ByteArrayDataSource.
|
|
142 |
*/
|
|
143 |
protected ByteArrayDataSource gzip(String data, String filename) throws IOException {
|
|
144 |
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
145 |
GZIPOutputStream gz = new GZIPOutputStream(out);
|
|
146 |
gz.write(data.getBytes());
|
|
147 |
gz.close();
|
|
148 |
out.close();
|
|
149 |
ByteArrayDataSource dataSrc = new ByteArrayDataSource(out.toByteArray(), "application/x-gzip");
|
|
150 |
dataSrc.setName(filename + ".gz");
|
|
151 |
return dataSrc;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Getting user email.
|
|
156 |
* @returns the user email.
|
|
157 |
*/
|
|
158 |
protected String getUserEmail() throws javax.naming.NamingException {
|
|
159 |
String username = System.getProperty("user.name");
|
|
160 |
|
|
161 |
// Set up environment for creating initial context
|
|
162 |
Hashtable<String, String> env = new Hashtable<String, String>(11);
|
|
163 |
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
|
164 |
"com.sun.jndi.ldap.LdapCtxFactory");
|
|
165 |
env.put(Context.PROVIDER_URL, LDAP_URL);
|
|
166 |
|
|
167 |
// Create initial context
|
|
168 |
DirContext ctx = new InitialDirContext(env);
|
|
169 |
SearchControls controls = new SearchControls();
|
|
170 |
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
171 |
NamingEnumeration<SearchResult> en = ctx.search("", "uid=" + username, controls);
|
|
172 |
if (en.hasMore()) {
|
|
173 |
SearchResult sr = (SearchResult) en.next();
|
|
174 |
if (sr.getAttributes().get("mail") != null) {
|
|
175 |
String email = (String) sr.getAttributes().get("mail").get();
|
|
176 |
return email;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
throw new NamingException("Could not find user email in LDAP.");
|
|
180 |
}
|
|
181 |
|
|
182 |
}
|