Skip to content

Commit

Permalink
Properly use headers set in Vert.x from RESTEasy Reactive
Browse files Browse the repository at this point in the history
Essentially this change makes sure RESTEasy Reactive does not
override the values of the headers that were set from
Vert.x Web Routes that ran before RESTEasy Reactive

Fixes: #25283
  • Loading branch information
geoand committed May 5, 2022
1 parent 6c5eb47 commit b2f24cc
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<artifactId>quarkus-jaxrs-client-reactive-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes-deployment</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.resteasy.reactive.server.test.headers;

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.ext.Provider;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.web.RouteFilter;
import io.vertx.ext.web.RoutingContext;

public class VertxHeadersTest {

@RegisterExtension
static QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(VertxFilter.class, JaxRsFilter.class, TestResource.class));

@Test
void testVaryHeaderValues() {
var headers = when().get("/test")
.then()
.statusCode(200)
.extract().headers();
assertThat(headers.getValues(HttpHeaders.VARY)).containsExactlyInAnyOrder("Origin", "Prefer");
}

public static class VertxFilter {
@RouteFilter
void addVary(final RoutingContext rc) {
rc.response().headers().add(HttpHeaders.VARY, "Origin");
rc.next();
}
}

@Provider
public static class JaxRsFilter implements ContainerResponseFilter {
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add(HttpHeaders.VARY, "Prefer");
}
}

@Path("test")
public static class TestResource {

@GET
public String test() {
return "test";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -71,7 +72,13 @@ public void accept(ResteasyReactiveRequestContext context) {
}
};

private static final String CONTENT_TYPE = "Content-Type"; // use this instead of the Vert.x constant because the TCK expects upper case
private static final String CONTENT = "Content";
private static final String CONTENT_LOWER = "content";
private static final String TYPE = "Type";
private static final String TYPE_LOWER = "type";
private static final String LENGTH = "Length";
private static final String LENGTH_LOWER = "length";
private static final String CONTENT_TYPE = CONTENT + "-" + TYPE; // use this instead of the Vert.x constant because the TCK expects upper case

static {
primitivesToWrappers.put(boolean.class, Boolean.class);
Expand Down Expand Up @@ -484,13 +491,27 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
MultivaluedMap<String, Object> headers = response.getHeaders();
for (Map.Entry<String, List<Object>> entry : headers.entrySet()) {
if (entry.getValue().size() == 1) {
String header = entry.getKey();
boolean useSet = useSet(header);
Object o = entry.getValue().get(0);
if (o == null) {
vertxResponse.setResponseHeader(entry.getKey(), "");
if (useSet) {
vertxResponse.setResponseHeader(header, "");
} else {
vertxResponse.addResponseHeader(header, "");
}
} else if (o instanceof CharSequence) {
vertxResponse.setResponseHeader(entry.getKey(), (CharSequence) o);
if (useSet) {
vertxResponse.setResponseHeader(header, (CharSequence) o);
} else {
vertxResponse.addResponseHeader(header, (CharSequence) o);
}
} else {
vertxResponse.setResponseHeader(entry.getKey(), (CharSequence) HeaderUtil.headerToString(o));
if (useSet) {
vertxResponse.setResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o));
} else {
vertxResponse.addResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o));
}
}
} else {
List<CharSequence> strValues = new ArrayList<>(entry.getValue().size());
Expand All @@ -502,4 +523,15 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
}
}

private static boolean useSet(String header) {
if (!(header.startsWith(CONTENT) || header.startsWith(CONTENT_LOWER))) {
return false;
}
if (header.length() < CONTENT.length() + 2) {
return false;
}
String substring = header.substring(CONTENT.length() + 1).toLowerCase(Locale.ROOT);
return substring.equals(TYPE_LOWER) || substring.equals(LENGTH_LOWER);
}

}

0 comments on commit b2f24cc

Please sign in to comment.