1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.domain

File Mail.java

 

Coverage histogram

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

Code metrics

10
47
18
1
231
116
24
0.51
2.61
18
1.33

Classes

Class Line # Actions
Mail 15 47 0% 24 0
1.0100%
 

Contributing tests

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