Polish "Allow to customize OkHttpClient.Builder"

Closes gh-9669
This commit is contained in:
Stephane Nicoll
2017-07-25 08:37:04 +02:00
parent 406f41740e
commit 663ff05895
2 changed files with 9 additions and 25 deletions

View File

@@ -18,7 +18,7 @@ package org.springframework.boot.autoconfigure.influx;
import okhttp3.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.impl.InfluxDBImpl;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -28,7 +28,6 @@ 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.Configuration;
import org.springframework.util.StringUtils;
/**
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
@@ -50,29 +49,15 @@ public class InfluxDbAutoConfiguration {
public InfluxDbAutoConfiguration(InfluxDbProperties properties,
ObjectProvider<OkHttpClient.Builder> builder) {
this.properties = properties;
this.builder = builder.getIfAvailable();
this.builder = builder.getIfAvailable(OkHttpClient.Builder::new);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty("spring.influx.url")
public InfluxDB influxDb() {
if (hasCredentials() && this.builder != null) {
return InfluxDBFactory.connect(this.properties.getUrl(),
this.properties.getUser(), this.properties.getUrl(), this.builder);
}
else if (hasCredentials()) {
return InfluxDBFactory.connect(this.properties.getUrl(),
this.properties.getUser(), this.properties.getPassword());
}
else if (this.builder != null) {
return InfluxDBFactory.connect(this.properties.getUrl(), this.builder);
}
return InfluxDBFactory.connect(this.properties.getUrl());
return new InfluxDBImpl(this.properties.getUrl(), this.properties.getUser(),
this.properties.getPassword(), this.builder);
}
private boolean hasCredentials() {
return StringUtils.hasText(this.properties.getUser())
&& StringUtils.hasText(this.properties.getPassword());
}
}

View File

@@ -20,16 +20,15 @@ import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.impl.InfluxDBImpl;
import org.junit.After;
import org.junit.Test;
import retrofit2.Retrofit;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -91,10 +90,10 @@ public class InfluxDbAutoConfigurationTests {
private int getReadTimeoutProperty() {
InfluxDB influxDB = this.context.getBean(InfluxDB.class);
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB,
InfluxDBImpl.class, "retrofit");
OkHttpClient callFactory = (OkHttpClient) ReflectionTestUtils.getField(retrofit,
Retrofit.class, "callFactory");
Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB)
.getPropertyValue("retrofit");
OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit)
.getPropertyValue("callFactory");
return callFactory.readTimeoutMillis();
}