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

File MailClientService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
93% of files have more coverage

Code metrics

4
43
5
1
188
109
9
0.21
8.6
5
1.8

Classes

Class Line # Actions
MailClientService 33 43 0% 9 18
0.6538461465.4%
 

Contributing tests

This file is covered by 4 tests. .

Source view

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    * Service implementation for the JakartaMailClient.
26    *
27    * @author elmar.dott@gmail.com
28    * @version 1.0
29    * @since 1.0
30    */
31    @API(status = STABLE, since = "1.0")
32    @Service
 
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    * Constructor.
48    */
 
49  1 toggle @API(status = STABLE, since = "1.0")
50    public MailClientService() {
51  1 LOGGER.log("instance class", LogLevel.INFO);
52    }
53   
54    /**
55    * Allows to update the database configuration for the MailClient by a map
56    * of configuration entries.<br>
57    * - mailer.host<br>
58    * - mailer.port<br>
59    * - mailer.sender<br>
60    * - mailer.user<br>
61    * - mailer.password<br>
62    * - mailer.ssl<br>
63    * - mailer.tls<br>
64    * - mailer.debug<br>
65    * - mailer.count<br>
66    * - mailer.wait
67    *
68    * @param configurationList as Map
69    */
 
70  1 toggle @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    * Get the active configuration for the e-mail service and return the result
98    * as Map.
99    *
100    * @return mailConfiguration as Map
101    */
 
102  2 toggle @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    * Send an composed (single) e-mail which is configured in a Mail data
113    * class. An e-mail can be configured as followed: <br>
114    * <code>
115    * Mail mail = new Mail();
116    * mail.setSubject(subject);
117    * mail.setMessage(content);
118    * mail.addRecipent(mailAdress);
119    * </code> <br>
120    * In the case there are more recipients configured the mail will be only
121    * send to the first entry.
122    *
123    * @param mail as MailClient
124    * @throws jakarta.mail.MessagingException case of error
125    */
 
126  1 toggle @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    * Send a bulk of composed mails to a configured list of recipients. The
143    * bulk mail supports a special feature, to interrupt the sending after an
144    * defined amount of mails fo a configured time (milliseconds) until the
145    * next bulk can be send. After the termination the method return th count
146    * of the send mails. This two parameters are also part of thw whole
147    * configuration and will not set seperatly.<br>
148    * - <b>wait time:</b> default = -1 (unlimited)<br>
149    * - <b>bulk mail limiter:</b> default = -1 (unlimited)
150    *
151    * @param mail as MailClient
152    * @return sendedEmails as int
153    * @throws jakarta.mail.MessagingException case of error
154    */
 
155  1 toggle @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    //after x mails wait for n seconds
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    }