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