001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.sendgrid; 018 019import com.sendgrid.SendGrid; 020import org.apache.http.HttpHost; 021import org.apache.http.impl.client.HttpClientBuilder; 022 023import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 024import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 028import org.springframework.boot.context.properties.EnableConfigurationProperties; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Conditional; 031import org.springframework.context.annotation.Configuration; 032 033/** 034 * {@link EnableAutoConfiguration Auto-configuration} for SendGrid. 035 * 036 * @author Maciej Walkowiak 037 * @author Patrick Bray 038 * @since 1.3.0 039 */ 040@Configuration 041@ConditionalOnClass(SendGrid.class) 042@Conditional(SendGridAutoConfiguration.SendGridPropertyCondition.class) 043@EnableConfigurationProperties(SendGridProperties.class) 044public class SendGridAutoConfiguration { 045 046 private final SendGridProperties properties; 047 048 public SendGridAutoConfiguration(SendGridProperties properties) { 049 this.properties = properties; 050 } 051 052 @Bean 053 @ConditionalOnMissingBean(SendGrid.class) 054 public SendGrid sendGrid() { 055 SendGrid sendGrid = createSendGrid(); 056 if (this.properties.isProxyConfigured()) { 057 HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(), 058 this.properties.getProxy().getPort()); 059 sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy) 060 .setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build()); 061 } 062 return sendGrid; 063 } 064 065 private SendGrid createSendGrid() { 066 if (this.properties.getApiKey() != null) { 067 return new SendGrid(this.properties.getApiKey()); 068 } 069 return new SendGrid(this.properties.getUsername(), this.properties.getPassword()); 070 } 071 072 static class SendGridPropertyCondition extends AnyNestedCondition { 073 074 SendGridPropertyCondition() { 075 super(ConfigurationPhase.PARSE_CONFIGURATION); 076 } 077 078 @ConditionalOnProperty(prefix = "spring.sendgrid", value = "username") 079 static class SendGridUserProperty { 080 081 } 082 083 @ConditionalOnProperty(prefix = "spring.sendgrid", value = "api-key") 084 static class SendGridApiKeyProperty { 085 086 } 087 088 } 089 090}