From 674a1acf069dee0b387114bc1fb7a9aaef5f4a0e Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Tue, 17 Oct 2023 14:18:29 +0200 Subject: [PATCH] Allow POSTing with a URL not ending with '/' to jax-ws endpoints (#10710) * #10699 allow POSTing with a URL not ending with '/' to jax-ws endpoints Signed-off-by: Ludovic Orban --- jetty-core/jetty-http-spi/pom.xml | 17 ++- .../jetty/http/spi/HttpSpiContextHandler.java | 3 + .../jetty/http/spi/JaxWsEndpointTest.java | 109 ++++++++++++++++++ .../eclipse/jetty/http/spi/LoggingUtil.java | 5 + 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/JaxWsEndpointTest.java diff --git a/jetty-core/jetty-http-spi/pom.xml b/jetty-core/jetty-http-spi/pom.xml index d0cd6f2c2cc..1563af4fad8 100644 --- a/jetty-core/jetty-http-spi/pom.xml +++ b/jetty-core/jetty-http-spi/pom.xml @@ -11,6 +11,8 @@ ${project.groupId}.http.spi true + 3.1.0 + 4.0.1 org.eclipse.jetty.http.spi.* @@ -24,6 +26,18 @@ slf4j-api provided + + com.sun.xml.ws + jaxws-rt + ${jakarta.xml.jaxws.impl.version} + test + + + jakarta.ws.rs + jakarta.ws.rs-api + ${jakarta.ws.rs.api.version} + test + org.eclipse.jetty jetty-client @@ -82,7 +96,8 @@ org.apache.maven.plugins maven-surefire-plugin - --add-opens java.base/jdk.internal.misc=ALL-UNNAMED + --add-opens java.base/jdk.internal.misc=ALL-UNNAMED + --add-reads org.eclipse.jetty.http.spi=org.eclipse.jetty.logging diff --git a/jetty-core/jetty-http-spi/src/main/java/org/eclipse/jetty/http/spi/HttpSpiContextHandler.java b/jetty-core/jetty-http-spi/src/main/java/org/eclipse/jetty/http/spi/HttpSpiContextHandler.java index 3d5ce4cd0f1..a4874ad52a5 100644 --- a/jetty-core/jetty-http-spi/src/main/java/org/eclipse/jetty/http/spi/HttpSpiContextHandler.java +++ b/jetty-core/jetty-http-spi/src/main/java/org/eclipse/jetty/http/spi/HttpSpiContextHandler.java @@ -45,6 +45,9 @@ public class HttpSpiContextHandler extends ContextHandler { this._httpContext = httpContext; this._httpHandler = httpHandler; + // The default jax-ws web server allows posting to URLs that do not end + // with a trailing '/'; allow it too to be a drop-in replacement. + setAllowNullPathInContext(true); super.setHandler(new Handler.Abstract() { @Override diff --git a/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/JaxWsEndpointTest.java b/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/JaxWsEndpointTest.java new file mode 100644 index 00000000000..95643f3200e --- /dev/null +++ b/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/JaxWsEndpointTest.java @@ -0,0 +1,109 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.http.spi; + +import java.net.InetSocketAddress; +import java.net.ServerSocket; + +import jakarta.jws.WebMethod; +import jakarta.jws.WebService; +import jakarta.xml.ws.Endpoint; +import org.eclipse.jetty.client.ContentResponse; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.logging.StacklessLogging; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; + +public class JaxWsEndpointTest +{ + private StacklessLogging stacklessLogging; + private String urlPrefix; + private Server server; + private HttpClient client; + + @Test + public void testPostToURLWithoutTrailingSlash() throws Exception + { + Endpoint.publish(urlPrefix + "/add", new AddService()); + + ContentResponse response = client.newRequest(urlPrefix) + .method(HttpMethod.POST) + .path("/add") + .send(); + + assertThat(response.getStatus(), is(500)); + assertThat(response.getContentAsString(), containsString("Couldn't create SOAP message due to exception")); + } + + @WebService + public static class AddService + { + @WebMethod + public int add(int a, int b) + { + return a + b; + } + } + + @BeforeEach + public void setUp() throws Exception + { + LoggingUtil.init(); + stacklessLogging = new StacklessLogging(com.sun.xml.ws.transport.http.HttpAdapter.class); + + int port; + try (ServerSocket serverSocket = new ServerSocket()) + { + serverSocket.setReuseAddress(true); + serverSocket.bind(new InetSocketAddress("localhost", 0)); + port = serverSocket.getLocalPort(); + urlPrefix = "http://localhost:" + port; + } + + server = new Server(new DelegatingThreadPool(new QueuedThreadPool())); + ServerConnector connector = new ServerConnector(server); + connector.setPort(port); + server.addConnector(connector); + server.setHandler(new ContextHandlerCollection()); + server.start(); + + JettyHttpServerProvider.setServer(server); + System.setProperty("com.sun.net.httpserver.HttpServerProvider", JettyHttpServerProvider.class.getName()); + + client = new HttpClient(); + client.start(); + } + + @AfterEach + public void tearDown() + { + LifeCycle.stop(client); + LifeCycle.stop(server); + JettyHttpServerProvider.setServer(null); + System.clearProperty("com.sun.net.httpserver.HttpServerProvider"); + stacklessLogging.close(); + LoggingUtil.end(); + } +} diff --git a/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/LoggingUtil.java b/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/LoggingUtil.java index a8dec68f56d..b30397bf2e0 100644 --- a/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/LoggingUtil.java +++ b/jetty-core/jetty-http-spi/src/test/java/org/eclipse/jetty/http/spi/LoggingUtil.java @@ -26,4 +26,9 @@ public final class LoggingUtil org.slf4j.bridge.SLF4JBridgeHandler.removeHandlersForRootLogger(); org.slf4j.bridge.SLF4JBridgeHandler.install(); } + + public static void end() + { + org.slf4j.bridge.SLF4JBridgeHandler.uninstall(); + } }