Skip to content

Commit f05769d

Browse files
maciejwalkowiakwilkinsona
authored andcommitted
Add auto-configuration for SendGrid's client
Closes gh-2160 Closes gh-2280
1 parent 33a92ca commit f05769d

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@
155155
<artifactId>groovy-templates</artifactId>
156156
<optional>true</optional>
157157
</dependency>
158+
<dependency>
159+
<groupId>com.sendgrid</groupId>
160+
<artifactId>sendgrid-java</artifactId>
161+
<optional>true</optional>
162+
</dependency>
158163
<dependency>
159164
<groupId>com.zaxxer</groupId>
160165
<artifactId>HikariCP-java6</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.boot.autoconfigure.sendgrid;
18+
19+
import org.apache.http.HttpHost;
20+
import org.apache.http.impl.client.CloseableHttpClient;
21+
import org.apache.http.impl.client.HttpClientBuilder;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
31+
import com.sendgrid.SendGrid;
32+
33+
/**
34+
* {@link EnableAutoConfiguration Auto-configuration} for SendGrid
35+
*
36+
* @author Maciej Walkowiak
37+
* @since 1.3.0
38+
*/
39+
@Configuration
40+
@ConditionalOnClass(SendGrid.class)
41+
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username")
42+
@EnableConfigurationProperties(SendGridProperties.class)
43+
public class SendGridAutoConfiguration {
44+
45+
@Autowired
46+
private SendGridProperties properties;
47+
48+
@Bean
49+
@ConditionalOnMissingBean(SendGrid.class)
50+
public SendGrid sendGrid() {
51+
SendGrid sendGrid = new SendGrid(this.properties.getUsername(),
52+
this.properties.getPassword());
53+
54+
if (this.properties.isProxyConfigured()) {
55+
HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(),
56+
this.properties.getProxy().getPort());
57+
CloseableHttpClient http = HttpClientBuilder.create().setProxy(proxy)
58+
.setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build();
59+
60+
sendGrid.setClient(http);
61+
}
62+
63+
return sendGrid;
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.boot.autoconfigure.sendgrid;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* {@link ConfigurationProperties} for SendGrid.
23+
*
24+
* @author Maciej Walkowiak
25+
* @since 1.3.0
26+
*/
27+
@ConfigurationProperties(prefix = "spring.sendgrid")
28+
public class SendGridProperties {
29+
30+
/**
31+
* SendGrid username.
32+
*/
33+
private String username;
34+
35+
/**
36+
* SendGrid password.
37+
*/
38+
private String password;
39+
40+
/**
41+
* Proxy configuration.
42+
*/
43+
private Proxy proxy;
44+
45+
public String getUsername() {
46+
return this.username;
47+
}
48+
49+
public void setUsername(String username) {
50+
this.username = username;
51+
}
52+
53+
public String getPassword() {
54+
return this.password;
55+
}
56+
57+
public void setPassword(String password) {
58+
this.password = password;
59+
}
60+
61+
public Proxy getProxy() {
62+
return this.proxy;
63+
}
64+
65+
public void setProxy(Proxy proxy) {
66+
this.proxy = proxy;
67+
}
68+
69+
public boolean isProxyConfigured() {
70+
return this.proxy != null && this.proxy.getHost() != null
71+
&& this.proxy.getPort() != null;
72+
}
73+
74+
public static class Proxy {
75+
76+
/**
77+
* SendGrid proxy host.
78+
*/
79+
private String host;
80+
81+
/**
82+
* SendGrid proxy port.
83+
*/
84+
private Integer port;
85+
86+
public String getHost() {
87+
return this.host;
88+
}
89+
90+
public void setHost(String host) {
91+
this.host = host;
92+
}
93+
94+
public Integer getPort() {
95+
return this.port;
96+
}
97+
98+
public void setPort(Integer port) {
99+
this.port = port;
100+
}
101+
102+
}
103+
104+
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
5050
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
5151
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
5252
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
53+
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
5354
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
5455
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
5556
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.boot.autoconfigure.sendgrid;
18+
19+
import org.apache.http.conn.routing.HttpRoutePlanner;
20+
import org.apache.http.impl.client.CloseableHttpClient;
21+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
22+
import org.junit.After;
23+
import org.junit.Test;
24+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25+
import org.springframework.boot.test.EnvironmentTestUtils;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.test.util.ReflectionTestUtils;
30+
31+
import com.sendgrid.SendGrid;
32+
33+
import static org.hamcrest.Matchers.instanceOf;
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertThat;
36+
37+
/**
38+
* Tests for {@link SendGridAutoConfiguration}.
39+
*
40+
* @author Maciej Walkowiak
41+
*/
42+
public class SendGridAutoConfigurationTests {
43+
private AnnotationConfigApplicationContext context;
44+
45+
@After
46+
public void close() {
47+
if (this.context != null) {
48+
this.context.close();
49+
}
50+
}
51+
52+
@Test
53+
public void expectedSendGridBeanCreated() {
54+
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret");
55+
SendGrid sendGrid = this.context.getBean(SendGrid.class);
56+
assertEquals("user", ReflectionTestUtils.getField(sendGrid, "username"));
57+
assertEquals("secret", ReflectionTestUtils.getField(sendGrid, "password"));
58+
}
59+
60+
@Test(expected = NoSuchBeanDefinitionException.class)
61+
public void autoConfigurationNotFiredWhenPropertiesNotSet() {
62+
loadContext();
63+
this.context.getBean(SendGrid.class);
64+
}
65+
66+
@Test
67+
public void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
68+
loadContext(ManualSendGridConfiguration.class, "spring.sendgrid.username:user",
69+
"spring.sendgrid.password:secret");
70+
SendGrid sendGrid = this.context.getBean(SendGrid.class);
71+
assertEquals("manual-user", ReflectionTestUtils.getField(sendGrid, "username"));
72+
assertEquals("manual-secret", ReflectionTestUtils.getField(sendGrid, "password"));
73+
}
74+
75+
@Test
76+
public void expectedSendGridBeanWithProxyCreated() {
77+
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret",
78+
"spring.sendgrid.proxy.host:localhost", "spring.sendgrid.proxy.port:5678");
79+
SendGrid sendGrid = this.context.getBean(SendGrid.class);
80+
CloseableHttpClient client = (CloseableHttpClient) ReflectionTestUtils.getField(
81+
sendGrid, "client");
82+
HttpRoutePlanner routePlanner = (HttpRoutePlanner) ReflectionTestUtils.getField(
83+
client, "routePlanner");
84+
assertThat(routePlanner, instanceOf(DefaultProxyRoutePlanner.class));
85+
}
86+
87+
private void loadContext(String... environment) {
88+
this.loadContext(null, environment);
89+
}
90+
91+
private void loadContext(Class<?> additionalConfiguration, String... environment) {
92+
this.context = new AnnotationConfigApplicationContext();
93+
EnvironmentTestUtils.addEnvironment(this.context, environment);
94+
this.context.register(SendGridAutoConfiguration.class);
95+
if (additionalConfiguration != null) {
96+
this.context.register(additionalConfiguration);
97+
}
98+
this.context.refresh();
99+
}
100+
101+
@Configuration
102+
static class ManualSendGridConfiguration {
103+
104+
@Bean
105+
SendGrid sendGrid() {
106+
return new SendGrid("manual-user", "manual-secret");
107+
}
108+
}
109+
}

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<spring-social-linkedin.version>1.0.1.RELEASE</spring-social-linkedin.version>
129129
<spring-social-twitter.version>1.1.0.RELEASE</spring-social-twitter.version>
130130
<spring-ws.version>2.2.0.RELEASE</spring-ws.version>
131+
<sendgrid.version>2.1.0</sendgrid.version>
131132
<sun-mail.version>${javax-mail.version}</sun-mail.version>
132133
<thymeleaf.version>2.1.4.RELEASE</thymeleaf.version>
133134
<thymeleaf-extras-springsecurity3.version>2.1.1.RELEASE</thymeleaf-extras-springsecurity3.version>
@@ -512,6 +513,11 @@
512513
<artifactId>jmustache</artifactId>
513514
<version>${jmustache.version}</version>
514515
</dependency>
516+
<dependency>
517+
<groupId>com.sendgrid</groupId>
518+
<artifactId>sendgrid-java</artifactId>
519+
<version>${sendgrid.version}</version>
520+
</dependency>
515521
<dependency>
516522
<groupId>com.sun.mail</groupId>
517523
<artifactId>javax.mail</artifactId>

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ content into your application; rather pick only the properties that you need.
573573
shell.auth.simple.user.password=
574574
shell.auth.spring.roles=
575575
576+
# SENDGRID ({sc-spring-boot-autoconfigure}/sendgrid/SendGridAutoConfiguration.{sc-ext}[SendGridAutoConfiguration])
577+
spring.sendgrid.username= # SendGrid account username
578+
spring.sendgrid.password= # SendGrid account password
579+
spring.sendgrid.proxy.host= # SendGrid proxy host
580+
spring.sendgrid.proxy.port= # SendGrid proxy port
581+
576582
# GIT INFO
577583
spring.git.properties= # resource ref to generated git info properties file
578584
----

spring-boot/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>jackson-databind</artifactId>
5555
<optional>true</optional>
5656
</dependency>
57+
<dependency>
58+
<groupId>com.sendgrid</groupId>
59+
<artifactId>sendgrid-java</artifactId>
60+
<optional>true</optional>
61+
</dependency>
5762
<dependency>
5863
<groupId>com.google.code.gson</groupId>
5964
<artifactId>gson</artifactId>

0 commit comments

Comments
 (0)