BrightSide Workbench Full Report + Source Code
elephant-mail/src/main/java/org/turro/mail/pool/MailMessagePool.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.mail.pool;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.function.Consumer;
26 import javax.mail.internet.InternetAddress;
27 import org.apache.commons.mail.EmailException;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.log.WebLoggers;
30 import org.turro.mail.message.MailMessage;
31 import org.turro.mail.message.MailMessageTemplate;
32 import org.turro.mail.provider.MailProvider;
33 import org.turro.marker.ElephantMarker;
34 
39 public class MailMessagePool {
40 
41  private final Date date = new Date();
42  private final Set<MailMessage> pool = new HashSet<>();
43 
44  public MailMessage addToPoolTemplate(String from, String to, String cc, String subject,
45  String message, String template, String name, String login) {
46  return addToPoolTemplate(from, to, cc, subject, message, template, name, login, null);
47  }
48 
49  public MailMessage addToPoolTemplate(String from, String to, String cc, String subject,
50  String message, String template, String name, String login, Map args) {
51  try {
52  if (to == null) {
53  return null;
54  }
56  mmt.setSubject(subject);
57  mmt.addTo(to);
58  if (cc != null) {
59  mmt.addCc(cc);
60  }
61  ElephantMarker em = new ElephantMarker(constructor, true);
62  em.put("email", to);
63  em.put("body", message);
64  if(args != null) {
65  em.putAll(args);
66  }
67  mmt.setMessage(em, template, to, name);
68  return addToPool(mmt);
69  } catch (EmailException ex) {
70  WebLoggers.severe(this).exception(ex).log();
71  }
72  return null;
73  }
74 
75  public MailMessage addToPool(String from, String to, String cc, String subject, String message) {
76  try {
77  if (to == null) {
78  return null;
79  }
80  MailMessage mail = MailMessage.of(provider);
81  mail.addTo(to);
82  if (cc != null) {
83  mail.addCc(cc);
84  }
85  mail.setSubject(subject);
86  mail.setMessage(message);
87  return addToPool(mail);
88  } catch (EmailException ex) {
89  WebLoggers.severe(this).exception(ex).log();
90  }
91  return null;
92  }
93 
94  public MailMessage addToPool(MailMessage mail) throws EmailException {
95  try {
96  mail.addHeader("X-Mailer", "Elephant Mail System");
97  mail.setSentDate(date);
98  mail.setSubject(cleanSubject(mail.getSubject()));
99 
100  pool.add(mail);
101 
102  WebLoggers.info(this).message("Added mail from %s to %s. Subject: %s",
103  mail.getProvider().getFrom().getMail(),
104  InternetAddress.toString((InternetAddress[]) mail.getEmail().getToAddresses().toArray(new InternetAddress[0])),
105  mail.getSubject()
106  ).log();
107 
108  return mail;
109  } catch(Exception ex) {
110  WebLoggers.severe(this).exception(ex).log();
111  }
112  return null;
113  }
114 
115  public void sendPool() {
116  sendPool(null);
117  }
118 
119  public void sendPool(Consumer onFinish) {
120  MailQueueConsumer.getInstance().addToQueue(pool, onFinish);
121  pool.clear();
122  }
123 
125  return provider;
126  }
127 
128  private String cleanSubject(String subject) {
129  subject = subject.replaceAll("\\<\\/?[\\?a-zA-Z\\-\\:\\_0-9]+\\ ?.*?\\>", "");
130  subject = subject.replaceAll("\\n|\\r", " ");
131  return subject;
132  }
133 
134  /* Factory */
135 
136  private final IConstructor constructor;
137  private final MailProvider provider;
138 
139  public static MailMessagePool of(IConstructor constructor, MailProvider provider) {
140  return new MailMessagePool(constructor, provider);
141  }
142 
143  public MailMessagePool(IConstructor constructor, MailProvider provider) {
144  this.constructor = constructor;
145  this.provider = provider;
146  }
147 
148 }
149 
WebLoggers message(String text, Object... parameters)
Definition: WebLoggers.java:34
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43
static MailMessagePool of(IConstructor constructor, MailProvider provider)
MailMessage addToPoolTemplate(String from, String to, String cc, String subject, String message, String template, String name, String login, Map args)
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)
static MailQueueConsumer getInstance()
void addToQueue(Collection< MailMessage > pool)
Object put(Object key, Object value)