Browse Source

Polish "Allow to customize OkHttpClient.Builder"

Closes gh-9669
pull/8048/merge
Stephane Nicoll 9 years ago
parent
commit
663ff05895
  1. 23
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java
  2. 11
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java

23
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java

@ -18,7 +18,7 @@ package org.springframework.boot.autoconfigure.influx; @@ -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; @@ -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 { @@ -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());
}
}

11
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java

@ -20,16 +20,15 @@ import java.util.concurrent.TimeUnit; @@ -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 { @@ -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();
}

Loading…
Cancel
Save