| 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 | public Mail() { | |
| 30 | mimeType = "plain"; | |
| 31 | attachmentSize = -1; | |
| 32 | attachmentList = new ArrayList<>(); | |
| 33 | recipientList = new ArrayList<>(); | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Copy constructor. | |
| 38 | * | |
| 39 | * @param mail as Mail | |
| 40 | */ | |
| 41 | public Mail(final Mail mail) { | |
| 42 | mimeType = mail.getMimeType(); | |
| 43 | attachmentList = mail.getAttachmentList(); | |
| 44 | attachmentSize = -1; | |
| 45 | recipientList = mail.getRecipentList(); | |
| 46 | subject = mail.getSubject(); | |
| 47 | 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 | public void addRecipientList(final List<String> recipients) | |
| 57 | throws AddressException { | |
| 58 | for (String recipient : recipients) { | |
| 59 | addRecipent(recipient); | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Add attachments from a list of Strings (resource path). | |
| 65 | * | |
| 66 | * @param attachments as List | |
| 67 | */ | |
| 68 | public void addAttachmentList(final List<String> attachments) { | |
| 69 | for (String attachment : attachments) { | |
| 70 | addAttachment(attachment); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Return the whole list of attachments - FileDataSource. | |
| 76 | * | |
| 77 | * @return attachments as List | |
| 78 | */ | |
| 79 | public List<FileDataSource> getAttachmentList() { | |
| 80 |
1
1. getAttachmentList : replaced return value with Collections.emptyList for org/europa/together/domain/Mail::getAttachmentList → KILLED |
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 | public boolean addAttachment(final String resource) { | |
| 92 | boolean success = false; | |
| 93 | FileDataSource file = new FileDataSource(resource); | |
| 94 |
1
1. addAttachment : negated conditional → KILLED |
if (file.getFile().exists()) { |
| 95 | long size = file.getFile().length(); | |
| 96 |
2
1. addAttachment : changed conditional boundary → KILLED 2. addAttachment : negated conditional → KILLED |
if (size > 0) { |
| 97 |
3
1. addAttachment : changed conditional boundary → SURVIVED 2. addAttachment : negated conditional → KILLED 3. addAttachment : negated conditional → KILLED |
if (attachmentSize >= size || attachmentSize == -1) { |
| 98 | attachmentList.add(file); | |
| 99 | success = true; | |
| 100 | LOGGER.log(file.getName() + " added. (" + size + ") bytes", LogLevel.DEBUG); | |
| 101 | } else { | |
| 102 |
1
1. addAttachment : Replaced long subtraction with addition → SURVIVED |
long difference = size - attachmentSize; |
| 103 | LOGGER.log("Filesize is " + difference + " bigger than allowed.", | |
| 104 | LogLevel.WARN); | |
| 105 | } | |
| 106 | } else { | |
| 107 | LOGGER.log("File is empty.", LogLevel.WARN); | |
| 108 | } | |
| 109 | } else { | |
| 110 | LOGGER.log("File " + resource + " don't exist.", LogLevel.ERROR); | |
| 111 | } | |
| 112 |
2
1. addAttachment : replaced boolean return with true for org/europa/together/domain/Mail::addAttachment → KILLED 2. addAttachment : replaced boolean return with false for org/europa/together/domain/Mail::addAttachment → KILLED |
return success; |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Reset the attachment list. | |
| 117 | */ | |
| 118 | public void clearAttachments() { | |
| 119 |
1
1. clearAttachments : removed call to java/util/List::clear → KILLED |
attachmentList.clear(); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Get the whole list of recipients - InternetAdress. | |
| 124 | * | |
| 125 | * @return recipients as List | |
| 126 | */ | |
| 127 | public List<InternetAddress> getRecipentList() { | |
| 128 |
1
1. getRecipentList : replaced return value with Collections.emptyList for org/europa/together/domain/Mail::getRecipentList → KILLED |
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 | public boolean addRecipent(final String recipient) | |
| 142 | throws AddressException { | |
| 143 | boolean success = false; | |
| 144 | InternetAddress mailAdress; | |
| 145 |
1
1. addRecipent : negated conditional → KILLED |
if (!Validator.validate(recipient, Validator.E_MAIL_ADDRESS)) { |
| 146 | throw new AddressException("[" + recipient + "] is not a valid email Adress."); | |
| 147 | } | |
| 148 | mailAdress = new InternetAddress(recipient); | |
| 149 |
1
1. addRecipent : removed call to jakarta/mail/internet/InternetAddress::validate → SURVIVED |
mailAdress.validate(); |
| 150 | ||
| 151 | //detect duplicate entries | |
| 152 |
1
1. addRecipent : negated conditional → KILLED |
if (recipientList.contains(mailAdress)) { |
| 153 | LOGGER.log("Address " + recipient + " already exist and will be ignored.", | |
| 154 | LogLevel.WARN); | |
| 155 | } else { | |
| 156 | recipientList.add(mailAdress); | |
| 157 | success = true; | |
| 158 | LOGGER.log("Add " + recipient + " to the recipient list.", LogLevel.DEBUG); | |
| 159 | } | |
| 160 |
2
1. addRecipent : replaced boolean return with false for org/europa/together/domain/Mail::addRecipent → SURVIVED 2. addRecipent : replaced boolean return with true for org/europa/together/domain/Mail::addRecipent → SURVIVED |
return success; |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * Reset the Recipient List. | |
| 165 | */ | |
| 166 | public void clearRecipents() { | |
| 167 |
1
1. clearRecipents : removed call to java/util/List::clear → KILLED |
recipientList.clear(); |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Set the MimeType of a E-Mail to HTML. | |
| 172 | */ | |
| 173 | public void setMimeTypeToHTML() { | |
| 174 | mimeType = "html"; | |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Set the MimeType of an E-Mail to plain text. | |
| 179 | */ | |
| 180 | public void setMimeTypeToText() { | |
| 181 | mimeType = "plain"; | |
| 182 | } | |
| 183 | ||
| 184 | /** | |
| 185 | * Get the MimeTYpe of the E-Mail. | |
| 186 | * | |
| 187 | * @return mimeType as String | |
| 188 | */ | |
| 189 | public String getMimeType() { | |
| 190 |
1
1. getMimeType : replaced return value with "" for org/europa/together/domain/Mail::getMimeType → KILLED |
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 | public String getSubject() { | |
| 200 |
1
1. getSubject : replaced return value with "" for org/europa/together/domain/Mail::getSubject → KILLED |
return subject; |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * Add a subject (topic) to the mail. | |
| 205 | * | |
| 206 | * @param subject as String | |
| 207 | */ | |
| 208 | public void setSubject(final String subject) { | |
| 209 | this.subject = subject; | |
| 210 | } | |
| 211 | ||
| 212 | /** | |
| 213 | * Get the content of a message. | |
| 214 | * | |
| 215 | * @return content as String | |
| 216 | */ | |
| 217 | public String getMessage() { | |
| 218 |
1
1. getMessage : replaced return value with "" for org/europa/together/domain/Mail::getMessage → KILLED |
return message; |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * Add e-mail content from a String. | |
| 223 | * | |
| 224 | * @param message as String | |
| 225 | */ | |
| 226 | public void setMessage(final String message) { | |
| 227 | 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 | public long getAttachmentSize() { | |
| 236 |
1
1. getAttachmentSize : replaced long return with 0 for org/europa/together/domain/Mail::getAttachmentSize → KILLED |
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 | public void setAttachmentSize(final long attachmentSize) { | |
| 245 | this.attachmentSize = attachmentSize; | |
| 246 | } | |
| 247 | //</editor-fold> | |
| 248 | ||
| 249 | @Override | |
| 250 | public String toString() { | |
| 251 |
1
1. toString : replaced return value with "" for org/europa/together/domain/Mail::toString → SURVIVED |
return "Mail{" |
| 252 | + "subject=" + subject | |
| 253 | + ", message=" + message | |
| 254 | + ", mimeType=" + mimeType | |
| 255 | + ", attachmentSize=" + attachmentSize | |
| 256 | + ", attachmentList=" + attachmentList | |
| 257 | + ", recipientList=" + recipientList | |
| 258 | + '}'; | |
| 259 | } | |
| 260 | ||
| 261 | } | |
Mutations | ||
| 80 |
1.1 |
|
| 94 |
1.1 |
|
| 96 |
1.1 2.2 |
|
| 97 |
1.1 2.2 3.3 |
|
| 102 |
1.1 |
|
| 112 |
1.1 2.2 |
|
| 119 |
1.1 |
|
| 128 |
1.1 |
|
| 145 |
1.1 |
|
| 149 |
1.1 |
|
| 152 |
1.1 |
|
| 160 |
1.1 2.2 |
|
| 167 |
1.1 |
|
| 190 |
1.1 |
|
| 200 |
1.1 |
|
| 218 |
1.1 |
|
| 236 |
1.1 |
|
| 251 |
1.1 |