From 8bdffe872c36d3033db8e50cf28a9eb3bf69b2c5 Mon Sep 17 00:00:00 2001 From: Domenico Francesco Bruscino Date: Fri, 17 Feb 2023 09:11:02 +0100 Subject: [PATCH] ARTEMIS-4176 Fix console custom root redirect --- .../artemis/component/DefaultHandler.java | 2 +- .../cli/test/WebServerComponentTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/artemis-web/src/main/java/org/apache/activemq/artemis/component/DefaultHandler.java b/artemis-web/src/main/java/org/apache/activemq/artemis/component/DefaultHandler.java index b068e1d78a..a53da3de76 100644 --- a/artemis-web/src/main/java/org/apache/activemq/artemis/component/DefaultHandler.java +++ b/artemis-web/src/main/java/org/apache/activemq/artemis/component/DefaultHandler.java @@ -40,7 +40,7 @@ public class DefaultHandler extends org.eclipse.jetty.server.handler.DefaultHand HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (rootRedirectLocation != null && target.matches("^$|/")) { - response.sendRedirect("/console"); + response.sendRedirect(rootRedirectLocation); } else { super.handle(target, baseRequest, request, response); } diff --git a/artemis-web/src/test/java/org/apache/activemq/cli/test/WebServerComponentTest.java b/artemis-web/src/test/java/org/apache/activemq/cli/test/WebServerComponentTest.java index 46556d7a39..d818790eba 100644 --- a/artemis-web/src/test/java/org/apache/activemq/cli/test/WebServerComponentTest.java +++ b/artemis-web/src/test/java/org/apache/activemq/cli/test/WebServerComponentTest.java @@ -23,8 +23,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -454,6 +456,45 @@ public class WebServerComponentTest extends Assert { Assert.assertFalse(webServerComponent.isStarted()); } + @Test + public void testDefaultRootRedirect() throws Exception { + testRootRedirect(null, 404, null); + } + + @Test + public void testCustomRootRedirect() throws Exception { + testRootRedirect("test-root-redirect", 302, "test-root-redirect"); + } + + public void testRootRedirect(String rootRedirectLocation, int expectedResponseCode, String expectedResponseLocation) throws Exception { + BindingDTO bindingDTO = new BindingDTO(); + bindingDTO.uri = "http://localhost:0"; + WebServerDTO webServerDTO = new WebServerDTO(); + webServerDTO.setBindings(Collections.singletonList(bindingDTO)); + webServerDTO.path = ""; + webServerDTO.rootRedirectLocation = rootRedirectLocation; + + WebServerComponent webServerComponent = new WebServerComponent(); + webServerComponent.configure(webServerDTO, null, null); + webServerComponent.start(); + try { + int port = webServerComponent.getPort(0); + java.net.URL url = new URL("http://localhost:" + port); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setInstanceFollowRedirects(false); + try { + assertEquals(expectedResponseCode, conn.getResponseCode()); + if (expectedResponseLocation != null) { + assertTrue(conn.getHeaderField("Location").endsWith(webServerDTO.rootRedirectLocation)); + } + } finally { + conn.disconnect(); + } + } finally { + webServerComponent.stop(true); + } + } + private void createTestWar(String warName) throws Exception { File warFile = new File("target", warName); File srcFile = new File("src/test/webapp");