This repository was archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 368
Add ses module #669
Merged
Merged
Add ses module #669
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48a6012
Add ses module
eddumelendez 53be93f
Deprecate cloud.aws.mail.enabled
eddumelendez a113322
Add condition when class is missing
eddumelendez 8cd92c0
Run SesAutoConfiguration before SimpleEmailAutoConfiguration
eddumelendez 2303607
Add docs
eddumelendez c4683fe
Fix format
eddumelendez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
.../src/main/java/org/springframework/cloud/aws/autoconfigure/mail/SesAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.aws.autoconfigure.mail; | ||
|
||
import javax.mail.Session; | ||
|
||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; | ||
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration; | ||
import org.springframework.cloud.aws.context.annotation.ConditionalOnMissingAmazonClient; | ||
import org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean; | ||
import org.springframework.cloud.aws.core.region.RegionProvider; | ||
import org.springframework.cloud.aws.core.region.StaticRegionProvider; | ||
import org.springframework.cloud.aws.ses.SimpleEmailServiceJavaMailSender; | ||
import org.springframework.cloud.aws.ses.SimpleEmailServiceMailSender; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.mail.MailSender; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for AWS Simple Email Service | ||
* support. | ||
* | ||
* @author Agim Emruli | ||
* @author Eddú Meléndez | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureAfter(MailSenderAutoConfiguration.class) | ||
@AutoConfigureBefore(SimpleEmailAutoConfiguration.class) | ||
@ConditionalOnClass({ AmazonSimpleEmailService.class, MailSender.class }) | ||
@ConditionalOnMissingBean(MailSender.class) | ||
@Import(ContextCredentialsAutoConfiguration.class) | ||
@EnableConfigurationProperties(SesProperties.class) | ||
@ConditionalOnProperty(name = "spring.cloud.aws.ses.enabled", havingValue = "true", matchIfMissing = true) | ||
public class SesAutoConfiguration { | ||
|
||
private final AWSCredentialsProvider credentialsProvider; | ||
|
||
private final RegionProvider regionProvider; | ||
|
||
public SesAutoConfiguration(ObjectProvider<RegionProvider> regionProvider, | ||
ObjectProvider<AWSCredentialsProvider> credentialsProvider, SesProperties properties) { | ||
this.credentialsProvider = credentialsProvider.getIfAvailable(); | ||
this.regionProvider = properties.getRegion() == null ? regionProvider.getIfAvailable() | ||
: new StaticRegionProvider(properties.getRegion()); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingAmazonClient(AmazonSimpleEmailService.class) | ||
public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService() { | ||
return new AmazonWebserviceClientFactoryBean<>(AmazonSimpleEmailServiceClient.class, this.credentialsProvider, | ||
this.regionProvider); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingClass("javax.mail.Session") | ||
public MailSender simpleMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { | ||
return new SimpleEmailServiceMailSender(amazonSimpleEmailService); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(Session.class) | ||
public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { | ||
return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...nfigure/src/main/java/org/springframework/cloud/aws/autoconfigure/mail/SesProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.aws.autoconfigure.mail; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties for AWS Simple Email Service. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.cloud.aws.ses") | ||
public class SesProperties { | ||
|
||
/** | ||
* Overrides the default region. | ||
*/ | ||
private String region; | ||
|
||
public String getRegion() { | ||
return region; | ||
} | ||
|
||
public void setRegion(String region) { | ||
this.region = region; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
.../test/java/org/springframework/cloud/aws/autoconfigure/mail/SesAutoConfigurationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.aws.autoconfigure.mail; | ||
|
||
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.FilteredClassLoader; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.mail.MailSender; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SesAutoConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(SesAutoConfiguration.class)); | ||
|
||
@Test | ||
void mailSenderWithJavaMail() { | ||
this.contextRunner.run(context -> { | ||
assertThat(context.getBean(MailSender.class)).isNotNull(); | ||
assertThat(context.getBean(JavaMailSender.class)).isNotNull(); | ||
assertThat(context.getBean(JavaMailSender.class)).isSameAs(context.getBean(MailSender.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
void mailSenderWithSimpleEmail() { | ||
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> { | ||
assertThat(context.getBean(MailSender.class)).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
void mailSenderWithDefaultRegion() { | ||
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> { | ||
assertThat(context.getBean(MailSender.class)).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class)); | ||
|
||
AmazonSimpleEmailServiceClient client = context.getBean(AmazonSimpleEmailServiceClient.class); | ||
Object region = ReflectionTestUtils.getField(client, "signingRegion"); | ||
assertThat(region).isEqualTo("us-west-2"); | ||
}); | ||
} | ||
|
||
@Test | ||
void mailSenderWithCustomRegion() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.region:us-east-1") | ||
.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> { | ||
assertThat(context.getBean(MailSender.class)).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isNotNull(); | ||
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class)); | ||
|
||
AmazonSimpleEmailServiceClient client = context.getBean(AmazonSimpleEmailServiceClient.class); | ||
Object region = ReflectionTestUtils.getField(client, "signingRegion"); | ||
assertThat(region).isEqualTo("us-east-1"); | ||
}); | ||
} | ||
|
||
@Test | ||
void sesAutoConfigurationIsDisabled() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.enabled:false").run(context -> { | ||
assertThat(context).doesNotHaveBean(MailSender.class); | ||
assertThat(context).doesNotHaveBean(JavaMailSender.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void sesAutoConfigurationIsDisabledButSimpleEmailAutoConfigurationIsEnabled() { | ||
new ApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of(SesAutoConfiguration.class, SimpleEmailAutoConfiguration.class)) | ||
.withPropertyValues("spring.cloud.aws.ses.enabled:false").run(context -> { | ||
assertThat(context).hasSingleBean(MailSender.class); | ||
assertThat(context).hasSingleBean(JavaMailSender.class); | ||
}); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.