From ac731a2c78775dce005c7ee72ff839950435490e Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 10 Oct 2022 10:23:25 +0300 Subject: [PATCH] Ensure that JAX-RS Cookie parameter type is usable in Resource methods Fixes: #28453 --- .../server/core/parameters/CookieParamExtractor.java | 11 ++++++++++- .../core/startup/RuntimeResourceDeployment.java | 2 +- .../vertx/test/simple/NewParamsRestResource.java | 11 +++++++---- .../vertx/test/simple/SimpleQuarkusRestTestCase.java | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/CookieParamExtractor.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/CookieParamExtractor.java index 19b7c273cb92a..f2daaadc2bcb9 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/CookieParamExtractor.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/CookieParamExtractor.java @@ -1,17 +1,26 @@ package org.jboss.resteasy.reactive.server.core.parameters; +import javax.ws.rs.core.Cookie; + import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; public class CookieParamExtractor implements ParameterExtractor { private final String name; + private final String parameterTypeName; - public CookieParamExtractor(String name) { + public CookieParamExtractor(String name, String parameterTypeName) { this.name = name; + this.parameterTypeName = parameterTypeName; } @Override public Object extractParameter(ResteasyReactiveRequestContext context) { + if (Cookie.class.getName().equals(parameterTypeName)) { + // we need to make sure we preserve the name because otherwise CookieHeaderDelegate will not be able to convert back to Cookie + Cookie cookie = context.getHttpHeaders().getCookies().get(name); + return cookie != null ? cookie.toString() : null; + } return context.getCookieParameter(name); } } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java index 6b71932e32a42..67118258789de 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java @@ -626,7 +626,7 @@ public ParameterExtractor parameterExtractor(Map pathParameterI case HEADER: return new HeaderParamExtractor(name, single); case COOKIE: - return new CookieParamExtractor(name); + return new CookieParamExtractor(name, javaType); case FORM: return new FormParamExtractor(name, single, encoded); case PATH: diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/NewParamsRestResource.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/NewParamsRestResource.java index d30b7d363e1f8..1756c42ad5e38 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/NewParamsRestResource.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/NewParamsRestResource.java @@ -9,6 +9,7 @@ import javax.ws.rs.container.ResourceContext; import javax.ws.rs.container.ResourceInfo; import javax.ws.rs.core.Configuration; +import javax.ws.rs.core.Cookie; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Request; @@ -53,12 +54,14 @@ public String params(@RestPath String p, @RestHeader("") String paramEmpty, @RestForm String f, @RestMatrix String m, - @RestCookie String c) { + @RestCookie String c, + @RestCookie("c") Cookie cookie) { return "params: p: " + p + ", q: " + q + ", h: " + h + ", xMyHeader: " + xMyHeader + ", testHeaderParam: " + testHeaderParam + ", paramEmpty: " - + paramEmpty + ", f: " + f + ", m: " + m + ", c: " - + c + ", q2: " - + q2.orElse("empty") + ", q3: " + q3.orElse(-1); + + paramEmpty + ", f: " + f + ", m: " + m + + ", c: " + c + + ", c2: " + cookie.getValue() + + ", q2: " + q2.orElse("empty") + ", q3: " + q3.orElse(-1); } @Blocking diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java index 6c008d01aa74e..1324e5ca35a5a 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java @@ -433,7 +433,7 @@ public void testNewParams() { .log().ifError() .body(Matchers .equalTo( - "params: p: pv, q: qv, h: 123, xMyHeader: test, testHeaderParam: test, paramEmpty: empty, f: fv, m: mv, c: cv, q2: empty, q3: 999")); + "params: p: pv, q: qv, h: 123, xMyHeader: test, testHeaderParam: test, paramEmpty: empty, f: fv, m: mv, c: cv, c2: cv, q2: empty, q3: 999")); RestAssured.get("/new-params/myklass/myregex/sse") .then() .log().ifError()