Project

General

Profile

Download (3.52 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.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
 *
24
 * @author a.kohlbecker
25
 * @since Sep 14, 2021
26
 */
27
@Configuration
28
@AppConfigurationProperties
29
public class SendEmailConfigurer {
30

    
31
    private static final Logger logger = Logger.getLogger(SendEmailConfigurer.class);
32

    
33
    @Autowired
34
    Environment env;
35

    
36
    // conforms to spring boot config
37
    private static final String HOST = "mail.host";
38
    public static final String PORT = "mail.port";
39
    private static final String USERNAME = "mail.username";
40
    private static final String PASSWORD = "mail.password";
41
    public static final String FROM_ADDRESS = "mail.from-address";
42
    private static final String DEFAULT_ENCODING = "mail.default-encoding";
43

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

    
46
    /**
47
     * Disables the configuration of the mail system
48
     * integration tests of the mail system are skipped if this property is set true
49
     */
50
    public static final String DISABLED = "mail.disabled";
51
    public static final String INT_TEST_SERVER = "mail.int-test-server";
52

    
53
    public SendEmailConfigurer() {
54
        System.out.print(1);
55
    }
56

    
57
    @Bean
58
    public JavaMailSenderImpl mailSender() {
59

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

    
76
    private void applyProperties(JavaMailSenderImpl sender) {
77
        sender.setHost(env.getProperty(HOST));
78
        if (env.getProperty(PORT) != null) {
79
            sender.setPort(Integer.parseInt(env.getProperty(PORT)));
80
        }
81
        if (env.getProperty(USERNAME) != null) {
82
            sender.setUsername(env.getProperty(USERNAME));
83
        }
84
        if (env.getProperty(PASSWORD) != null) {
85
        sender.setPassword(env.getProperty(PASSWORD));
86
        }
87
        // sender.setProtocol(this.properties.getProtocol());
88
        if (env.getProperty(DEFAULT_ENCODING) != null) {
89
            sender.setDefaultEncoding(env.getProperty(DEFAULT_ENCODING));
90
        }
91

    
92
        Properties smtpProperties = new Properties();
93
        for(String key : SMTP_PROPERTY_KEYS) {
94
            String value = env.getProperty(key);
95
            if(value != null) {
96
                smtpProperties.put(key, value);
97
            }
98
        }
99
        sender.setJavaMailProperties(smtpProperties);
100
    }
101

    
102
}
(9-9/9)