Commit 3bf6c2fe authored by Patrick Bray's avatar Patrick Bray Committed by Stephane Nicoll

Support for SendGrid ApiKey

See gh-4574
parent f9fd849a
...@@ -22,11 +22,13 @@ import org.apache.http.impl.client.HttpClientBuilder; ...@@ -22,11 +22,13 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
...@@ -37,7 +39,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -37,7 +39,7 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration @Configuration
@ConditionalOnClass(SendGrid.class) @ConditionalOnClass(SendGrid.class)
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username") @Conditional(SendGridAutoConfiguration.SendGridPropertyCondition.class)
@EnableConfigurationProperties(SendGridProperties.class) @EnableConfigurationProperties(SendGridProperties.class)
public class SendGridAutoConfiguration { public class SendGridAutoConfiguration {
...@@ -47,15 +49,39 @@ public class SendGridAutoConfiguration { ...@@ -47,15 +49,39 @@ public class SendGridAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(SendGrid.class) @ConditionalOnMissingBean(SendGrid.class)
public SendGrid sendGrid() { public SendGrid sendGrid() {
SendGrid sendGrid = new SendGrid(this.properties.getUsername(),
this.properties.getPassword()); SendGrid sendGrid;
if (this.properties.getApikey() != null) {
sendGrid = new SendGrid(this.properties.getApikey());
}
else {
sendGrid = new SendGrid(this.properties.getUsername(),
this.properties.getPassword());
}
if (this.properties.isProxyConfigured()) { if (this.properties.isProxyConfigured()) {
HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(), HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(),
this.properties.getProxy().getPort()); this.properties.getProxy().getPort());
sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy) sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy)
.setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build()); .setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build());
} }
return sendGrid; return sendGrid;
} }
static class SendGridPropertyCondition extends AnyNestedCondition {
SendGridPropertyCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username")
private class SendGridUserProperty {
}
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "apikey")
private class SendGridApiKeyProperty {
}
}
} }
...@@ -37,11 +37,24 @@ public class SendGridProperties { ...@@ -37,11 +37,24 @@ public class SendGridProperties {
*/ */
private String password; private String password;
/**
* SendGrid api key.
*/
private String apikey;
/** /**
* Proxy configuration. * Proxy configuration.
*/ */
private Proxy proxy; private Proxy proxy;
public String getApikey() {
return this.apikey;
}
public void setApikey(final String apikey) {
this.apikey = apikey;
}
public String getUsername() { public String getUsername() {
return this.username; return this.username;
} }
......
...@@ -58,6 +58,17 @@ public class SendGridAutoConfigurationTests { ...@@ -58,6 +58,17 @@ public class SendGridAutoConfigurationTests {
assertEquals("secret", ReflectionTestUtils.getField(sendGrid, "password")); assertEquals("secret", ReflectionTestUtils.getField(sendGrid, "password"));
} }
@Test
public void expectedSendGridBeanCreated_UsingApiKey() {
loadContext("spring.sendgrid.apikey:SG.SECRET-API-KEY");
SendGrid sendGrid = this.context.getBean(SendGrid.class);
assertEquals("SG.SECRET-API-KEY",
ReflectionTestUtils.getField(sendGrid, "password"));
}
@Test(expected = NoSuchBeanDefinitionException.class) @Test(expected = NoSuchBeanDefinitionException.class)
public void autoConfigurationNotFiredWhenPropertiesNotSet() { public void autoConfigurationNotFiredWhenPropertiesNotSet() {
loadContext(); loadContext();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment