@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 2015 the original author or authors .
* Copyright 2012 - 2016 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,22 +22,25 @@ import org.apache.http.impl.client.HttpClientBuilder;
@@ -22,22 +22,25 @@ 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.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 ;
/ * *
* { @link EnableAutoConfiguration Auto - configuration } for SendGrid .
*
* @author Maciej Walkowiak
* @author Patrick Bray
* @since 1 . 3 . 0
* /
@Configuration
@ConditionalOnClass ( SendGrid . class )
@ConditionalOnProperty ( prefix = "spring.sendgrid" , value = "username" )
@Conditional ( SendGridAutoConfiguration . SendGridPropertyCondition . class )
@EnableConfigurationProperties ( SendGridProperties . class )
public class SendGridAutoConfiguration {
@ -47,8 +50,7 @@ public class SendGridAutoConfiguration {
@@ -47,8 +50,7 @@ public class SendGridAutoConfiguration {
@Bean
@ConditionalOnMissingBean ( SendGrid . class )
public SendGrid sendGrid ( ) {
SendGrid sendGrid = new SendGrid ( this . properties . getUsername ( ) ,
this . properties . getPassword ( ) ) ;
SendGrid sendGrid = createSendGrid ( ) ;
if ( this . properties . isProxyConfigured ( ) ) {
HttpHost proxy = new HttpHost ( this . properties . getProxy ( ) . getHost ( ) ,
this . properties . getProxy ( ) . getPort ( ) ) ;
@ -58,4 +60,29 @@ public class SendGridAutoConfiguration {
@@ -58,4 +60,29 @@ public class SendGridAutoConfiguration {
return sendGrid ;
}
private SendGrid createSendGrid ( ) {
if ( this . properties . getApiKey ( ) ! = null ) {
return new SendGrid ( this . properties . getApiKey ( ) ) ;
}
else {
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 {
}
}
}