BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/mail/impl/MailPool.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.mail.impl;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.Properties;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.Session;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.turro.string.Strings;
38 import org.turro.assistant.Assistant;
39 import org.turro.assistant.AssistantSet;
40 import org.turro.assistant.Assistants;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.HeadlessApplication;
43 import org.turro.elephant.impl.abstracts.AbstractImplementation;
44 import org.turro.elephant.impl.context.ContextFactory;
45 import org.turro.elephant.impl.util.FileUtil;
46 import org.turro.elephant.impl.util.Parser;
47 import org.turro.elephant.impl.util.StringParser;
48 
53 @Deprecated
54 public class MailPool extends AbstractImplementation {
55 
56  protected Session session = null;
57  private final Date date = new Date();
58  private final Set<MimeMessage> pool = new HashSet<>();
59  private final Set<String> cssFiles = new HashSet<>();
60  private String encoding;
61 
63  public MailPool() {
64  }
65 
66  public void setEncoding(String encoding) {
67  this.encoding = encoding;
68  }
69 
70  public String getTemplateString(String attribute) {
71  return loadFromFile((String) getAttributes().get(attribute));
72  }
73 
74  public void addToPool(String from, String to, String cc, String subject, String message, String content) {
75  String poolFrom = (String) getAttributes().get("from");
76  String poolTo = (String) getAttributes().get("to");
77  String template = getTemplateString("template");
78 
79  if(to == null) {
80  to = poolTo;
81  }
82  if(to == null) {
83  return;
84  }
85  try {
86 
87  if (session == null) {
89  }
90 
91  MimeMessage msg = new MimeMessage(session);
92 
93  if (from != null) {
94  msg.setFrom(new InternetAddress(from));
95  } else if (poolFrom != null) {
96  msg.setFrom(new InternetAddress(poolFrom));
97  } else {
98  msg.setFrom();
99  }
100  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
101 
102  if (cc != null) {
103  msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
104  }
105  msg.setSubject(cleanSubject(subject), encoding);
106 
107  message = template == null ? message : template.replaceAll("\\#body", Parser.escapeGrouping(message));
108 
109  if (content == null) {
110  msg.setText(addStyles(message), encoding);
111  } else {
112  msg.setContent(addStyles(message), content + ";charset=\"" + encoding + "\"");
113  }
114  msg.setHeader("X-Mailer", "Elephant Mail System");
115  msg.setHeader("Content-Transfer-Encoding", "quoted-printable");
116  msg.setSentDate(date);
117 
118  pool.add(msg);
119 
120  Logger.getLogger(MailPool.class.getName()).log(Level.INFO, "Added mail from {0} to {1}. Subject: {2}",
121  new Object[]{InternetAddress.toString(msg.getFrom()),
122  InternetAddress.toString(msg.getAllRecipients()), msg.getSubject()});
123 
124  } catch (MessagingException mex) {
125  Logger.getLogger(MailPool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), mex);
126  }
127  }
128 
129  public void sendPool() {
130  String mailhost = (String) getAttributes().get("mailhost");
131  String user = (String) getAttributes().get("user");
132  String password = (String) getAttributes().get("password");
133 
134  if(password == null) {
135  try {
136  password = ElephantContext.decrypt((String) getAttributes().get("cryptpass"));
137  } catch (Exception ex) {
138  Logger.getLogger(MailPool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
139  }
140  }
141 
142  //new MailPoolSender(session, pool, mailhost, user, password).send();
143  }
144 
145  protected Session getMailSession() throws MessagingException {
146  String mailhost = (String) getAttributes().get("mailhost");
147  String user = (String) getAttributes().get("user");
148 
149  Session tmpSession = null;
150 
151  if (mailhost.startsWith("java:")) {
152  try {
153  InitialContext ic = new InitialContext();
154  tmpSession = (Session) ic.lookup(mailhost);
155  } catch (NamingException ex) {
156  Logger.getLogger(MailPool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
157  throw new MessagingException("Naming problem with: " + mailhost + "\n\n" + ex.getExplanation());
158  }
159  } else {
160  Properties props = System.getProperties();
161  tmpSession = Session.getDefaultInstance(props, null);
162  if (mailhost != null) {
163  props.put("mail.smtp.host", mailhost);
164  }
165  if (user != null) {
166  props.put("mail.smtp.auth", "true");
167  } else {
168  props.put("mail.smtp.auth", "false");
169  }
170  }
171 
172  return tmpSession;
173  }
174 
175  public void addCssFile(String cssFile) {
176  if(cssFiles.isEmpty()) {
177  String cssS = (String) getAttributes().get("cssS");
178  if(!Strings.isBlank(cssS)) {
179  String[] cssA = cssS.split(",");
180  for(String css1 : cssA) {
181  cssFiles.add(ElephantContext.getRealPath(css1));
182  }
183  }
184  }
185  cssFiles.add(cssFile);
186  }
187 
188  private String cleanSubject(String subject) {
189  subject = subject.replaceAll("\\<\\/?[\\?a-zA-Z\\-\\:\\_0-9]+\\ ?.*?\\>", "");
190  subject = subject.replaceAll("\\n|\\r", " ");
191  return subject;
192  }
193 
194  private String addStyles(String message) {
195  File css;
196  String style = "";
197 
198  for (String fileName : cssFiles) {
199  css = new File(fileName);
200  if (css.exists()) {
201  try {
202  BufferedReader br = new BufferedReader(new FileReader(css));
203  String l;
204  while ((l = br.readLine()) != null) {
205  style += l + "\n";
206  }
207  } catch (Exception ex) {
208  Logger.getLogger(MailPool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
209  }
210  }
211  }
212 
213  if(!Strings.isBlank(style)) {
214  style = "<style type='text/css'>\n" + style + "\n</style>\n";
215  }
216 
217  return style + "<div class='mail-body'>" + message + "</div>";
218  }
219 
220  private String loadFromFile(String file) {
221  if(!Strings.isBlank(file)) {
222  try {
223  return FileUtil.getContent(new File(ElephantContext.getRealPath(file)));
224  } catch (IOException ex) {
225  return null;
226  }
227  } else {
228  return null;
229  }
230  }
231 
232  public static void sendMailToAssistants(String path, String subject, String message, Object data) {
233  AssistantSet al = new AssistantSet();
234  Assistants.addAssistants(path, true, al, data);
235  if(!al.isEmpty()) {
238  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
239  for(Assistant a : al) {
240  mp.addToPool(null, a.email, null, subject + " : " + ElephantContext.getSiteName(),
241  StringParser.toHTML(al.getSubject() + "\n\n" + message), "text/html");
242  }
243  mp.sendPool();
244  }
245  }
246 
247  public static void sendMailToAdmin(String subject, String message) {
250  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
251  mp.addToPool(null, null, null, subject + " : " + ElephantContext.getSiteName(),
252  StringParser.toHTML(message), "text/html");
253  mp.sendPool();
254  }
255 
256  public static void sendMailToRole(String role, String subject, String message, Object data) {
257  AssistantSet al = new AssistantSet();
258  Assistants.addAssistants(role, al, data);
259  if(!al.isEmpty()) {
262  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
263  for(Assistant a : al) {
264  mp.addToPool(null, a.email, null, subject + " : " + ElephantContext.getSiteName(),
265  StringParser.toHTML(al.getSubject() + "\n\n" + message), "text/html");
266  }
267  mp.sendPool();
268  }
269  }
270 
271  public static void sendMailToAssistantsAndAdmin(String path, String subject, String message, Object data) {
274  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
275  AssistantSet al = new AssistantSet();
276  Assistants.addAssistants(path, true, al, data);
277  String to = (String) mp.getAttributes().get("to");
278  if(to != null) {
279  al.add(new Assistant(null, to, null, null));
280  }
281  if(!al.isEmpty()) {
282  for(Assistant a : al) {
283  mp.addToPool(null, a.email, null, subject + " : " + ElephantContext.getSiteName(),
284  StringParser.toHTML(al.getSubject() + "\n\n" + message), "text/html");
285  }
286  mp.sendPool();
287  }
288  }
289 
290  public static void sendMailToRoleAndAdmin(String role, String subject, String message, Object data) {
293  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
294  AssistantSet al = new AssistantSet();
295  Assistants.addAssistants(role, al, data);
296  String to = (String) mp.getAttributes().get("to");
297  if(to != null) {
298  al.add(new Assistant(null, to, null, null));
299  }
300  if(!al.isEmpty()) {
301  for(Assistant a : al) {
302  mp.addToPool(null, a.email, null, subject + " : " + ElephantContext.getSiteName(),
303  StringParser.toHTML(al.getSubject() + "\n\n" + message), "text/html");
304  }
305  mp.sendPool();
306  }
307  }
308 
309 }
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
static Object getImplementation(IElement iel, String name)
static String escapeGrouping(String sequence)
Definition: Parser.java:241
static String toHTML(String value)
void addToPool(String from, String to, String cc, String subject, String message, String content)
static void sendMailToAssistants(String path, String subject, String message, Object data)
static void sendMailToRole(String role, String subject, String message, Object data)
static void sendMailToRoleAndAdmin(String role, String subject, String message, Object data)
static void sendMailToAdmin(String subject, String message)
static void sendMailToAssistantsAndAdmin(String path, String subject, String message, Object data)