Commit 22b08f1a by Dave Syer

Fix Feign Ribbon with Apache HTTP client

The Apcahe client does not allow empty body for PATCH, nor does it allow a request to be pre-populated with a Content-Length. Spring Cloud tests were trying to do the former, and Feign allows the latter.
parent 65ec7730
......@@ -59,19 +59,19 @@ public class FeignClientsConfiguration {
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
}
@Bean
@ConditionalOnMissingBean
public Encoder feignEncoder() {
return new SpringEncoder(messageConverters);
return new SpringEncoder(this.messageConverters);
}
@Bean
@ConditionalOnMissingBean
public Contract feignContract() {
return new SpringMvcContract(parameterProcessors);
return new SpringMvcContract(this.parameterProcessors);
}
@Configuration
......@@ -101,9 +101,10 @@ public class FeignClientsConfiguration {
private HttpClient httpClient;
@ConditionalOnMissingBean
@Bean
public Client feignClient() {
if (httpClient != null) {
return new ApacheHttpClient(httpClient);
if (this.httpClient != null) {
return new ApacheHttpClient(this.httpClient);
}
return new ApacheHttpClient();
}
......
......@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.feign.ribbon;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
......@@ -39,6 +40,7 @@ import feign.Client;
import feign.Request;
import feign.RequestTemplate;
import feign.Response;
import feign.Util;
public class FeignLoadBalancer extends
AbstractLoadBalancerAwareClient<FeignLoadBalancer.RibbonRequest, FeignLoadBalancer.RibbonResponse> {
......@@ -112,14 +114,22 @@ public class FeignLoadBalancer extends
RibbonRequest(Client client, Request request, URI uri) {
this.client = client;
this.request = request;
setUri(uri);
this.request = toRequest(request);
}
private Request toRequest(Request request) {
Map<String, Collection<String>> headers = new LinkedHashMap<>(
request.headers());
// Apache client barfs if you set the content length
headers.remove(Util.CONTENT_LENGTH);
return new RequestTemplate().method(request.method())
.append(getUri().toASCIIString())
.body(request.body(), request.charset()).headers(headers).request();
}
Request toRequest() {
return new RequestTemplate().method(this.request.method())
.append(getUri().toASCIIString()).headers(this.request.headers())
.body(this.request.body(), this.request.charset()).request();
return toRequest(this.request);
}
Client client() {
......
......@@ -63,7 +63,6 @@ public class FeignRibbonClientAutoConfiguration {
@Configuration
@ConditionalOnClass(ApacheHttpClient.class)
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
@ConditionalOnMissingBean(Client.class)
protected static class HttpClientConfiguration {
@Autowired(required = false)
......@@ -73,6 +72,7 @@ public class FeignRibbonClientAutoConfiguration {
CachingSpringLoadBalancerFactory cachingFactory;
@Bean
@ConditionalOnMissingBean(Client.class)
public Client feignClient() {
ApacheHttpClient delegate;
if (this.httpClient != null) {
......@@ -88,7 +88,6 @@ public class FeignRibbonClientAutoConfiguration {
@Configuration
@ConditionalOnClass(OkHttpClient.class)
@ConditionalOnProperty(value = "feign.okhttp.enabled", matchIfMissing = true)
@ConditionalOnMissingBean(Client.class)
protected static class OkHttpConfiguration {
@Autowired(required = false)
......@@ -98,6 +97,7 @@ public class FeignRibbonClientAutoConfiguration {
CachingSpringLoadBalancerFactory cachingFactory;
@Bean
@ConditionalOnMissingBean(Client.class)
public Client feignClient() {
OkHttpClient delegate;
if (this.okHttpClient != null) {
......
......@@ -36,7 +36,8 @@ public class LoadBalancerFeignClient implements Client {
private final Client delegate;
private CachingSpringLoadBalancerFactory lbClientFactory;
public LoadBalancerFeignClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory) {
public LoadBalancerFeignClient(Client delegate,
CachingSpringLoadBalancerFactory lbClientFactory) {
this.delegate = delegate;
this.lbClientFactory = lbClientFactory;
}
......@@ -47,8 +48,8 @@ public class LoadBalancerFeignClient implements Client {
URI asUri = URI.create(request.url());
String clientName = asUri.getHost();
URI uriWithoutHost = cleanUrl(request.url(), clientName);
FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(this.delegate,
request, uriWithoutHost);
FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
this.delegate, request, uriWithoutHost);
return lbClient(clientName).executeWithLoadBalancer(ribbonRequest,
new FeignOptionsClientConfig(options)).toResponse();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment