Project

General

Profile

Revision bcf94376

IDbcf94376128f49035cca1927d59e04c0016bdc7c
Parent 1574f983
Child 82da5e6c

Added by Andreas Kohlbecker 2 months ago

fix #9758 configuration for sending emails with JavaMailSenderImpl

View differences:

cdmlib-services/pom.xml
86 86
      <groupId>org.springframework.security</groupId>
87 87
      <artifactId>spring-security-config</artifactId>
88 88
    </dependency>
89
    <!-- needed for JavaMailSender -->
90
    <dependency>
91
        <groupId>org.springframework</groupId>
92
        <artifactId>spring-context-support</artifactId>
93
    </dependency>
94
    <dependency>
95
      <groupId>javax.mail</groupId>
96
      <artifactId>mailapi</artifactId>
97
    </dependency>
98
    <dependency>
99
        <groupId>org.subethamail</groupId>
100
        <artifactId>subethasmtp</artifactId>
101
        <scope>test</scope>
102
    </dependency>    
103
    <!-- =========================== -->
89 104
    <dependency>
90 105
      <groupId>com.ibm.lsid</groupId>
91 106
      <artifactId>lsid-server</artifactId>
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/config/AppConfigurationProperties.java
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 org.springframework.context.annotation.Import;
12

  
13
/**
14
 * Annotation to make sure the {@link AppConfigurationPropertiesConfig} is
15
 * already loaded for other configuration classes.
16
 *
17
 * @author a.kohlbecker
18
 * @since Sep 14, 2021
19
 */
