From 3bf6c2fe1f6117ae1dbe8e51f78faaa8fefe0991 Mon Sep 17 00:00:00 2001 From: Patrick Bray Date: Sat, 21 Nov 2015 18:53:10 +1100 Subject: [PATCH 1/2] Support for SendGrid ApiKey See gh-4574 --- .../sendgrid/SendGridAutoConfiguration.java | 32 +++++++++++++++++-- .../sendgrid/SendGridProperties.java | 13 ++++++++ .../SendGridAutoConfigurationTests.java | 11 +++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java index afda757b17d..0f4bbd23003 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java @@ -22,11 +22,13 @@ 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; /** @@ -37,7 +39,7 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @ConditionalOnClass(SendGrid.class) -@ConditionalOnProperty(prefix = "spring.sendgrid", value = "username") +@Conditional(SendGridAutoConfiguration.SendGridPropertyCondition.class) @EnableConfigurationProperties(SendGridProperties.class) public class SendGridAutoConfiguration { @@ -47,15 +49,39 @@ public class SendGridAutoConfiguration { @Bean @ConditionalOnMissingBean(SendGrid.class) 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()) { 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 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 { + } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java index 480933a8fb6..699f487af85 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java @@ -37,11 +37,24 @@ public class SendGridProperties { */ private String password; + /** + * SendGrid api key. + */ + private String apikey; + /** * Proxy configuration. */ private Proxy proxy; + public String getApikey() { + return this.apikey; + } + + public void setApikey(final String apikey) { + this.apikey = apikey; + } + public String getUsername() { return this.username; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java index 09d337fe7f5..09cf9460461 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java @@ -58,6 +58,17 @@ public class SendGridAutoConfigurationTests { 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) public void autoConfigurationNotFiredWhenPropertiesNotSet() { loadContext(); From 3562026e9a2aa6a970b2acf83bb7632a151a6b97 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 26 Jan 2016 15:18:32 +0100 Subject: [PATCH 2/2] Polish contribution Closes gh-4574 --- .../sendgrid/SendGridAutoConfiguration.java | 33 ++++++++++--------- .../sendgrid/SendGridProperties.java | 24 +++++++------- .../SendGridAutoConfigurationTests.java | 15 ++++----- .../appendix-application-properties.adoc | 1 + 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java index 0f4bbd23003..23006e1caf7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfiguration.java @@ -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. @@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration; * {@link EnableAutoConfiguration Auto-configuration} for SendGrid. * * @author Maciej Walkowiak + * @author Patrick Bray * @since 1.3.0 */ @Configuration @@ -49,27 +50,27 @@ public class SendGridAutoConfiguration { @Bean @ConditionalOnMissingBean(SendGrid.class) public SendGrid sendGrid() { - - SendGrid sendGrid; - - if (this.properties.getApikey() != null) { - sendGrid = new SendGrid(this.properties.getApikey()); - } - else { - 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()); sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy) .setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build()); } - 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() { @@ -77,11 +78,11 @@ public class SendGridAutoConfiguration { } @ConditionalOnProperty(prefix = "spring.sendgrid", value = "username") - private class SendGridUserProperty { + static class SendGridUserProperty { } - @ConditionalOnProperty(prefix = "spring.sendgrid", value = "apikey") - private class SendGridApiKeyProperty { + @ConditionalOnProperty(prefix = "spring.sendgrid", value = "api-key") + static class SendGridApiKeyProperty { } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java index 699f487af85..6a97a2a32a7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sendgrid/SendGridProperties.java @@ -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. @@ -28,7 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; public class SendGridProperties { /** - * SendGrid username. + * SendGrid username. Alternative to api key. */ private String username; @@ -38,23 +38,15 @@ public class SendGridProperties { private String password; /** - * SendGrid api key. + * SendGrid api key. Alternative to username/password. */ - private String apikey; + private String apiKey; /** * Proxy configuration. */ private Proxy proxy; - public String getApikey() { - return this.apikey; - } - - public void setApikey(final String apikey) { - this.apikey = apikey; - } - public String getUsername() { return this.username; } @@ -71,6 +63,14 @@ public class SendGridProperties { this.password = password; } + public String getApiKey() { + return this.apiKey; + } + + public void setApiKey(final String apiKey) { + this.apiKey = apiKey; + } + public Proxy getProxy() { return this.proxy; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java index 09cf9460461..59f57a9d931 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java @@ -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. @@ -38,6 +38,7 @@ import static org.junit.Assert.assertThat; * Tests for {@link SendGridAutoConfiguration}. * * @author Maciej Walkowiak + * @author Patrick Bray */ public class SendGridAutoConfigurationTests { @@ -51,7 +52,7 @@ public class SendGridAutoConfigurationTests { } @Test - public void expectedSendGridBeanCreated() { + public void expectedSendGridBeanCreatedUsername() { loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret"); SendGrid sendGrid = this.context.getBean(SendGrid.class); assertEquals("user", ReflectionTestUtils.getField(sendGrid, "username")); @@ -59,14 +60,10 @@ public class SendGridAutoConfigurationTests { } @Test - public void expectedSendGridBeanCreated_UsingApiKey() { - - loadContext("spring.sendgrid.apikey:SG.SECRET-API-KEY"); - + public void expectedSendGridBeanCreatedApiKey() { + loadContext("spring.sendgrid.apiKey:SG.SECRET-API-KEY"); SendGrid sendGrid = this.context.getBean(SendGrid.class); - - assertEquals("SG.SECRET-API-KEY", - ReflectionTestUtils.getField(sendGrid, "password")); + assertEquals("SG.SECRET-API-KEY", ReflectionTestUtils.getField(sendGrid, "password")); } @Test(expected = NoSuchBeanDefinitionException.class) diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 601cee365e5..07f648d15ae 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -120,6 +120,7 @@ content into your application; rather pick only the properties that you need. spring.profiles.include= # Unconditionally activate the specified comma separated profiles. # 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