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
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user