From cb86b052de84f41236e31f9e1a1754b9511c8f5a Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Wed, 4 Dec 2024 16:33:55 -0600 Subject: [PATCH] Fixes #4316 - Fix incorrect coordinates for Jakarta REST API for Piranha Core Profile (#4317) --- dist/coreprofile/pom.xml | 14 +++++ .../ContainerRequestContextBean.java | 54 ++++++++++++++++++ .../ContainerRequestContextFilter.java | 53 ++++++++++++++++++ .../coreprofile/distribution/AsyncIT.java | 20 ------- .../ContainerRequestContextIT.java | 56 +++++++++++++++++++ 5 files changed, 177 insertions(+), 20 deletions(-) create mode 100644 test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextBean.java create mode 100644 test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextFilter.java create mode 100644 test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextIT.java diff --git a/dist/coreprofile/pom.xml b/dist/coreprofile/pom.xml index 12c54219f..a44d3cff4 100644 --- a/dist/coreprofile/pom.xml +++ b/dist/coreprofile/pom.xml @@ -34,6 +34,20 @@ ${project.version} compile + + + + jakarta.ws.rs + jakarta.ws.rs-api + 4.0.0 + runtime + diff --git a/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextBean.java b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextBean.java new file mode 100644 index 000000000..599da752d --- /dev/null +++ b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextBean.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN; + +/** + * The bean to test the ContainerRequestContext integration works. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +@Path("containerRequestContext") +public class ContainerRequestContextBean { + + /** + * Test to validate if the "accept" header with value "text/plain" is found. + * + * @return "This should not show up!" + */ + @GET + @Path("containsHeaderString") + @Produces(TEXT_PLAIN) + public String property1() { + return System.getProperty("containsHeaderString"); + } +} diff --git a/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextFilter.java b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextFilter.java new file mode 100644 index 000000000..5f213b509 --- /dev/null +++ b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextFilter.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import jakarta.annotation.Priority; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.container.PreMatching; +import jakarta.ws.rs.ext.Provider; +import java.io.IOException; + +/** + * The ContainerRequestFilter used to validate we can access the + * ContainerRequestContext and see the given header string. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +@Provider +@Priority(100) +@PreMatching +public class ContainerRequestContextFilter implements ContainerRequestFilter { + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + boolean equals = requestContext.containsHeaderString("accept", "text/plain"::equals); + System.setProperty("containsHeaderString", Boolean.toString(equals)); + } +} diff --git a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/AsyncIT.java b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/AsyncIT.java index 6e4edf76e..40aeb7c83 100644 --- a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/AsyncIT.java +++ b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/AsyncIT.java @@ -70,24 +70,4 @@ void testAsyncNotAcceptable() { fail(ex); } } - - /** - * Test that validates that a sync invocation to an endpoint with the - * wrong accept header returns the NOT_ACCEPTABlE status code. - */ - @Test - void testAsyncNotAcceptableSync() throws Exception { - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(baseUrl - + "/async/notAcceptable")) - .header("Accept", "text/xml") - .method("TRACE", BodyPublishers.noBody()) - .build(); - - HttpResponse response = client.send(request, - HttpResponse.BodyHandlers.ofString()); - - assertEquals(406, response.statusCode()); - } } diff --git a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextIT.java b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextIT.java new file mode 100644 index 000000000..94668a5f7 --- /dev/null +++ b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/ContainerRequestContextIT.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import static cloud.piranha.test.coreprofile.distribution.ITBase.baseUrl; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +/** + * The integration tests validating ContainerRequestContext is available. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class ContainerRequestContextIT extends ITBase { + + @Test + public void testContainsHeaderString() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(URI.create(baseUrl + + "/containerRequestContext/containsHeaderString")) + .header("Accept", "text/plain") + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assertTrue(response.body().contains("true")); + } +}