1
|
/**
|
2
|
* Copyright (C) 2021 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.config;
|
10
|
|
11
|
import java.util.Properties;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.context.annotation.Bean;
|
16
|
import org.springframework.context.annotation.Configuration;
|
17
|
import org.springframework.core.env.Environment;
|
18
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
19
|
|
20
|
/**
|
21
|
* This class replaces the {@code org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration} which can not
|
22
|
* yet used in this project.
|
23
|
* <p>
|
24
|
* The configurations keys contained in here explicitly are not placed in {@link CdmConfigurationKeys} since this class here
|
25
|
* does only replace missing functionality which otherwise would be provided by the spring boot <code>MailSenderAutoConfiguration</code>.
|
26
|
*
|
27
|
* @author a.kohlbecker
|
28
|
* @since Sep 14, 2021
|
29
|
*/
|
30
|
@Configuration
|
31
|
@AppConfigurationProperties
|
32
|
public class SendEmailConfigurer {
|
33
|
|
34
|
private static final Logger logger = Logger.getLogger(SendEmailConfigurer.class);
|
35
|
|
36
|
@Autowired
|
37
|
Environment env;
|
38
|
|
39
|
// conforms to spring boot config
|
40
|
private static final String HOST = "mail.host";
|
41
|
public static final String PORT = "mail.port";
|
42
|
private static final String USERNAME = "mail.username";
|
43
|
private static final String PASSWORD = "mail.password";
|
44
|
public static final String FROM_ADDRESS = "mail.from-address";
|
45
|
private static final String DEFAULT_ENCODING = "mail.default-encoding";
|
46
|
|
47
|
private static final String[] SMTP_PROPERTY_KEYS = new String[] {"mail.smtp.auth", "mail.smtp.starttls.enable"};
|
48
|
|
49
|
/**
|
50
|
* Disables the configuration of the mail system
|
51
|
* integration tests of the mail system are skipped if this property is set true
|
52
|
*/
|
53
|
public static final String DISABLED = "mail.disabled";
|
54
|
public static final String INT_TEST_SERVER = "mail.int-test-server";
|
55
|
|
56
|
public SendEmailConfigurer() {
|
57
|
System.out.print(1);
|
58
|
}
|
59
|
|
60
|
@Bean
|
61
|
public JavaMailSenderImpl mailSender() {
|
62
|
|
63
|
boolean disabled = false;
|
64
|
try {
|
65
|
disabled = Boolean.valueOf(env.getProperty(DISABLED));
|
66
|
} catch (Exception e) {
|
67
|
logger.error("Invalid property '" + DISABLED + "=" + env.getProperty(DISABLED) + "' for JavaMailSenderImpl configuration");
|
68
|
}
|
69
|
if(disabled) {
|
70
|
logger.warn("JavaMailSenderImpl configuration explictly disabled by property '" + DISABLED + "=true'");
|
71
|
return null;
|
72
|
} else {
|
73
|
JavaMailSenderImpl sender = new JavaMailSenderImpl();
|
74
|
applyProperties(sender);
|
75
|
return sender;
|
76
|
}
|
77
|
}
|
78
|
|
79
|
private void applyProperties(JavaMailSenderImpl sender) {
|
80
|
sender.setHost(env.getProperty(HOST));
|
81
|
if (env.getProperty(PORT) != null) {
|
82
|
sender.setPort(Integer.parseInt(env.getProperty(PORT)));
|
83
|
}
|
84
|
if (env.getProperty(USERNAME) != null) {
|
85
|
sender.setUsername(env.getProperty(USERNAME));
|
86
|
}
|
87
|
if (env.getProperty(PASSWORD) != null) {
|
88
|
sender.setPassword(env.getProperty(PASSWORD));
|
89
|
}
|
90
|
// sender.setProtocol(this.properties.getProtocol());
|
91
|
if (env.getProperty(DEFAULT_ENCODING) != null) {
|
92
|
sender.setDefaultEncoding(env.getProperty(DEFAULT_ENCODING));
|
93
|
}
|
94
|
|
95
|
Properties smtpProperties = new Properties();
|
96
|
for(String key : SMTP_PROPERTY_KEYS) {
|
97
|
String value = env.getProperty(key);
|
98
|
if(value != null) {
|
99
|
smtpProperties.put(key, value);
|
100
|
}
|
101
|
}
|
102
|
sender.setJavaMailProperties(smtpProperties);
|
103
|
}
|
104
|
|
105
|
}
|