Project

General

Profile

Download (4.55 KB) Statistics
| Branch: | Tag: | Revision:
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.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.Properties;
15

    
16
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.context.annotation.Bean;
19
import org.springframework.context.annotation.Configuration;
20
import org.springframework.core.env.Environment;
21
import org.springframework.mail.javamail.JavaMailSenderImpl;
22

    
23
/**
24
 * This class replaces the {@code org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration} which can not
25
 * yet used in this project.
26
 * <p>
27
 * The configurations keys contained in here explicitly are not placed in {@link CdmConfigurationKeys} since this class here
28
 * does only replace missing functionality which otherwise would be provided by the spring boot <code>MailSenderAutoConfiguration</code>.
29
 *
30
 * @author a.kohlbecker
31
 * @since Sep 14, 2021
32
 */
33
@Configuration
34
@AppConfigurationProperties
35
public class SendEmailConfigurer {
36

    
37
    private static final Logger logger = LogManager.getLogger(SendEmailConfigurer.class);
38

    
39
    @Autowired
40
    Environment env;
41

    
42
    // conforms to spring boot config
43
    private static final String HOST = "mail.host";
44
    public static final String PORT = "mail.port";
45
    private static final String USERNAME = "mail.username";
46
    private static final String PASSWORD = "mail.password";
47
    public static final String FROM_ADDRESS = "mail.from-address";
48
    private static final String DEFAULT_ENCODING = "mail.default-encoding";
49

    
50
    private static final String[] SMTP_PROPERTY_KEYS = new String[] {"mail.smtp.auth", "mail.smtp.starttls.enable"};
51

    
52
    /**
53
     * Disables the configuration of the mail system
54
     * integration tests of the mail system are skipped if this property is set true
55
     */
56
    public static final String DISABLED = "mail.disabled";
57
    public static final String INT_TEST_SERVER = "mail.int-test-server";
58

    
59
    public SendEmailConfigurer() {
60
    }
61

    
62
    @Bean
63
    public JavaMailSenderImpl mailSender() {
64

    
65
        reportMailConfiguration();
66
        boolean disabled = false;
67
        try {
68
           disabled = Boolean.valueOf(env.getProperty(DISABLED));
69
        } catch (Exception e) {
70
            logger.error("Invalid  property '" + DISABLED + "=" + env.getProperty(DISABLED) + "' for JavaMailSenderImpl configuration");
71
        }
72
        if(disabled) {
73
            logger.warn("JavaMailSenderImpl configuration explictly disabled by property '" + DISABLED + "=true'");
74
            return null;
75
        } else {
76
            JavaMailSenderImpl sender = new JavaMailSenderImpl();
77
            applyProperties(sender);
78
            return sender;
79
        }
80
    }
81

    
82
    private void applyProperties(JavaMailSenderImpl sender) {
83
        sender.setHost(env.getProperty(HOST));
84
        if (env.getProperty(PORT) != null) {
85
            sender.setPort(Integer.parseInt(env.getProperty(PORT)));
86
        }
87
        if (env.getProperty(USERNAME) != null) {
88
            sender.setUsername(env.getProperty(USERNAME));
89
        }
90
        if (env.getProperty(PASSWORD) != null) {
91
        sender.setPassword(env.getProperty(PASSWORD));
92
        }
93
        // sender.setProtocol(this.properties.getProtocol());
94
        if (env.getProperty(DEFAULT_ENCODING) != null) {
95
            sender.setDefaultEncoding(env.getProperty(DEFAULT_ENCODING));
96
        }
97

    
98
        Properties smtpProperties = new Properties();
99
        for(String key : SMTP_PROPERTY_KEYS) {
100
            String value = env.getProperty(key);
101
            if(value != null) {
102
                smtpProperties.put(key, value);
103
            }
104
        }
105
        sender.setJavaMailProperties(smtpProperties);
106
    }
107

    
108
    private void reportMailConfiguration() {
109
        logger.info("+-------------------------------------------");
110
        logger.info("|          SendEmail Configuration            ");
111
        configKeys().stream().forEach(key -> logger.info("| " + key + " : " + env.getProperty(key, "[NULL]")));
112
        logger.info("+--------------------------------------------");
113

    
114
    }
115

    
116
    private List<String> configKeys() {
117
        List<String> configKeys = new ArrayList<>(Arrays.asList(HOST, PORT, USERNAME, PASSWORD, DISABLED, FROM_ADDRESS, INT_TEST_SERVER));
118
        configKeys.addAll(Arrays.asList(SMTP_PROPERTY_KEYS));
119
        return configKeys;
120
    }
121

    
122
}
(10-10/10)