BrightSide Workbench Full Report + Source Code
MailProvider.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.provider;
20 
21 import java.util.Objects;
22 import java.util.Set;
23 import javax.naming.NamingException;
24 import org.apache.commons.mail.EmailException;
25 import org.apache.commons.mail.HtmlEmail;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.string.Strings;
28 import org.turro.util.Comparison;
29 
34 public class MailProvider implements Comparable<MailProvider> {
35 
36  private String id;
37  private String hostname;
38  private Integer port;
39  private boolean tls, ssl;
40  private String user, password;
41  private MailRecipient from;
42  private Set<MailRecipient> replies;
43  private Set<MailRecipient> ccs;
44  private Set<MailRecipient> bccs;
45 
46  public String getId() {
47  return id;
48  }
49 
50  public void setId(String id) {
51  this.id = id;
52  }
53 
54  public String getHostname() {
55  return hostname;
56  }
57 
58  public void setHostname(String hostname) {
59  this.hostname = hostname;
60  }
61 
62  public Integer getPort() {
63  return port;
64  }
65 
66  public void setPort(Integer port) {
67  this.port = port;
68  }
69 
70  public boolean isTls() {
71  return tls;
72  }
73 
74  public void setTls(boolean tls) {
75  this.tls = tls;
76  }
77 
78  public boolean isSsl() {
79  return ssl;
80  }
81 
82  public void setSsl(boolean ssl) {
83  this.ssl = ssl;
84  }
85 
86  public String getUser() {
87  return user;
88  }
89 
90  public void setUser(String user) {
91  this.user = user;
92  }
93 
94  public String getPassword() {
95  return password;
96  }
97 
98  public void setPassword(String password) {
99  this.password = password;
100  }
101 
103  return from;
104  }
105 
106  public void setFrom(MailRecipient from) {
107  this.from = from;
108  }
109 
110  public Set<MailRecipient> getReplies() {
111  return replies;
112  }
113 
114  public void setReplies(Set<MailRecipient> replies) {
115  this.replies = replies;
116  }
117 
118  public Set<MailRecipient> getCcs() {
119  return ccs;
120  }
121 
122  public void setCcs(Set<MailRecipient> ccs) {
123  this.ccs = ccs;
124  }
125 
126  public Set<MailRecipient> getBccs() {
127  return bccs;
128  }
129 
130  public void setBccs(Set<MailRecipient> bccs) {
131  this.bccs = bccs;
132  }
133 
134  /* Mail */
135 
136  public void setupMail(HtmlEmail email) throws NamingException, EmailException {
137  if (hostname.startsWith("java:")) {
138  email.setMailSessionFromJNDI(hostname);
139  } else {
140  email.setHostName(hostname);
141  }
142  if(port != null) email.setSmtpPort(port);
143  if(ssl) {
144  email.setSSLOnConnect(true);
145  if(port != null) email.setSslSmtpPort(String.valueOf(port));
146  }
147  if(tls) email.setStartTLSEnabled(true);
148  if(!Strings.isBlank(user) && !Strings.isBlank(password)) {
149  email.setAuthentication(user, ElephantContext.decrypt(password));
150  }
151  if(email.getFromAddress() == null) {
152  email.setFrom(from.getMail(), from.getName(), ElephantContext.getEncoding());
153  }
154  if(replies != null) for(MailRecipient reply : replies) {
155  email.addReplyTo(reply.getMail(), reply.getName(), ElephantContext.getEncoding());
156  }
157  if(ccs != null) for(MailRecipient cc : ccs) {
158  email.addCc(cc.getMail(), cc.getName(), ElephantContext.getEncoding());
159  }
160  if(bccs != null) for(MailRecipient bcc : bccs) {
161  email.addCc(bcc.getMail(), bcc.getName(), ElephantContext.getEncoding());
162  }
163  }
164 
165  /* Comparable */
166 
167  @Override
168  public int compareTo(MailProvider o) {
169  return Comparison.ascendant().compare(id, o.id).get();
170  }
171 
172  /* Utils */
173 
174  public boolean isEmpty() {
175  return Strings.isBlank(id) || Strings.isBlank(hostname) || from.isEmpty();
176  }
177 
178  public void clearEmpties() {
179  id = Strings.nullIfBlank(id);
180  hostname = Strings.nullIfBlank(hostname);
181  user = Strings.nullIfBlank(user);
182  password = Strings.nullIfBlank(password);
183  from = from.isEmpty() ? null : from;
184  if(replies != null) replies.removeIf(r -> r.isEmpty());
185  if(ccs != null) ccs.removeIf(c -> c.isEmpty());
186  if(bccs != null) bccs.removeIf(b -> b.isEmpty());
187  }
188 
189  @Override
190  public int hashCode() {
191  int hash = 7;
192  hash = 67 * hash + Objects.hashCode(this.id);
193  return hash;
194  }
195 
196  @Override
197  public boolean equals(Object obj) {
198  if (this == obj) {
199  return true;
200  }
201  if (obj == null) {
202  return false;
203  }
204  if (getClass() != obj.getClass()) {
205  return false;
206  }
207  final MailProvider other = (MailProvider) obj;
208  return Objects.equals(this.id, other.id);
209  }
210 
211 }
void setCcs(Set< MailRecipient > ccs)
void setFrom(MailRecipient from)
void setBccs(Set< MailRecipient > bccs)
void setReplies(Set< MailRecipient > replies)