BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/mail/impl/MailMessagePool.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.util.Date;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.function.Consumer;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.mail.internet.InternetAddress;
28 import org.apache.commons.mail.EmailException;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.abstracts.AbstractImplementation;
31 import org.turro.marker.ElephantMarker;
32 
37 @Deprecated
39 
40  private final Date date = new Date();
41  private final Set<MailMessage> pool = new HashSet<>();
42  private String encoding;
43 
45  public MailMessagePool() {
46  }
47 
48  public void setEncoding(String encoding) {
49  this.encoding = encoding;
50  }
51 
52  public MailMessage addToPoolTemplate(String from, String to, String cc, String subject,
53  String message, String template, String name, String login) {
54  return addToPoolTemplate(from, to, cc, subject, message, template, name, login, null);
55  }
56 
57  public MailMessage addToPoolTemplate(String from, String to, String cc, String subject,
58  String message, String template, String name, String login, Map args) {
59  try {
60  if (to == null) {
61  return null;
62  }
64  mmt.setCharset(encoding);
65  mmt.setSubject(subject);
66  mmt.setFrom(from == null ? (String) getAttributes().get("from") : from);
67  mmt.addTo(to);
68  if (cc != null) {
69  mmt.addCc(cc);
70  }
72  em.put("email", to);
73  em.put("body", message);
74  if(args != null) {
75  em.putAll(args);
76  }
77  mmt.setMessage(em, template, to, name);
78  return addToPool(mmt);
79  } catch (EmailException ex) {
80  Logger.getLogger(MailMessagePool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
81  }
82  return null;
83  }
84 
85  public MailMessage addToPool(String from, String to, String cc, String subject, String message) {
86  try {
87  if (to == null) {
88  return null;
89  }
90  MailMessage mail = new MailMessage();
91  mail.setCharset(encoding);
92  mail.setFrom(from == null ? (String) getAttributes().get("from") : from);
93  mail.addTo(to);
94  if (cc != null) {
95  mail.addCc(cc);
96  }
97  mail.setSubject(subject);
98  mail.setMessage(message);
99  return addToPool(mail);
100  } catch (EmailException ex) {
101  Logger.getLogger(MailMessagePool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  return null;
104  }
105 
106  public MailMessage addToPool(MailMessage mail) throws EmailException {
107  try {
108  mail.addHeader("X-Mailer", "Elephant Mail System");
109  mail.setSentDate(date);
110  mail.setHostName((String) getAttributes().get("mailhost"));
111  mail.setPort((String) getAttributes().get("port"));
112  mail.setTls((String) getAttributes().get("tls"));
113  mail.setSsl((String) getAttributes().get("ssl"));
114  mail.setUser((String) getAttributes().get("user"));
115  String password = (String) getAttributes().get("password");
116  if(password == null) {
117  password = ElephantContext.decrypt((String) getAttributes().get("cryptpass"));
118  }
119  mail.setPassword(password);
120  mail.setFromIfNull((String) getAttributes().get("from"));
121  mail.setSubject(cleanSubject(mail.getSubject()));
122 
123  pool.add(mail);
124 
125  Logger.getLogger(MailMessagePool.class.getName()).log(Level.INFO, "Added mail from {0} to {1}. Subject: {2}",
126  new Object[]{mail.getEmail().getFromAddress().getAddress(),
127  InternetAddress.toString((InternetAddress[]) mail.getEmail().getToAddresses().toArray(new InternetAddress[0])), mail.getSubject()});
128 
129  return mail;
130  } catch(Exception ex) {
131  Logger.getLogger(MailMessagePool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
132  }
133  return null;
134  }
135 
136  public void sendPool() {
137  sendPool(null);
138  }
139 
140  public void sendPool(Consumer onFinish) {
141  //MailQueueConsumer.getInstance().addToQueue(pool, onFinish);
142  //new MailMessagePoolSender(pool).send(onFinish);
143  pool.clear();
144  }
145 
146  private String cleanSubject(String subject) {
147  subject = subject.replaceAll("\\<\\/?[\\?a-zA-Z\\-\\:\\_0-9]+\\ ?.*?\\>", "");
148  subject = subject.replaceAll("\\n|\\r", " ");
149  return subject;
150  }
151 
152 }
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessage addToPoolTemplate(String from, String to, String cc, String subject, String message, String template, String name, String login)
MailMessage addToPoolTemplate(String from, String to, String cc, String subject, String message, String template, String name, String login, Map args)
Object put(Object key, Object value)