mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add auto-configuration for SendGrid's client
Closes gh-2160 Closes gh-2280
This commit is contained in:
parent
33a92caad6
commit
f05769dcc5
@ -155,6 +155,11 @@
|
||||
<artifactId>groovy-templates</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sendgrid</groupId>
|
||||
<artifactId>sendgrid-java</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP-java6</artifactId>
|
||||
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.boot.autoconfigure.sendgrid;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.sendgrid.SendGrid;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for SendGrid
|
||||
*
|
||||
* @author Maciej Walkowiak
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(SendGrid.class)
|
||||
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username")
|
||||
@EnableConfigurationProperties(SendGridProperties.class)
|
||||
public class SendGridAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private SendGridProperties properties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(SendGrid.class)
|
||||
public SendGrid sendGrid() {
|
||||
SendGrid sendGrid = new SendGrid(this.properties.getUsername(),
|
||||
this.properties.getPassword());
|
||||
|
||||
if (this.properties.isProxyConfigured()) {
|
||||
HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(),
|
||||
this.properties.getProxy().getPort());
|
||||
CloseableHttpClient http = HttpClientBuilder.create().setProxy(proxy)
|
||||
.setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build();
|
||||
|
||||
sendGrid.setClient(http);
|
||||
}
|
||||
|
||||
return sendGrid;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.boot.autoconfigure.sendgrid;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* {@link ConfigurationProperties} for SendGrid.
|
||||
*
|
||||
* @author Maciej Walkowiak
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.sendgrid")
|
||||
public class SendGridProperties {
|
||||
|
||||
/**
|
||||
* SendGrid username.
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* SendGrid password.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Proxy configuration.
|
||||
*/
|
||||
private Proxy proxy;
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Proxy getProxy() {
|
||||
return this.proxy;
|
||||
}
|
||||
|
||||
public void setProxy(Proxy proxy) {
|
||||
this.proxy = proxy;
|
||||
}
|
||||
|
||||
public boolean isProxyConfigured() {
|
||||
return this.proxy != null && this.proxy.getHost() != null
|
||||
&& this.proxy.getPort() != null;
|
||||
}
|
||||
|
||||
public static class Proxy {
|
||||
|
||||
/**
|
||||
* SendGrid proxy host.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* SendGrid proxy port.
|
||||
*/
|
||||
private Integer port;
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -50,6 +50,7 @@ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
|
||||
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.boot.autoconfigure.sendgrid;
|
||||
|
||||
import org.apache.http.conn.routing.HttpRoutePlanner;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import com.sendgrid.SendGrid;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SendGridAutoConfiguration}.
|
||||
*
|
||||
* @author Maciej Walkowiak
|
||||
*/
|
||||
public class SendGridAutoConfigurationTests {
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSendGridBeanCreated() {
|
||||
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertEquals("user", ReflectionTestUtils.getField(sendGrid, "username"));
|
||||
assertEquals("secret", ReflectionTestUtils.getField(sendGrid, "password"));
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void autoConfigurationNotFiredWhenPropertiesNotSet() {
|
||||
loadContext();
|
||||
this.context.getBean(SendGrid.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
|
||||
loadContext(ManualSendGridConfiguration.class, "spring.sendgrid.username:user",
|
||||
"spring.sendgrid.password:secret");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertEquals("manual-user", ReflectionTestUtils.getField(sendGrid, "username"));
|
||||
assertEquals("manual-secret", ReflectionTestUtils.getField(sendGrid, "password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSendGridBeanWithProxyCreated() {
|
||||
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret",
|
||||
"spring.sendgrid.proxy.host:localhost", "spring.sendgrid.proxy.port:5678");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
CloseableHttpClient client = (CloseableHttpClient) ReflectionTestUtils.getField(
|
||||
sendGrid, "client");
|
||||
HttpRoutePlanner routePlanner = (HttpRoutePlanner) ReflectionTestUtils.getField(
|
||||
client, "routePlanner");
|
||||
assertThat(routePlanner, instanceOf(DefaultProxyRoutePlanner.class));
|
||||
}
|
||||
|
||||
private void loadContext(String... environment) {
|
||||
this.loadContext(null, environment);
|
||||
}
|
||||
|
||||
private void loadContext(Class<?> additionalConfiguration, String... environment) {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, environment);
|
||||
this.context.register(SendGridAutoConfiguration.class);
|
||||
if (additionalConfiguration != null) {
|
||||
this.context.register(additionalConfiguration);
|
||||
}
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ManualSendGridConfiguration {
|
||||
|
||||
@Bean
|
||||
SendGrid sendGrid() {
|
||||
return new SendGrid("manual-user", "manual-secret");
|
||||
}
|
||||
}
|
||||
}
|
@ -128,6 +128,7 @@
|
||||
<spring-social-linkedin.version>1.0.1.RELEASE</spring-social-linkedin.version>
|
||||
<spring-social-twitter.version>1.1.0.RELEASE</spring-social-twitter.version>
|
||||
<spring-ws.version>2.2.0.RELEASE</spring-ws.version>
|
||||
<sendgrid.version>2.1.0</sendgrid.version>
|
||||
<sun-mail.version>${javax-mail.version}</sun-mail.version>
|
||||
<thymeleaf.version>2.1.4.RELEASE</thymeleaf.version>
|
||||
<thymeleaf-extras-springsecurity3.version>2.1.1.RELEASE</thymeleaf-extras-springsecurity3.version>
|
||||
@ -512,6 +513,11 @@
|
||||
<artifactId>jmustache</artifactId>
|
||||
<version>${jmustache.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sendgrid</groupId>
|
||||
<artifactId>sendgrid-java</artifactId>
|
||||
<version>${sendgrid.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
|
@ -573,6 +573,12 @@ content into your application; rather pick only the properties that you need.
|
||||
shell.auth.simple.user.password=
|
||||
shell.auth.spring.roles=
|
||||
|
||||
# SENDGRID ({sc-spring-boot-autoconfigure}/sendgrid/SendGridAutoConfiguration.{sc-ext}[SendGridAutoConfiguration])
|
||||
spring.sendgrid.username= # SendGrid account username
|
||||
spring.sendgrid.password= # SendGrid account password
|
||||
spring.sendgrid.proxy.host= # SendGrid proxy host
|
||||
spring.sendgrid.proxy.port= # SendGrid proxy port
|
||||
|
||||
# GIT INFO
|
||||
spring.git.properties= # resource ref to generated git info properties file
|
||||
----
|
||||
|
@ -54,6 +54,11 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sendgrid</groupId>
|
||||
<artifactId>sendgrid-java</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
|
Loading…
Reference in New Issue
Block a user