| 1 |
|
package org.europa.together.service; |
| 2 |
|
|
| 3 |
|
import java.util.List; |
| 4 |
|
import java.util.Map; |
| 5 |
|
import java.util.concurrent.TimeUnit; |
| 6 |
|
import jakarta.mail.Address; |
| 7 |
|
import jakarta.mail.MessagingException; |
| 8 |
|
import jakarta.mail.Transport; |
| 9 |
|
import org.apiguardian.api.API; |
| 10 |
|
import static org.apiguardian.api.API.Status.STABLE; |
| 11 |
|
import org.europa.together.application.LogbackLogger; |
| 12 |
|
import org.europa.together.business.ConfigurationDAO; |
| 13 |
|
import org.europa.together.business.Logger; |
| 14 |
|
import org.europa.together.business.MailClient; |
| 15 |
|
import org.europa.together.domain.ConfigurationDO; |
| 16 |
|
import org.europa.together.domain.HashAlgorithm; |
| 17 |
|
import org.europa.together.domain.LogLevel; |
| 18 |
|
import org.europa.together.utils.Constraints; |
| 19 |
|
import org.europa.together.business.CryptoTools; |
| 20 |
|
import org.europa.together.domain.Mail; |
| 21 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 22 |
|
import org.springframework.stereotype.Service; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
@author |
| 28 |
|
@version |
| 29 |
|
@since |
| 30 |
|
|
| 31 |
|
@API(status = STABLE, since = "1.0") |
| 32 |
|
@Service |
| |
|
| 65.4% |
Uncovered Elements: 18 (52) |
Complexity: 9 |
Complexity Density: 0.21 |
|
| 33 |
|
public final class MailClientService { |
| 34 |
|
|
| 35 |
|
private static final long serialVersionUID = 206L; |
| 36 |
|
private static final Logger LOGGER = new LogbackLogger(MailClientService.class); |
| 37 |
|
private String configurationFile = ""; |
| 38 |
|
|
| 39 |
|
@Autowired |
| 40 |
|
private CryptoTools cryptoTools; |
| 41 |
|
@Autowired |
| 42 |
|
private ConfigurationDAO configurationDAO; |
| 43 |
|
@Autowired |
| 44 |
|
private MailClient mailClient; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 49 |
1 |
@API(status = STABLE, since = "1.0")... |
| 50 |
|
public MailClientService() { |
| 51 |
1 |
LOGGER.log("instance class", LogLevel.INFO); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@param |
| 69 |
|
|
| |
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 70 |
1 |
@API(status = STABLE, since = "3.0")... |
| 71 |
|
public void updateConfiguration( |
| 72 |
|
final Map<String, String> configurationList) { |
| 73 |
|
|
| 74 |
1 |
mailClient.clearConfiguration(); |
| 75 |
1 |
try { |
| 76 |
1 |
List<ConfigurationDO> configurationEntries |
| 77 |
|
= configurationDAO.getAllConfigurationSetEntries( |
| 78 |
|
Constraints.MODULE_NAME, |
| 79 |
|
MailClient.CONFIG_VERSION, |
| 80 |
|
MailClient.CONFIG_SET); |
| 81 |
1 |
for (ConfigurationDO configEntry : configurationEntries) { |
| 82 |
|
|
| 83 |
10 |
for (Map.Entry<String, String> entry : configurationList.entrySet()) { |
| 84 |
10 |
if (configEntry.getKey().equals( |
| 85 |
|
cryptoTools.calculateHash(entry.getKey(), HashAlgorithm.SHA256))) { |
| 86 |
1 |
configEntry.setValue(entry.getValue()); |
| 87 |
1 |
configurationDAO.update(configEntry.getUuid(), configEntry); |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
} catch (Exception ex) { |
| 92 |
0 |
LOGGER.catchException(ex); |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
@return |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 102 |
2 |
@API(status = STABLE, since = "3.0")... |
| 103 |
|
public Map<String, String> loadConfiguration() { |
| 104 |
2 |
mailClient.loadConfigurationFromDatabase(); |
| 105 |
2 |
Map<String, String> config |
| 106 |
|
= mailClient.getDebugActiveConfiguration(); |
| 107 |
2 |
LOGGER.log("CONFIG: " + config, LogLevel.DEBUG); |
| 108 |
2 |
return config; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
@param |
| 124 |
|
@throws |
| 125 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 126 |
1 |
@API(status = STABLE, since = "1.0")... |
| 127 |
|
public void sendEmail(final Mail mail) throws MessagingException { |
| 128 |
1 |
mailClient.loadConfigurationFromDatabase(); |
| 129 |
1 |
mailClient.composeMail(mail); |
| 130 |
|
|
| 131 |
1 |
Address[] address = new Address[1]; |
| 132 |
1 |
address[0] = (Address) mail.getRecipentList().get(0); |
| 133 |
|
|
| 134 |
1 |
Transport postman = mailClient.getSession().getTransport(); |
| 135 |
1 |
postman.connect(); |
| 136 |
0 |
postman.sendMessage(mailClient.getMimeMessage(), address); |
| 137 |
0 |
postman.close(); |
| 138 |
0 |
LOGGER.log("E-Mail should send.", LogLevel.TRACE); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
@param |
| 152 |
|
@return |
| 153 |
|
@throws |
| 154 |
|
|
| |
|
| 36.4% |
Uncovered Elements: 14 (22) |
Complexity: 3 |
Complexity Density: 0.15 |
|
| 155 |
1 |
@API(status = STABLE, since = "1.0")... |
| 156 |
|
public int sendBulkMail(final Mail mail) throws MessagingException { |
| 157 |
1 |
int countSendedMails = 0; |
| 158 |
1 |
try { |
| 159 |
1 |
mailClient.loadConfigurationFromDatabase(); |
| 160 |
1 |
mailClient.composeMail(mail); |
| 161 |
|
|
| 162 |
1 |
int maximumMailBulk = mailClient.getBulkMailLimiter(); |
| 163 |
1 |
long countWaitTime = mailClient.getWaitTime(); |
| 164 |
1 |
Transport postman = mailClient.getSession().getTransport(); |
| 165 |
1 |
postman.connect(); |
| 166 |
|
|
| 167 |
0 |
for (Object recipient : mail.getRecipentList()) { |
| 168 |
0 |
Address[] address = new Address[1]; |
| 169 |
0 |
address[0] = (Address) recipient; |
| 170 |
0 |
countSendedMails++; |
| 171 |
|
|
| 172 |
0 |
if (countSendedMails % maximumMailBulk == 0) { |
| 173 |
0 |
TimeUnit.SECONDS.sleep(countWaitTime); |
| 174 |
|
|
| 175 |
0 |
LOGGER.log("Timer wait for " + countWaitTime + " seconds.", |
| 176 |
|
LogLevel.DEBUG); |
| 177 |
|
} |
| 178 |
0 |
Transport.send(mailClient.getMimeMessage(), address); |
| 179 |
|
} |
| 180 |
0 |
postman.close(); |
| 181 |
|
|
| 182 |
|
} catch (InterruptedException ex) { |
| 183 |
0 |
LOGGER.catchException(ex); |
| 184 |
|
} |
| 185 |
0 |
LOGGER.log(countSendedMails + " E-Mails should sended", LogLevel.DEBUG); |
| 186 |
0 |
return countSendedMails; |
| 187 |
|
} |
| 188 |
|
} |