1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.domain

File Mail.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
54
20
1
261
135
26
0.48
2.7
20
1.3

Classes

Class Line # Actions
Mail 15 54 0% 26 0
1.0100%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    package org.europa.together.domain;
2   
3    import java.util.ArrayList;
4    import java.util.List;
5    import jakarta.activation.FileDataSource;
6    import jakarta.mail.internet.AddressException;
7    import jakarta.mail.internet.InternetAddress;
8    import org.europa.together.application.LogbackLogger;
9    import org.europa.together.business.Logger;
10    import org.europa.together.utils.Validator;
11   
12    /**
13    * Compose E-Mail.
14    */
 
15    public class Mail {
16   
17    private static final Logger LOGGER = new LogbackLogger(Mail.class);
18   
19    private String subject;
20    private String message;
21    private String mimeType;
22    private long attachmentSize;
23    private List<FileDataSource> attachmentList;
24    private List<InternetAddress> recipientList;
25   
26    /**
27    * default constructor.
28    */
 
29  21 toggle public Mail() {
30  21 mimeType = "plain";
31  21 attachmentSize = -1;
32  21 attachmentList = new ArrayList<>();
33  21 recipientList = new ArrayList<>();
34    }
35   
36    /**
37    * Copy constructor.
38    *
39    * @param mail as Mail
40    */
 
41  7 toggle public Mail(final Mail mail) {
42  7 mimeType = mail.getMimeType();
43  7 attachmentList = mail.getAttachmentList();
44  7 attachmentSize = -1;
45  7 recipientList = mail.getRecipentList();
46  7 subject = mail.getSubject();
47  7 message = mail.getMessage();
48    }
49   
50    /**
51    * Add recipients from a List of Strings (E-Mail Address).
52    *
53    * @param recipients as List.
54    * @throws jakarta.mail.internet.AddressException case of error
55    */
 
56  1 toggle public void addRecipientList(final List<String> recipients)
57    throws AddressException {
58  1 for (String recipient : recipients) {
59  5 addRecipent(recipient);
60    }
61    }
62   
63    /**
64    * Add attachments from a list of Strings (resource path).
65    *
66    * @param attachments as List
67    */
 
68  1 toggle public void addAttachmentList(final List<String> attachments) {
69  1 for (String attachment : attachments) {
70  2 addAttachment(attachment);
71    }
72    }
73   
74    /**
75    * Return the whole list of attachments - FileDataSource.
76    *
77    * @return attachments as List
78    */
 
79  23 toggle public List<FileDataSource> getAttachmentList() {
80  23 return List.copyOf(attachmentList);
81    }
82   
83    /**
84    * Add an attachment to the attachment list. The file size of attachments
85    * can be limited. To refer an attachment, set the resource e.g.:
86    * picture.png
87    *
88    * @param resource as String
89    * @return true on success
90    */
 
91  10 toggle public boolean addAttachment(final String resource) {
92  10 boolean success = false;
93  10 FileDataSource file = new FileDataSource(resource);
94  10 if (file.getFile().exists()) {
95  9 long size = file.getFile().length();
96  9 if (size > 0) {
97  8 if (attachmentSize >= size || attachmentSize == -1) {
98  7 attachmentList.add(file);
99  7 success = true;
100  7 LOGGER.log(file.getName() + " added. (" + size + ") bytes", LogLevel.DEBUG);
101    } else {
102  1 long difference = size - attachmentSize;
103  1 LOGGER.log("Filesize is " + difference + " bigger than allowed.",
104    LogLevel.WARN);
105    }
106    } else {
107  1 LOGGER.log("File is empty.", LogLevel.WARN);
108    }
109    } else {
110  1 LOGGER.log("File " + resource + " don't exist.", LogLevel.ERROR);
111    }
112  10 return success;
113    }
114   
115    /**
116    * Reset the attachment list.
117    */
 
118  1 toggle public void clearAttachments() {
119  1 attachmentList.clear();
120    }
121   
122    /**
123    * Get the whole list of recipients - InternetAdress.
124    *
125    * @return recipients as List
126    */
 
127  16 toggle public List<InternetAddress> getRecipentList() {
128  16 return List.copyOf(recipientList);
129    }
130   
131    /**
132    * Add an recipient to the recipient l.The implementation check if the
133    * recipient already exist in the list.Also the format of an valid e-mail
134    * address will be tested. If an given e-mail address is not valid it will
135    * not added to the list.
136    *
137    * @param recipient as String
138    * @return true on success
139    * @throws jakarta.mail.internet.AddressException case of error
140    */
 
141  25 toggle public boolean addRecipent(final String recipient)
142    throws AddressException {
143  25 boolean success = false;
144  25 InternetAddress mailAdress;
145  25 if (!Validator.validate(recipient, Validator.E_MAIL_ADDRESS)) {
146  4 throw new AddressException("[" + recipient + "] is not a valid email Adress.");
147    }
148  21 mailAdress = new InternetAddress(recipient);
149  21 mailAdress.validate();
150   
151    //detect duplicate entries
152  21 if (recipientList.contains(mailAdress)) {
153  3 LOGGER.log("Address " + recipient + " already exist and will be ignored.",
154    LogLevel.WARN);
155    } else {
156  18 recipientList.add(mailAdress);
157  18 success = true;
158  18 LOGGER.log("Add " + recipient + " to the recipient list.", LogLevel.DEBUG);
159    }
160  21 return success;
161    }
162   
163    /**
164    * Reset the Recipient List.
165    */
 
166  1 toggle public void clearRecipents() {
167  1 recipientList.clear();
168    }
169   
170    /**
171    * Set the MimeType of a E-Mail to HTML.
172    */
 
173  3 toggle public void setMimeTypeToHTML() {
174  3 mimeType = "html";
175    }
176   
177    /**
178    * Set the MimeType of an E-Mail to plain text.
179    */
 
180  1 toggle public void setMimeTypeToText() {
181  1 mimeType = "plain";
182    }
183   
184    /**
185    * Get the MimeTYpe of the E-Mail.
186    *
187    * @return mimeType as String
188    */
 
189  16 toggle public String getMimeType() {
190  16 return mimeType;
191    }
192   
193    //<editor-fold defaultstate="collapsed" desc="Getter & Setter">
194    /**
195    * Get the subject of a e-mail.
196    *
197    * @return subject as String
198    */
 
199  14 toggle public String getSubject() {
200  14 return subject;
201    }
202   
203    /**
204    * Add a subject (topic) to the mail.
205    *
206    * @param subject as String
207    */
 
208  7 toggle public void setSubject(final String subject) {
209  7 this.subject = subject;
210    }
211   
212    /**
213    * Get the content of a message.
214    *
215    * @return content as String
216    */
 
217  14 toggle public String getMessage() {
218  14 return message;
219    }
220   
221    /**
222    * Add e-mail content from a String.
223    *
224    * @param message as String
225    */
 
226  7 toggle public void setMessage(final String message) {
227  7 this.message = message;
228    }
229   
230    /**
231    * Get the limitation for the filesize of attachments. 0 = unlimited.
232    *
233    * @return attachmentSize as long
234    */
 
235  2 toggle public long getAttachmentSize() {
236  2 return attachmentSize;
237    }
238   
239    /**
240    * Set a limiter to the filesize for attachments. Default = 0, unlimited.
241    *
242    * @param attachmentSize as long
243    */
 
244  2 toggle public void setAttachmentSize(final long attachmentSize) {
245  2 this.attachmentSize = attachmentSize;
246    }
247    //</editor-fold>
248   
 
249  2 toggle @Override
250    public String toString() {
251  2 return "Mail{"
252    + "subject=" + subject
253    + ", message=" + message
254    + ", mimeType=" + mimeType
255    + ", attachmentSize=" + attachmentSize
256    + ", attachmentList=" + attachmentList
257    + ", recipientList=" + recipientList
258    + '}';
259    }
260   
261    }