Skip to content

Auto-configuration for SendGrid's Java client #2280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2012-2014 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 com.sendgrid.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;

/**
* {@link EnableAutoConfiguration Auto-configuration} for SendGrid
*
* @author Maciej Walkowiak
* @since 1.2.1
*/
@Configuration
@ConditionalOnClass(SendGrid.class)
@ConditionalOnProperty(prefix = "sendgrid", value = "username")
@ConditionalOnMissingBean(SendGrid.class)
@EnableConfigurationProperties(SendGridProperties.class)
public class SendGridAutoConfiguration {
@Autowired
private SendGridProperties properties;
@Bean
public SendGrid sendGrid() {
SendGrid sendGrid = new SendGrid(properties.getUsername(), properties.getPassword());

if (properties.isProxyConfigured()) {
HttpHost proxy = new HttpHost(properties.getProxy().getHost(), properties.getProxy().getPort());
CloseableHttpClient http = HttpClientBuilder.create()
.setProxy(proxy)
.setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java")
.build();

sendGrid.setClient(http);
}

return sendGrid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2012-2014 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.2.1
*/
@ConfigurationProperties(prefix = "sendgrid")
public class SendGridProperties {
/**
* SendGrid username
*/
private String username;

/**
* SendGrid password
*/
private String password;

/**
* Proxy configuration
*/
private Proxy proxy;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Proxy getProxy() {
return proxy;
}

public void setProxy(Proxy proxy) {
this.proxy = proxy;
}

public boolean isProxyConfigured() {
return proxy != null
&& proxy.getHost() != null
&& proxy.getPort() != null;
}

public static class Proxy {
/**
* SendGrid proxy host
*/
private String host;

/**
* SendGrid proxy port
*/
private Integer port;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public Integer getPort() {
return port;
}

public void setPort(Integer port) {
this.port = port;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2012-2014 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 com.sendgrid.SendGrid;
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 static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* 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() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.username:user");
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.password:secret");
this.context.register(SendGridAutoConfiguration.class);
this.context.refresh();

SendGrid bean = this.context.getBean(SendGrid.class);
assertNotNull(bean);
}

@Test
public void autoConfigurationNotFiredWhenPropertiesNotSet() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(SendGridAutoConfiguration.class);
this.context.refresh();

try {
this.context.getBean(SendGrid.class);
fail("Unexpected bean in context of type SendGrid");
} catch (NoSuchBeanDefinitionException ignored) {
}
}

@Test
public void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
final String username = "user";
final String password = "secret";

this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.username:" + username, "sendgrid.password:" + password);
this.context.register(SendGridAutoConfiguration.class, ManualSendGridConfiguration.class);
this.context.refresh();

SendGrid bean = this.context.getBean(SendGrid.class);

assertNotEquals(username, ReflectionTestUtils.getField(bean, "username"));
assertNotEquals(password, ReflectionTestUtils.getField(bean, "password"));
}

@Test
public void expectedSendGridBeanWithProxyCreated() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.username:user");
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.password:secret");
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.proxy.host:localhost");
EnvironmentTestUtils.addEnvironment(this.context, "sendgrid.proxy.port:5678");
this.context.register(SendGridAutoConfiguration.class);
this.context.refresh();

SendGrid bean = this.context.getBean(SendGrid.class);
assertNotNull(bean);
}

@Configuration
static class ManualSendGridConfiguration {

@Bean
SendGrid sendGrid() {
return new SendGrid("manual-user", "manual-secret");
}
}
}
6 changes: 6 additions & 0 deletions spring-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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.0.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>
Expand Down Expand Up @@ -483,6 +484,11 @@
<artifactId>json-path</artifactId>
<version>${json-path.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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,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])
sendgrid.username= # sendgrid account username
sendgrid.password= # sendgrid account password
sendgrid.proxy.host=
sendgrid.proxy.port=

# GIT INFO
spring.git.properties= # resource ref to generated git info properties file
----
5 changes: 5 additions & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down