From 362b2a5fd3390822d6656d9e1a9ed73b2b5957af Mon Sep 17 00:00:00 2001 From: AndreTeigler <75010275+AndreTeigler@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:23:39 +0200 Subject: [PATCH 1/5] Make Content-Encoding configurable (#1071) * Make Content-Encoding configurable A new configuration property was added to make the Content-Encoding configurable when using the feign request compression. The default values are the same as the previous hardcoded values. Closes issue 1048 --- .../FeignClientEncodingProperties.java | 22 ++- .../FeignContentGzipEncodingInterceptor.java | 13 +- ...gnContentGzipEncodingInterceptorTests.java | 159 ++++++++++++++++++ 3 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptorTests.java diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignClientEncodingProperties.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignClientEncodingProperties.java index d1669008..865a0294 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignClientEncodingProperties.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignClientEncodingProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2024 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. @@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * The Feign encoding properties. * * @author Jakub Narloch + * @author André Teigler */ @ConfigurationProperties("spring.cloud.openfeign.compression.request") public class FeignClientEncodingProperties { @@ -39,6 +40,11 @@ public class FeignClientEncodingProperties { */ private int minRequestSize = 2048; + /** + * The list of content encodings (applicable encodings depend on the used client). + */ + private String[] contentEncodingTypes = new String[] { HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING }; + public String[] getMimeTypes() { return mimeTypes; } @@ -55,6 +61,14 @@ public class FeignClientEncodingProperties { this.minRequestSize = minRequestSize; } + public String[] getContentEncodingTypes() { + return contentEncodingTypes; + } + + public void setContentEncodingTypes(String[] contentEncodingTypes) { + this.contentEncodingTypes = contentEncodingTypes; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -64,7 +78,8 @@ public class FeignClientEncodingProperties { return false; } FeignClientEncodingProperties that = (FeignClientEncodingProperties) o; - return Arrays.equals(mimeTypes, that.mimeTypes) && Objects.equals(minRequestSize, that.minRequestSize); + return Arrays.equals(mimeTypes, that.mimeTypes) && Objects.equals(minRequestSize, that.minRequestSize) + && Arrays.equals(contentEncodingTypes, that.contentEncodingTypes); } @Override @@ -79,6 +94,9 @@ public class FeignClientEncodingProperties { .append(", ") .append("minRequestSize=") .append(minRequestSize) + .append(", ") + .append("contentEncodingTypes=") + .append(Arrays.toString(contentEncodingTypes)) .append("}") .toString(); } diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java index e8cbe19c..3575023a 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java @@ -44,11 +44,20 @@ public class FeignContentGzipEncodingInterceptor extends BaseRequestInterceptor public void apply(RequestTemplate template) { if (requiresCompression(template)) { - addHeader(template, HttpEncoding.CONTENT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING, - HttpEncoding.DEFLATE_ENCODING); + addHeader(template, HttpEncoding.CONTENT_ENCODING_HEADER, getContentEncodings()); } } + private String[] getContentEncodings() { + if (getProperties().getContentEncodingTypes() == null + || getProperties().getContentEncodingTypes().length == 0) { + + throw new IllegalStateException("Invalid ContentEncodingTypes configuration"); + } + + return getProperties().getContentEncodingTypes(); + } + /** * Returns whether the request requires GZIP compression. * @param template the request template diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptorTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptorTests.java new file mode 100644 index 00000000..c4ea2df3 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptorTests.java @@ -0,0 +1,159 @@ +/* + * Copyright 2013-2024 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 + * + * https://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.cloud.openfeign.encoding; + +import java.util.List; + +import feign.RequestTemplate; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Unit tests for {@link FeignContentGzipEncodingInterceptor}. + * + * @author André Teigler + */ +class FeignContentGzipEncodingInterceptorTests { + + @Test + void shouldNotAddCompressionHeaderWhenSizeBelowThreshold() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2047"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); + } + + @Test + void shouldNotAddCompressionHeaderWhenSizeEqualsThreshold() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2048"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); + } + + @Test + void shouldAddCompressionHeaderWhenSizeAboveThreshold() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); + } + + @Test + void shouldNotAddCompressionHeaderWhenTypeMismatch() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setMimeTypes(new String[] { "application/xml" }); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).doesNotContainKey(HttpEncoding.CONTENT_ENCODING_HEADER); + } + + @Test + void shouldAddDefaultCompressionHeaderWhenMimeTypeNotSet() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setMimeTypes(new String[] {}); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); + } + + @Test + void shouldAddDefaultCompressionHeaderWhenNullMimeTypeSet() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setMimeTypes(null); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip", "deflate")); + } + + @Test + void shouldAddCustomCompressionHeader() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setContentEncodingTypes(new String[] { "gzip" }); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + interceptor.apply(template); + + assertThat(template.headers()).containsKey(HttpEncoding.CONTENT_ENCODING_HEADER); + assertThat(template.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)).isEqualTo(List.of("gzip")); + } + + @Test + void shouldThrowExceptionWhenContentEncodingTypesAreEmpty() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setContentEncodingTypes(new String[] {}); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + assertThatThrownBy(() -> interceptor.apply(template)).isInstanceOf(IllegalStateException.class) + .hasMessage("Invalid ContentEncodingTypes configuration"); + } + + @Test + void shouldThrowExceptionWhenNullContentEncodingTypes() { + final FeignClientEncodingProperties properties = new FeignClientEncodingProperties(); + properties.setContentEncodingTypes(null); + final RequestTemplate template = new RequestTemplate(); + template.header(HttpEncoding.CONTENT_LENGTH, "2049"); + template.header(HttpEncoding.CONTENT_TYPE, "application/json"); + final FeignContentGzipEncodingInterceptor interceptor = new FeignContentGzipEncodingInterceptor(properties); + + assertThatThrownBy(() -> interceptor.apply(template)).isInstanceOf(IllegalStateException.class) + .hasMessage("Invalid ContentEncodingTypes configuration"); + } + +} From 7901dedfa60b2a65bb6316fb5f9bcda13fed395e Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Wed, 25 Sep 2024 13:42:52 +0200 Subject: [PATCH 2/5] Regenerate configprops. --- docs/modules/ROOT/partials/_configprops.adoc | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/modules/ROOT/partials/_configprops.adoc b/docs/modules/ROOT/partials/_configprops.adoc index 2819a12c..81cc1df1 100644 --- a/docs/modules/ROOT/partials/_configprops.adoc +++ b/docs/modules/ROOT/partials/_configprops.adoc @@ -73,6 +73,7 @@ |spring.cloud.openfeign.client.default-config | `+++default+++` | |spring.cloud.openfeign.client.default-to-properties | `+++true+++` | |spring.cloud.openfeign.client.refresh-enabled | `+++false+++` | Enables options value refresh capability for Feign. +|spring.cloud.openfeign.compression.request.content-encoding-types | | The list of content encodings (applicable encodings depend on the used client). |spring.cloud.openfeign.compression.request.enabled | `+++false+++` | Enables the request sent by Feign to be compressed. |spring.cloud.openfeign.compression.request.mime-types | `+++[text/xml, application/xml, application/json]+++` | The list of supported mime types. |spring.cloud.openfeign.compression.request.min-request-size | `+++2048+++` | The minimum threshold content size. From 6024de0abaa54d81a1816de045c08b9b37ffaba8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:44:03 +0200 Subject: [PATCH 3/5] Bump io.github.openfeign:feign-bom from 13.3 to 13.4 (#1085) Bumps [io.github.openfeign:feign-bom](https://github.com/openfeign/feign) from 13.3 to 13.4. - [Release notes](https://github.com/openfeign/feign/releases) - [Changelog](https://github.com/OpenFeign/feign/blob/master/CHANGELOG.md) - [Commits](https://github.com/openfeign/feign/compare/13.3...13.4) --- updated-dependencies: - dependency-name: io.github.openfeign:feign-bom dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- spring-cloud-openfeign-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-openfeign-dependencies/pom.xml b/spring-cloud-openfeign-dependencies/pom.xml index e40bb1cc..c1cb9e99 100644 --- a/spring-cloud-openfeign-dependencies/pom.xml +++ b/spring-cloud-openfeign-dependencies/pom.xml @@ -15,7 +15,7 @@ spring-cloud-openfeign-dependencies Spring Cloud OpenFeign Dependencies - 13.3 + 13.4 3.8.0 From 76efdca6b82b3986c517a00203cffe2de008af68 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:01:31 +0200 Subject: [PATCH 4/5] Bump com.google.protobuf:protobuf-java from 3.25.4 to 3.25.5 (#1089) Bumps [com.google.protobuf:protobuf-java](https://github.com/protocolbuffers/protobuf) from 3.25.4 to 3.25.5. - [Release notes](https://github.com/protocolbuffers/protobuf/releases) - [Changelog](https://github.com/protocolbuffers/protobuf/blob/main/protobuf_release.bzl) - [Commits](https://github.com/protocolbuffers/protobuf/compare/v3.25.4...v3.25.5) --- updated-dependencies: - dependency-name: com.google.protobuf:protobuf-java dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- spring-cloud-openfeign-core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-openfeign-core/pom.xml b/spring-cloud-openfeign-core/pom.xml index 44ad53be..c6ecc934 100644 --- a/spring-cloud-openfeign-core/pom.xml +++ b/spring-cloud-openfeign-core/pom.xml @@ -183,7 +183,7 @@ com.google.protobuf protobuf-java - 3.25.4 + 3.25.5 test From f832b1060de5a86f61eb0913f5a7332393d799ab Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:02:48 +0200 Subject: [PATCH 5/5] Bump @springio/asciidoctor-extensions in /docs (#1093) Bumps [@springio/asciidoctor-extensions](https://github.com/spring-io/asciidoctor-extensions) from 1.0.0-alpha.13 to 1.0.0-alpha.14. - [Changelog](https://github.com/spring-io/asciidoctor-extensions/blob/main/CHANGELOG.adoc) - [Commits](https://github.com/spring-io/asciidoctor-extensions/compare/v1.0.0-alpha.13...v1.0.0-alpha.14) --- updated-dependencies: - dependency-name: "@springio/asciidoctor-extensions" dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- docs/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/package.json b/docs/package.json index 6a68ce60..07706522 100644 --- a/docs/package.json +++ b/docs/package.json @@ -5,6 +5,6 @@ "@antora/collector-extension": "1.0.0-beta.2", "@asciidoctor/tabs": "1.0.0-beta.6", "@springio/antora-extensions": "1.14.2", - "@springio/asciidoctor-extensions": "1.0.0-alpha.13" + "@springio/asciidoctor-extensions": "1.0.0-alpha.14" } }