parent
97a3a94042
commit
47807b8925
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
|
@ -16,18 +16,17 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.sendgrid;
|
||||
|
||||
import com.sendgrid.Client;
|
||||
import com.sendgrid.SendGrid;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
|
||||
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.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.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
|
|
@ -35,11 +34,12 @@ import org.springframework.context.annotation.Configuration;
|
|||
*
|
||||
* @author Maciej Walkowiak
|
||||
* @author Patrick Bray
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(SendGrid.class)
|
||||
@Conditional(SendGridAutoConfiguration.SendGridPropertyCondition.class)
|
||||
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "api-key")
|
||||
@EnableConfigurationProperties(SendGridProperties.class)
|
||||
public class SendGridAutoConfiguration {
|
||||
|
||||
|
|
@ -52,39 +52,13 @@ public class SendGridAutoConfiguration {
|
|||
@Bean
|
||||
@ConditionalOnMissingBean(SendGrid.class)
|
||||
public SendGrid sendGrid() {
|
||||
SendGrid sendGrid = createSendGrid();
|
||||
if (this.properties.isProxyConfigured()) {
|
||||
HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(),
|
||||
this.properties.getProxy().getPort());
|
||||
sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy)
|
||||
.setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build());
|
||||
return new SendGrid(this.properties.getApiKey(),
|
||||
new Client(HttpClientBuilder.create().setProxy(proxy).build()));
|
||||
}
|
||||
return sendGrid;
|
||||
}
|
||||
|
||||
private SendGrid createSendGrid() {
|
||||
if (this.properties.getApiKey() != null) {
|
||||
return new SendGrid(this.properties.getApiKey());
|
||||
}
|
||||
return new SendGrid(this.properties.getUsername(), this.properties.getPassword());
|
||||
}
|
||||
|
||||
static class SendGridPropertyCondition extends AnyNestedCondition {
|
||||
|
||||
SendGridPropertyCondition() {
|
||||
super(ConfigurationPhase.PARSE_CONFIGURATION);
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username")
|
||||
static class SendGridUserProperty {
|
||||
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(prefix = "spring.sendgrid", value = "api-key")
|
||||
static class SendGridApiKeyProperty {
|
||||
|
||||
}
|
||||
|
||||
return new SendGrid(this.properties.getApiKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
|
@ -22,21 +22,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||
* {@link ConfigurationProperties} for SendGrid.
|
||||
*
|
||||
* @author Maciej Walkowiak
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.sendgrid")
|
||||
public class SendGridProperties {
|
||||
|
||||
/**
|
||||
* SendGrid username. Alternative to api key.
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* SendGrid password.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* SendGrid api key. Alternative to username/password.
|
||||
*/
|
||||
|
|
@ -47,22 +38,6 @@ public class SendGridProperties {
|
|||
*/
|
||||
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 String getApiKey() {
|
||||
return this.apiKey;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,19 +47,11 @@ public class SendGridAutoConfigurationTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSendGridBeanCreatedUsername() {
|
||||
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertThat(sendGrid).extracting("username").containsExactly("user");
|
||||
assertThat(sendGrid).extracting("password").containsExactly("secret");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSendGridBeanCreatedApiKey() {
|
||||
loadContext("spring.sendgrid.apiKey:SG.SECRET-API-KEY");
|
||||
loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertThat(sendGrid).extracting("password").containsExactly("SG.SECRET-API-KEY");
|
||||
assertThat(sendGrid).extracting("apiKey").containsExactly("SG.SECRET-API-KEY");
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
|
|
@ -70,20 +62,20 @@ public class SendGridAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
|
||||
loadContext(ManualSendGridConfiguration.class, "spring.sendgrid.username:user",
|
||||
"spring.sendgrid.password:secret");
|
||||
loadContext(ManualSendGridConfiguration.class,
|
||||
"spring.sendgrid.api-key:SG.SECRET-API-KEY");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertThat(sendGrid).extracting("username").containsExactly("manual-user");
|
||||
assertThat(sendGrid).extracting("password").containsExactly("manual-secret");
|
||||
assertThat(sendGrid).extracting("apiKey").containsExactly("SG.CUSTOM_API_KEY");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSendGridBeanWithProxyCreated() {
|
||||
loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret",
|
||||
loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY",
|
||||
"spring.sendgrid.proxy.host:localhost",
|
||||
"spring.sendgrid.proxy.port:5678");
|
||||
SendGrid sendGrid = this.context.getBean(SendGrid.class);
|
||||
assertThat(sendGrid).extracting("client").extracting("routePlanner")
|
||||
assertThat(sendGrid).extracting("client").extracting("httpClient")
|
||||
.extracting("routePlanner")
|
||||
.hasOnlyElementsOfType(DefaultProxyRoutePlanner.class);
|
||||
}
|
||||
|
||||
|
|
@ -107,7 +99,7 @@ public class SendGridAutoConfigurationTests {
|
|||
|
||||
@Bean
|
||||
SendGrid sendGrid() {
|
||||
return new SendGrid("manual-user", "manual-secret");
|
||||
return new SendGrid("SG.CUSTOM_API_KEY", true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@
|
|||
<rxjava2.version>2.1.0</rxjava2.version>
|
||||
<selenium.version>3.4.0</selenium.version>
|
||||
<selenium-htmlunit.version>2.26</selenium-htmlunit.version>
|
||||
<sendgrid.version>2.2.2</sendgrid.version>
|
||||
<sendgrid.version>3.2.0</sendgrid.version>
|
||||
<servlet-api.version>3.1.0</servlet-api.version>
|
||||
<simple-json.version>1.1.1</simple-json.version>
|
||||
<slf4j.version>1.7.25</slf4j.version>
|
||||
|
|
|
|||
|
|
@ -134,8 +134,6 @@ content into your application; rather pick only the properties that you need.
|
|||
|
||||
# SENDGRID ({sc-spring-boot-autoconfigure}/sendgrid/SendGridAutoConfiguration.{sc-ext}[SendGridAutoConfiguration])
|
||||
spring.sendgrid.api-key= # SendGrid api key (alternative to username/password)
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue