diff --git a/jetty-core/jetty-server/pom.xml b/jetty-core/jetty-server/pom.xml index a6e11b81ae4f..794f57ee25f0 100644 --- a/jetty-core/jetty-server/pom.xml +++ b/jetty-core/jetty-server/pom.xml @@ -49,6 +49,11 @@ jetty-slf4j-impl test + + org.eclipse.jetty + jetty-slf4j-impl + test + org.eclipse.jetty jetty-util-ajax diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DebugHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DebugHandler.java index 43b2c6a3d9a0..bbbcb866f34b 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DebugHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DebugHandler.java @@ -147,7 +147,7 @@ public void setShowHeaders(boolean showHeaders) public boolean handle(Request request, Response response, Callback callback) throws Exception { Thread thread = Thread.currentThread(); - String name = thread.getName() + ":" + request.getHttpURI(); + String name = thread.getName() + ":" + request.getHttpURI().getPathQuery(); boolean willHandle = false; Throwable ex = null; String rname = findRequestName(request); @@ -155,6 +155,7 @@ public boolean handle(Request request, Response response, Callback callback) thr try { + thread.setName(name); String headers = _showHeaders ? ("\n" + request.getHeaders().toString()) : ""; log(">> r=%s %s %s %s %s %s", @@ -164,7 +165,6 @@ public boolean handle(Request request, Response response, Callback callback) thr request.getConnectionMetaData().getProtocol(), request.getConnectionMetaData(), headers); - thread.setName(name); willHandle = getHandler().handle(request, response, handlingCallback); return willHandle; @@ -195,10 +195,11 @@ protected void log(String format, Object... arg) String s = String.format(format, arg); + String threadName = Thread.currentThread().getName(); long now = System.currentTimeMillis(); long ms = now % 1000; if (_print != null) - _print.printf("%s.%03d:%s%n", __date.format(now), ms, s); + _print.printf("%s.%03d:%s:%s%n", __date.format(now), ms, threadName, s); } protected String findRequestName(Request request) @@ -218,7 +219,6 @@ protected String findRequestName(Request request) } catch (IllegalStateException e) { - // TODO can we avoid creating and catching this exception? see #8024 // Handle the case when the request has already been completed return String.format("%s@%x", request.getHttpURI(), request.hashCode()); } @@ -234,7 +234,7 @@ protected void doStart() throws Exception for (Connector connector : getServer().getConnectors()) { if (connector instanceof AbstractConnector) - ((AbstractConnector)connector).addBean(this, false); + connector.addBean(this, false); } super.doStart(); @@ -248,7 +248,7 @@ protected void doStop() throws Exception for (Connector connector : getServer().getConnectors()) { if (connector instanceof AbstractConnector) - ((AbstractConnector)connector).removeBean(this); + connector.removeBean(this); } } diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java index cacb7ac44f33..0e544ddf4c06 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java @@ -19,6 +19,7 @@ import java.net.HttpURLConnection; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.security.KeyStore; import java.util.concurrent.TimeUnit; @@ -28,18 +29,22 @@ import javax.net.ssl.TrustManagerFactory; import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.IOResources; import org.eclipse.jetty.server.AbstractConnectionFactory; +import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.MavenPaths; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static org.awaitility.Awaitility.await; import static org.hamcrest.MatcherAssert.assertThat; @@ -47,9 +52,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -@Disabled // TODO public class DebugHandlerTest { + private static final Logger LOG = LoggerFactory.getLogger(DebugHandlerTest.class); public static final HostnameVerifier __hostnameverifier = (hostname, session) -> true; private SSLContext sslContext; @@ -72,7 +77,7 @@ public void startServer() throws Exception httpConnector.setPort(0); server.addConnector(httpConnector); - Path keystorePath = MavenTestingUtils.getTestResourcePath("keystore.p12"); + Path keystorePath = MavenPaths.findTestResourceFile("keystore.p12"); SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); sslContextFactory.setKeyStorePath(keystorePath.toAbsolutePath().toString()); sslContextFactory.setKeyStorePassword("storepwd"); @@ -85,19 +90,20 @@ public void startServer() throws Exception debugHandler = new DebugHandler(); capturedLog = new ByteArrayOutputStream(); debugHandler.setOutputStream(capturedLog); - /* TODO - debugHandler.setHandler(new AbstractHandler() + debugHandler.setHandler(new Handler.Abstract() { + @Override - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + public boolean handle(Request request, Response response, Callback callback) throws Exception { - baseRequest.setHandled(true); - response.setStatus(HttpStatus.OK_200); + LOG.info("Abstract handle()"); + response.setStatus(200); + callback.succeeded(); + return true; } }); server.setHandler(debugHandler); - */ server.start(); String host = httpConnector.getHost(); @@ -106,9 +112,15 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques serverURI = URI.create(String.format("http://%s:%d/", host, httpConnector.getLocalPort())); secureServerURI = URI.create(String.format("https://%s:%d/", host, sslConnector.getLocalPort())); + } + + @BeforeEach + public void trustAllHttpsUrlConnection() throws Exception + { + Path keystorePath = MavenPaths.findTestResourceFile("keystore.p12"); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); - try (InputStream stream = IOResources.asInputStream(sslContextFactory.getKeyStoreResource())) + try (InputStream stream = Files.newInputStream(keystorePath)) { keystore.load(stream, "storepwd".toCharArray()); } @@ -151,7 +163,7 @@ public void testThreadName() throws IOException HttpURLConnection http = (HttpURLConnection)serverURI.resolve("/foo/bar?a=b").toURL().openConnection(); assertThat("Response Code", http.getResponseCode(), is(200)); - String log = capturedLog.toString(StandardCharsets.UTF_8.name()); + String log = capturedLog.toString(StandardCharsets.UTF_8); String expectedThreadName = ":/foo/bar?a=b"; assertThat("ThreadName", log, containsString(expectedThreadName)); // Look for bad/mangled/duplicated schemes @@ -165,7 +177,7 @@ public void testSecureThreadName() throws IOException HttpURLConnection http = (HttpURLConnection)secureServerURI.resolve("/foo/bar?a=b").toURL().openConnection(); assertThat("Response Code", http.getResponseCode(), is(200)); - String log = capturedLog.toString(StandardCharsets.UTF_8.name()); + String log = capturedLog.toString(StandardCharsets.UTF_8); String expectedThreadName = ":/foo/bar?a=b"; assertThat("ThreadName", log, containsString(expectedThreadName)); // Look for bad/mangled/duplicated schemes