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