20
@Import(AppConfigurationPropertiesConfig.class)
21
public @interface AppConfigurationProperties {
22

  
23
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/config/AppConfigurationPropertiesConfig.java
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 org.springframework.context.annotation.Configuration;
12
import org.springframework.context.annotation.PropertySource;
13
import org.springframework.context.annotation.PropertySources;
14

  
15
/**
16
 * Centralizes the application configuration properties for all <code>@Configuration</code> beans.
17
 *
18
 * The default configuration in {@code classpath:/application.properties} can be overwritten
19
 * by {@code file:/${user.home}/.cdmLibrary/application-dev.properties} for development purposes.
20
 * For production environments it is recommended to use system properties instead.
21
 *
22
 * Mimics the spring boot auto-configuration capabilities to some very basic extend.
23
 *
24
 * Can be replaced once we are using spring boot.
25
 *
26
 * The property source file <code>application.properties</code> may be placed in
27
 * <code>src/test/resources</code> and <code>src/main/resources</code>
28
 *
29
 * @author a.kohlbecker
30
 * @since Sep 15, 2021
31
 */
32
@Configuration
33
@PropertySources({
34
    // classpath:/application.properties as first
35
    @PropertySource("classpath:/application.properties"),
36
    // allows application-dev.properties to override the default settings !!!
37
    @PropertySource(value="file:${user.home}/.cdmLibrary/application-dev.properties", ignoreResourceNotFound = true)
38
})
39
public class AppConfigurationPropertiesConfig {
40

  
41
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/config/SendEmailConfigurer.java
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
        boolean disabled = false;
60
        try {
61
           disabled = Boolean.valueOf(env.getProperty(DISABLED));
62
        } catch (Exception e) {
63
            logger.error("Invalid  property '" + DISABLED + "=" + env.getProperty(DISABLED) + "' for JavaMailSenderImpl configuration");
64
        }
65
        if(disabled) {
66
            logger.warn("JavaMailSenderImpl configuration explictly disabled by property '" + DISABLED + "=true'");
67
            return null;
68
        } else {
69
            JavaMailSenderImpl sender = new JavaMailSenderImpl();
70
            applyProperties(sender);
71
            return sender;
72
        }
73
    }
74

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

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

  
101
}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/EmailSendTest.java
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.service;
10

  
11
import static org.junit.Assert.assertEquals;
12

  
13
import java.io.FileNotFoundException;
14
import java.util.Objects;
15

  
16
import javax.mail.MessagingException;
17
import javax.mail.internet.MimeMessage;
18

  
19
import org.junit.Test;
20
import org.springframework.core.env.Environment;
21
import org.springframework.mail.SimpleMailMessage;
22
import org.springframework.mail.javamail.JavaMailSender;
23
import org.subethamail.wiser.Wiser;
24
import org.subethamail.wiser.WiserMessage;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

  
27
import eu.etaxonomy.cdm.api.config.SendEmailConfigurer;
28
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
29

  
30

  
31
public class EmailSendTest extends CdmIntegrationTest {
32

  
33

  
34
    private static final String SUBJECT = "eu.etaxonomy.cdm.test.function.EmailSendTest";
35

  
36
    private static final String TO = "a.kohlbecker@bgbm.org";
37

  
38
    @SpringBeanByType
39
    private JavaMailSender emailSender;
40

  
41
    @SpringBeanByType
42
    private Environment env;
43

  
44
    @Test
45
    public void sendEmailTest() {
46

  
47
        String from = env.getProperty(SendEmailConfigurer.FROM_ADDRESS);
48

  
49
        boolean useWiser = Objects.equals(env.getProperty(SendEmailConfigurer.INT_TEST_SERVER), "wiser");
50
        Wiser wiser = null;
51
        if(useWiser) {
52
            // start test smtp server
53
            wiser = new Wiser();
54
            wiser.setPort(2500); // Default is 25
55
            wiser.start();
56
        }
57

  
58
        boolean disabled = false;
59
        try {
60
           disabled = Boolean.valueOf(env.getProperty(SendEmailConfigurer.DISABLED));
61
        } catch (Exception e) {
62
            // ignore all
63
        }
64
        if(!disabled) {
65

  
66
            SimpleMailMessage message = new SimpleMailMessage();
67
            message.setFrom(from);
68
            message.setTo(TO);
69
            message.setSubject(SUBJECT);
70
            message.setText("This is a test email send from the cdmlib intergration test suite");
71
            emailSender.send(message);
72
        }
73

  
74
        if(useWiser) {
75
            for (WiserMessage message : wiser.getMessages())
76
            {
77
                String envelopeSender = message.getEnvelopeSender();
78
                String envelopeReceiver = message.getEnvelopeReceiver();
79
                String subject = null;
80

  
81
                assertEquals(from, envelopeSender);
82
                assertEquals(TO, envelopeReceiver);
83
                try {
84
                    MimeMessage mess = message.getMimeMessage();
85
                    subject  = mess.getSubject();
86
                } catch (MessagingException e) {
87
                    logger.error("MessagingException", e);
88
                }
89
                assertEquals(SUBJECT, subject);
90
            }
91
            wiser.stop();
92
        }
93

  
94
    }
95

  
96
    @Override
97
    public void createTestDataSet() throws FileNotFoundException {
98
        // no data needed
99
    }
100

  
101
}
cdmlib-services/src/test/resources/application.properties
1
# ======================================================
2
# = MAIL configuration for the local SMPT server Wiser
3
#   see eu.etaxonomy.cdm.api.service.EmailSendTest
4
# ======================================================
5
mail.default-encoding: UTF-8
6
mail.host: localhost
7
mail.from-address: test-mail@bgbm.org
8
#mail.username: test-mail@bgbm.org
9
#mail.password: lsdjfoianef
10
mail.port: 2500
11
# JavaMailProperties
12
#mail.smtp.auth: true
13
#mail.smtp.starttls.enable: true
14
# this properties allows for disabling the mail agent
15
mail.disabled: false 
pom.xml
841 841
     <!--    <scope>test</scope>  -->
842 842
		<version>${project.version}</version>
843 843
	  </dependency>
844
      <dependency>
845
        <!-- used in cdmlib-services for the EmailSendTest -->
846
        <groupId>org.subethamail</groupId>
847
        <artifactId>subethasmtp</artifactId>
848
        <version>3.1.7</version>
849
        <scope>test</scope>
850
      </dependency>    
844 851
      <!-- ******* aspect ******* -->
845 852
      <dependency>
846 853
        <!-- not really needed as long as aspectjweaver is on classpath which is a superset of aspectjrt -->
......
1648 1655
         <artifactId>tools</artifactId>
1649 1656
         <version>1.8.0</version>
1650 1657
      </dependency>
1658
      <!-- Email functionality (used in cdmlib-services) -->
1659
      <dependency>
1660
        <groupId>javax.mail</groupId>
1661
        <artifactId>mailapi</artifactId>
1662
        <version>1.4.3</version>
1663
      </dependency>
1651 1664
   </dependencies>    
1652 1665
  </dependencyManagement>
1653 1666
</project>

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)