From 8207852bcd6749275a840683bfa64ec49000ab1f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 22 Jul 2017 08:10:04 +0100 Subject: [PATCH 1/2] Ensure that detected request factories are initialized Closes gh-9797 --- .../boot/web/client/RestTemplateBuilder.java | 18 ++++++- .../client/RestTemplateBuilderNettyTests.java | 49 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderNettyTests.java diff --git a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java index 0220c03edd..f8edf46254 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Set; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.InitializingBean; import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; @@ -591,12 +592,27 @@ public class RestTemplateBuilder { if (ClassUtils.isPresent(candidate.getKey(), classLoader)) { Class factoryClass = ClassUtils.resolveClassName(candidate.getValue(), classLoader); - return (ClientHttpRequestFactory) BeanUtils.instantiate(factoryClass); + ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory) BeanUtils + .instantiate(factoryClass); + initializeIfNecessary(requestFactory); + return requestFactory; } } return new SimpleClientHttpRequestFactory(); } + private void initializeIfNecessary(ClientHttpRequestFactory requestFactory) { + if (requestFactory instanceof InitializingBean) { + try { + ((InitializingBean) requestFactory).afterPropertiesSet(); + } + catch (Exception ex) { + throw new IllegalStateException( + "Failed to initialize request factory " + requestFactory, ex); + } + } + } + private Set append(Set set, T addition) { Set result = new LinkedHashSet( set == null ? Collections.emptySet() : set); diff --git a/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderNettyTests.java b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderNettyTests.java new file mode 100644 index 0000000000..b0e6ecaa0e --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderNettyTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.client; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.junit.runner.classpath.ClassPathExclusions; +import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.Netty4ClientHttpRequestFactory; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RestTemplateBuilder} when Netty is used by default. + * + * @author Andy Wilkinson + */ +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions({ "httpclient-*.jar", "okhttp-*.jar" }) +public class RestTemplateBuilderNettyTests { + + @Test + public void sslContextIsInitialized() { + RestTemplate restTemplate = new RestTemplateBuilder().build(); + ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory(); + assertThat(requestFactory).isInstanceOf(Netty4ClientHttpRequestFactory.class); + assertThat(ReflectionTestUtils.getField(requestFactory, "sslContext")) + .isNotNull(); + } + +} From 39b4ecdf63c8ca794a9b820e9a4564318747d4d5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 22 Jul 2017 08:11:26 +0100 Subject: [PATCH 2/2] Adapt to breaking change in Spring Data See gh-9834 --- .../AbstractRepositoryConfigurationSourceSupport.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java index d011830734..10213185ae 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java @@ -55,16 +55,17 @@ public abstract class AbstractRepositoryConfigurationSourceSupport @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - new RepositoryConfigurationDelegate(getConfigurationSource(), this.resourceLoader, - this.environment).registerRepositoriesIn(registry, + new RepositoryConfigurationDelegate(getConfigurationSource(registry), + this.resourceLoader, this.environment).registerRepositoriesIn(registry, getRepositoryConfigurationExtension()); } - private AnnotationRepositoryConfigurationSource getConfigurationSource() { + private AnnotationRepositoryConfigurationSource getConfigurationSource( + BeanDefinitionRegistry registry) { StandardAnnotationMetadata metadata = new StandardAnnotationMetadata( getConfiguration(), true); return new AnnotationRepositoryConfigurationSource(metadata, getAnnotation(), - this.resourceLoader, this.environment) { + this.resourceLoader, this.environment, registry) { @Override public java.lang.Iterable getBasePackages() { return AbstractRepositoryConfigurationSourceSupport.this