Skip to content

Commit 68c4513

Browse files
Fix condition. (#834)
1 parent ec245b0 commit 68c4513

File tree

6 files changed

+101
-18
lines changed

6 files changed

+101
-18
lines changed

docs/src/main/asciidoc/spring-cloud-openfeign.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ feign.compression.request.min-request-size=2048
561561

562562
These properties allow you to be selective about the compressed media types and minimum request threshold length.
563563

564+
TIP: Since the OkHttpClient uses "transparent" compression, that is disabled if the `content-encoding` or `accept-encoding` header is present, we do not enable compression when `feign.okhttp.OkHttpClient` is present on the classpath and `feign.okhttp.enabled` is set to `true`.
565+
564566
=== Feign logging
565567

566568
A logger is created for each Feign client created. By default the name of the logger is the full class name of the interface used to create the Feign client. Feign logging only responds to the `DEBUG` level.

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignCircuitBreakerInvocationHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -154,12 +154,12 @@ private Supplier<Object> asSupplier(final Method method, final Object[] args) {
154154
}
155155

156156
/**
157-
* If the method param of InvocationHandler.invoke is not accessible, i.e in a
158-
* package-private interface, the fallback call will cause of access restrictions. But
159-
* methods in dispatch are copied methods. So setting access to dispatch method
160-
* doesn't take effect to the method in InvocationHandler.invoke. Use map to store a
161-
* copy of method to invoke the fallback to bypass this and reducing the count of
162-
* reflection calls.
157+
* If the method param of {@link InvocationHandler#invoke(Object, Method, Object[])}
158+
* is not accessible, i.e in a package-private interface, the fallback call will cause
159+
* of access restrictions. But methods in dispatch are copied methods. So setting
160+
* access to dispatch method doesn't take effect to the method in
161+
* InvocationHandler.invoke. Use map to store a copy of method to invoke the fallback
162+
* to bypass this and reducing the count of reflection calls.
163163
* @return cached methods map for fallback invoking
164164
*/
165165
static Map<Method, Method> toFallbackMethod(Map<Method, InvocationHandlerFactory.MethodHandler> dispatch) {

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignAcceptGzipEncodingAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,17 +22,18 @@
2222
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2625
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2726
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2827
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
2928
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Conditional;
3030
import org.springframework.context.annotation.Configuration;
3131

3232
/**
3333
* Configures the Feign response compression.
3434
*
3535
* @author Jakub Narloch
36+
* @author Olga Maciaszek-Sharma
3637
* @see FeignAcceptGzipEncodingInterceptor
3738
*/
3839
@Configuration(proxyBeanMethods = false)
@@ -41,8 +42,8 @@
4142
@ConditionalOnBean(Client.class)
4243
@ConditionalOnProperty(value = "feign.compression.response.enabled", matchIfMissing = false)
4344
// The OK HTTP client uses "transparent" compression.
44-
// If the accept-encoding header is present it disable transparent compression
45-
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
45+
// If the accept-encoding header is present, it disables transparent compression.
46+
@Conditional(OkHttpFeignClientBeanMissingCondition.class)
4647
@AutoConfigureAfter(FeignAutoConfiguration.class)
4748
public class FeignAcceptGzipEncodingAutoConfiguration {
4849

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,25 +20,26 @@
2020

2121
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2423
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2524
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2625
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
2726
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Conditional;
2828
import org.springframework.context.annotation.Configuration;
2929

3030
/**
3131
* Configures the Feign request compression.
3232
*
3333
* @author Jakub Narloch
34+
* @author Olga Maciaszek-Sharma
3435
* @see FeignContentGzipEncodingInterceptor
3536
*/
3637
@Configuration(proxyBeanMethods = false)
3738
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
3839
@ConditionalOnClass(Feign.class)
3940
// The OK HTTP client uses "transparent" compression.
40-
// If the content-encoding header is present it disable transparent compression
41-
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
41+
// If the content-encoding header is present, it disables transparent compression.
42+
@Conditional(OkHttpFeignClientBeanMissingCondition.class)
4243
@ConditionalOnProperty("feign.compression.request.enabled")
4344
@AutoConfigureAfter(FeignAutoConfiguration.class)
4445
public class FeignContentGzipEncodingAutoConfiguration {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2023-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.openfeign.encoding;
18+
19+
import feign.Client;
20+
import feign.okhttp.OkHttpClient;
21+
22+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.context.annotation.Condition;
26+
27+
/**
28+
* A {@link Condition} that verifies whether the conditions for creating Feign
29+
* {@link Client} beans that either are of type {@link OkHttpClient} or have a delegate of
30+
* type {@link OkHttpClient} are not met.
31+
*
32+
* @author Olga Maciaszek-Sharma
33+
* @since 4.0.2
34+
*/
35+
public class OkHttpFeignClientBeanMissingCondition extends AnyNestedCondition {
36+
37+
public OkHttpFeignClientBeanMissingCondition() {
38+
super(ConfigurationPhase.REGISTER_BEAN);
39+
}
40+
41+
@ConditionalOnMissingClass("feign.okhttp.OkHttpClient")
42+
static class FeignOkHttpClientPresent {
43+
44+
}
45+
46+
@ConditionalOnProperty(value = "feign.okhttp.enabled", havingValue = "false")
47+
static class FeignOkHttpClientEnabled {
48+
49+
}
50+
51+
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignCompressionTests.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import feign.Client;
2222
import feign.RequestInterceptor;
2323
import feign.httpclient.ApacheHttpClient;
24+
import okhttp3.OkHttpClient;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,18 +40,19 @@
3940
/**
4041
* @author Ryan Baxter
4142
* @author Biju Kunjummen
43+
* @author Olga Maciaszek-Sharma
4244
*/
4345
class FeignCompressionTests {
4446

4547
@Test
46-
void testInterceptors() {
48+
void shouldAddCompressionInterceptors() {
4749
new ApplicationContextRunner()
4850
.withPropertyValues("feign.compression.response.enabled=true", "feign.compression.request.enabled=true",
4951
"feign.okhttp.enabled=false")
5052
.withConfiguration(AutoConfigurations.of(FeignAutoConfiguration.class,
5153
FeignContentGzipEncodingAutoConfiguration.class, FeignAcceptGzipEncodingAutoConfiguration.class,
5254
HttpClientConfiguration.class, PlainConfig.class))
53-
.run(context -> {
55+
.withUserConfiguration(OkHttpClientConfiguration.class).run(context -> {
5456
FeignContext feignContext = context.getBean(FeignContext.class);
5557
Map<String, RequestInterceptor> interceptors = feignContext.getInstances("foo",
5658
RequestInterceptor.class);
@@ -62,6 +64,22 @@ void testInterceptors() {
6264
});
6365
}
6466

67+
@Test
68+
void shouldNotAddInterceptorsIfFeignOkHttpClientPresent() {
69+
new ApplicationContextRunner()
70+
.withPropertyValues("feign.compression.response.enabled=true", "feign.compression.request.enabled=true",
71+
"feign.okhttp.enabled=true")
72+
.withConfiguration(AutoConfigurations.of(FeignAutoConfiguration.class,
73+
FeignContentGzipEncodingAutoConfiguration.class, FeignAcceptGzipEncodingAutoConfiguration.class,
74+
HttpClientConfiguration.class))
75+
.run(context -> {
76+
FeignContext feignContext = context.getBean(FeignContext.class);
77+
Map<String, RequestInterceptor> interceptors = feignContext.getInstances("foo",
78+
RequestInterceptor.class);
79+
assertThat(interceptors).isEmpty();
80+
});
81+
}
82+
6583
@Configuration(proxyBeanMethods = false)
6684
protected static class PlainConfig {
6785

@@ -85,4 +103,14 @@ public ApacheHttpClient client() {
85103

86104
}
87105

106+
@Configuration(proxyBeanMethods = false)
107+
static class OkHttpClientConfiguration {
108+
109+
@Bean
110+
OkHttpClient okHttpClient() {
111+
return new OkHttpClient();
112+
}
113+
114+
}
115+
88116
}

0 commit comments

Comments
 (0)