Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 900acb9

Browse files
committed
Add ses module
New modules are introduced: * spring-cloud-aws-ses * spring-cloud-starter-aws-ses Closes gh-664
1 parent 641a6b8 commit 900acb9

File tree

18 files changed

+1308
-6
lines changed

18 files changed

+1308
-6
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@
6060
<module>spring-cloud-aws-autoconfigure</module>
6161
<module>spring-cloud-aws-parameter-store-config</module>
6262
<module>spring-cloud-aws-secrets-manager-config</module>
63+
<module>spring-cloud-aws-ses</module>
6364
<module>spring-cloud-starter-aws</module>
6465
<module>spring-cloud-starter-aws-jdbc</module>
6566
<module>spring-cloud-starter-aws-messaging</module>
6667
<module>spring-cloud-starter-aws-parameter-store-config</module>
6768
<module>spring-cloud-starter-aws-secrets-manager-config</module>
69+
<module>spring-cloud-starter-aws-ses</module>
6870
<module>spring-cloud-aws-integration-test</module>
6971
<module>docs</module>
7072
</modules>

spring-cloud-aws-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<artifactId>spring-cloud-aws-parameter-store-config</artifactId>
4949
<optional>true</optional>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.cloud</groupId>
53+
<artifactId>spring-cloud-aws-ses</artifactId>
54+
<optional>true</optional>
55+
</dependency>
5156
<dependency>
5257
<groupId>org.springframework.boot</groupId>
5358
<artifactId>spring-boot-autoconfigure</artifactId>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.autoconfigure.mail;
18+
19+
import javax.mail.Session;
20+
21+
import com.amazonaws.auth.AWSCredentialsProvider;
22+
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
23+
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
24+
25+
import org.springframework.beans.factory.ObjectProvider;
26+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
27+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
32+
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
33+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34+
import org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration;
35+
import org.springframework.cloud.aws.context.annotation.ConditionalOnMissingAmazonClient;
36+
import org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean;
37+
import org.springframework.cloud.aws.core.region.RegionProvider;
38+
import org.springframework.cloud.aws.core.region.StaticRegionProvider;
39+
import org.springframework.cloud.aws.ses.SimpleEmailServiceJavaMailSender;
40+
import org.springframework.cloud.aws.ses.SimpleEmailServiceMailSender;
41+
import org.springframework.context.annotation.Bean;
42+
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.context.annotation.Import;
44+
import org.springframework.mail.MailSender;
45+
import org.springframework.mail.javamail.JavaMailSender;
46+
47+
/**
48+
* {@link EnableAutoConfiguration Auto-configuration} for AWS Simple Email Service
49+
* support.
50+
*
51+
* @author Agim Emruli
52+
* @author Eddú Meléndez
53+
*/
54+
@Configuration(proxyBeanMethods = false)
55+
@AutoConfigureAfter(MailSenderAutoConfiguration.class)
56+
@ConditionalOnClass({ AmazonSimpleEmailService.class, MailSender.class })
57+
@ConditionalOnMissingBean(MailSender.class)
58+
@Import(ContextCredentialsAutoConfiguration.class)
59+
@EnableConfigurationProperties(SesProperties.class)
60+
@ConditionalOnProperty(name = "spring.cloud.aws.ses.enabled", havingValue = "true",
61+
matchIfMissing = true)
62+
public class SesAutoConfiguration {
63+
64+
private final AWSCredentialsProvider credentialsProvider;
65+
66+
private final RegionProvider regionProvider;
67+
68+
public SesAutoConfiguration(ObjectProvider<RegionProvider> regionProvider,
69+
ObjectProvider<AWSCredentialsProvider> credentialsProvider,
70+
SesProperties properties) {
71+
this.credentialsProvider = credentialsProvider.getIfAvailable();
72+
this.regionProvider = properties.getRegion() == null
73+
? regionProvider.getIfAvailable()
74+
: new StaticRegionProvider(properties.getRegion());
75+
}
76+
77+
@Bean
78+
@ConditionalOnMissingAmazonClient(AmazonSimpleEmailService.class)
79+
public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService() {
80+
return new AmazonWebserviceClientFactoryBean<>(
81+
AmazonSimpleEmailServiceClient.class, this.credentialsProvider,
82+
this.regionProvider);
83+
}
84+
85+
@Bean
86+
@ConditionalOnMissingClass("javax.mail.Session")
87+
public MailSender simpleMailSender(
88+
AmazonSimpleEmailService amazonSimpleEmailService) {
89+
return new SimpleEmailServiceMailSender(amazonSimpleEmailService);
90+
}
91+
92+
@Bean
93+
@ConditionalOnClass(Session.class)
94+
public JavaMailSender javaMailSender(
95+
AmazonSimpleEmailService amazonSimpleEmailService) {
96+
return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
97+
}
98+
99+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.autoconfigure.mail;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for AWS Simple Email Service.
23+
*
24+
* @author Eddú Meléndez
25+
*/
26+
@ConfigurationProperties(prefix = "spring.cloud.aws.ses")
27+
public class SesProperties {
28+
29+
/**
30+
* Overrides the default region.
31+
*/
32+
private String region;
33+
34+
public String getRegion() {
35+
return region;
36+
}
37+
38+
public void setRegion(String region) {
39+
this.region = region;
40+
}
41+
42+
}

spring-cloud-aws-autoconfigure/src/main/java/org/springframework/cloud/aws/autoconfigure/mail/SimpleEmailAutoConfiguration.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,26 @@
5454
matchIfMissing = true)
5555
public class SimpleEmailAutoConfiguration {
5656

57+
private final AWSCredentialsProvider credentialsProvider;
58+
5759
private final RegionProvider regionProvider;
5860

59-
public SimpleEmailAutoConfiguration(ObjectProvider<RegionProvider> regionProvider) {
61+
public SimpleEmailAutoConfiguration(ObjectProvider<RegionProvider> regionProvider,
62+
ObjectProvider<AWSCredentialsProvider> credentialsProvider) {
63+
this.credentialsProvider = credentialsProvider.getIfAvailable();
6064
this.regionProvider = regionProvider.getIfAvailable();
6165
}
6266

6367
@Bean
6468
@ConditionalOnMissingAmazonClient(AmazonSimpleEmailService.class)
65-
public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService(
66-
AWSCredentialsProvider credentialsProvider) {
69+
public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService() {
6770
return new AmazonWebserviceClientFactoryBean<>(
68-
AmazonSimpleEmailServiceClient.class, credentialsProvider,
71+
AmazonSimpleEmailServiceClient.class, this.credentialsProvider,
6972
this.regionProvider);
7073
}
7174

7275
@Bean
73-
@ConditionalOnMissingClass("org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender")
76+
@ConditionalOnMissingClass("javax.mail.Session")
7477
public MailSender simpleMailSender(
7578
AmazonSimpleEmailService amazonSimpleEmailService) {
7679
return new SimpleEmailServiceMailSender(amazonSimpleEmailService);

spring-cloud-aws-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
"name": "cloud.aws.stack.enabled",
4848
"description": "Enables Stack integration.",
4949
"type": "java.lang.Boolean"
50+
},
51+
{
52+
"defaultValue": true,
53+
"name": "spring.cloud.aws.ses.enabled",
54+
"description": "Enables Simple Email Service integration.",
55+
"type": "java.lang.Boolean"
5056
}
5157
]
5258
}

spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoCon
66
org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration,\
77
org.springframework.cloud.aws.autoconfigure.mail.SimpleEmailAutoConfiguration,\
88
org.springframework.cloud.aws.autoconfigure.cache.ElastiCacheAutoConfiguration,\
9+
org.springframework.cloud.aws.autoconfigure.mail.SesAutoConfiguration,\
910
org.springframework.cloud.aws.autoconfigure.messaging.SqsAutoConfiguration,\
1011
org.springframework.cloud.aws.autoconfigure.messaging.SnsAutoConfiguration,\
1112
org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration,\
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.autoconfigure.mail;
18+
19+
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.FilteredClassLoader;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.mail.MailSender;
26+
import org.springframework.mail.javamail.JavaMailSender;
27+
import org.springframework.test.util.ReflectionTestUtils;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class SesAutoConfigurationTest {
32+
33+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
34+
.withConfiguration(AutoConfigurations.of(SesAutoConfiguration.class));
35+
36+
@Test
37+
void mailSenderWithJavaMail() {
38+
this.contextRunner.run(context -> {
39+
assertThat(context.getBean(MailSender.class)).isNotNull();
40+
assertThat(context.getBean(JavaMailSender.class)).isNotNull();
41+
assertThat(context.getBean(JavaMailSender.class))
42+
.isSameAs(context.getBean(MailSender.class));
43+
});
44+
}
45+
46+
@Test
47+
void mailSenderWithSimpleEmail() {
48+
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session"))
49+
.run(context -> {
50+
assertThat(context.getBean(MailSender.class)).isNotNull();
51+
assertThat(context.getBean("simpleMailSender")).isNotNull();
52+
assertThat(context.getBean("simpleMailSender"))
53+
.isSameAs(context.getBean(MailSender.class));
54+
});
55+
}
56+
57+
@Test
58+
void mailSenderWithDefaultRegion() {
59+
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session"))
60+
.run(context -> {
61+
assertThat(context.getBean(MailSender.class)).isNotNull();
62+
assertThat(context.getBean("simpleMailSender")).isNotNull();
63+
assertThat(context.getBean("simpleMailSender"))
64+
.isSameAs(context.getBean(MailSender.class));
65+
66+
AmazonSimpleEmailServiceClient client = context
67+
.getBean(AmazonSimpleEmailServiceClient.class);
68+
Object region = ReflectionTestUtils.getField(client, "signingRegion");
69+
assertThat(region).isEqualTo("us-west-2");
70+
});
71+
}
72+
73+
@Test
74+
void mailSenderWithCustomRegion() {
75+
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.region:us-east-1")
76+
.withClassLoader(new FilteredClassLoader("javax.mail.Session"))
77+
.run(context -> {
78+
assertThat(context.getBean(MailSender.class)).isNotNull();
79+
assertThat(context.getBean("simpleMailSender")).isNotNull();
80+
assertThat(context.getBean("simpleMailSender"))
81+
.isSameAs(context.getBean(MailSender.class));
82+
83+
AmazonSimpleEmailServiceClient client = context
84+
.getBean(AmazonSimpleEmailServiceClient.class);
85+
Object region = ReflectionTestUtils.getField(client, "signingRegion");
86+
assertThat(region).isEqualTo("us-east-1");
87+
});
88+
}
89+
90+
@Test
91+
void mailIsDisabled() {
92+
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.enabled:false")
93+
.run(context -> {
94+
assertThat(context).doesNotHaveBean(MailSender.class);
95+
assertThat(context).doesNotHaveBean(JavaMailSender.class);
96+
});
97+
}
98+
99+
}

spring-cloud-aws-autoconfigure/src/test/java/org/springframework/cloud/aws/autoconfigure/mail/SimpleEmailAutoConfigurationTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
2222
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
23+
import org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender;
2324
import org.springframework.mail.MailSender;
2425
import org.springframework.mail.javamail.JavaMailSender;
2526

@@ -31,7 +32,7 @@ class SimpleEmailAutoConfigurationTest {
3132
.withConfiguration(AutoConfigurations.of(SimpleEmailAutoConfiguration.class));
3233

3334
@Test
34-
public void mailSender_MailSenderWithJava_configuresJavaMailSender() {
35+
void mailSender_MailSenderWithJava_configuresJavaMailSender() {
3536
this.contextRunner.run(context -> {
3637
assertThat(context.getBean(MailSender.class)).isNotNull();
3738
assertThat(context.getBean(JavaMailSender.class)).isNotNull();
@@ -40,6 +41,17 @@ public void mailSender_MailSenderWithJava_configuresJavaMailSender() {
4041
});
4142
}
4243

44+
@Test
45+
void mailSenderWithSimpleEmail() {
46+
this.contextRunner.run(context -> {
47+
assertThat(context.getBean(MailSender.class)).isNotNull();
48+
assertThat(context.getBean(SimpleEmailServiceJavaMailSender.class))
49+
.isNotNull();
50+
assertThat(context.getBean(SimpleEmailServiceJavaMailSender.class))
51+
.isSameAs(context.getBean(MailSender.class));
52+
});
53+
}
54+
4355
@Test
4456
void mailIsDisabled() {
4557
this.contextRunner.withPropertyValues("cloud.aws.mail.enabled:false")

spring-cloud-aws-context/src/main/java/org/springframework/cloud/aws/mail/simplemail/SimpleEmailServiceJavaMailSender.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
*
5757
* @author Agim Emruli
5858
* @since 1.0
59+
* @deprecated Use `spring-cloud-starter-aws-ses`
5960
*/
6061
public class SimpleEmailServiceJavaMailSender extends SimpleEmailServiceMailSender
6162
implements JavaMailSender {

0 commit comments

Comments
 (0)