Skip to content

Commit

Permalink
feat: configuration switch for encoding filter
Browse files Browse the repository at this point in the history
  • Loading branch information
llaszkie committed Aug 25, 2023
1 parent c9805cc commit 47debe9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
7 changes: 7 additions & 0 deletions extensions/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ Quarkus configuration value `quarkus.default-locale`. Exception is in the packa
In the package `org.tkit.quarkus.rs.mappers` is the `ExceptionMapper` for all `Exception` in the project with priority `DefaultExceptionMapper.PRIORITY`.
You can `extend` and overwrite this `ExceptionMapper`. Use the `@Priority` to overwrite existing registration.

### Interceptor

Reactive version of RX-JS was equipped in Quarkus in a nice feature to clean up an up-stream response if it contained incorrect
combination of [Transfer-Encoding and Content-Length](https://github.com/quarkusio/quarkus/issues/29059) headers.
To mimic that behavior for a non-reactive stack you, can apply a provided `TransferEncodingFilter`. It cannot detect
improper combination, but instead it is always removing the problematic header. The behavior can be blocked by setting:
`tkit.rs.filter.transfer-encoding` to `false`
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package org.tkit.quarkus.rs.interceptor;

import java.util.Optional;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.ext.Provider;

import org.eclipse.microprofile.config.ConfigProvider;

@Provider
public class TransferEncodingFilter implements ClientResponseFilter {

public static final String TRANSFER_ENCODING = "Transfer-Encoding";

@Override
public void filter(ClientRequestContext clientRequestContext, ClientResponseContext clientResponseContext) {
String teHeaderValue = clientResponseContext.getHeaderString(TRANSFER_ENCODING);
if (teHeaderValue != null && teHeaderValue.contains("chunked")) {
clientResponseContext.getHeaders().remove(TRANSFER_ENCODING);
Optional<Boolean> activateFilter = ConfigProvider.getConfig().getOptionalValue("tkit.rs.filter.transfer-encoding",
Boolean.class);
if (activateFilter.isEmpty() || activateFilter.get()) {
String teHeaderValue = clientResponseContext.getHeaderString(TRANSFER_ENCODING);
if (teHeaderValue != null && teHeaderValue.contains("chunked")) {
clientResponseContext.getHeaders().remove(TRANSFER_ENCODING);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package org.tkit.quarkus.rs.interceptor;

import static org.junit.jupiter.api.Assertions.*;

import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class TransferEncodingFilterTest {

Expand All @@ -32,7 +32,7 @@ void filterChunked() {
Mockito.when(clientResponseContextMock.getHeaderString("Transfer-Encoding")).thenReturn("chunked");

MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
headers.add("Transfer-Encoding","chunked");
headers.add("Transfer-Encoding", "chunked");
Mockito.when(clientResponseContextMock.getHeaders()).thenReturn(headers);

// when
Expand All @@ -48,7 +48,7 @@ void filterOther() {
Mockito.when(clientResponseContextMock.getHeaderString("Transfer-Encoding")).thenReturn("compress");

MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
headers.add("Transfer-Encoding","compress");
headers.add("Transfer-Encoding", "compress");
Mockito.when(clientResponseContextMock.getHeaders()).thenReturn(headers);

// when
Expand All @@ -71,4 +71,4 @@ void filterNoHeader() {
Mockito.verify(clientResponseContextMock, Mockito.never()).getHeaders();
}

}
}

0 comments on commit 47debe9

Please sign in to comment.