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

File MailClientService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
91% of files have more coverage

Code metrics

4
45
5
1
189
112
10
0.22
9
5
2

Classes

Class Line # Actions
MailClientService 32 45 0% 10 36
0.3333333433.3%
 

Contributing tests

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