From a2032701eff4aaec9292465b007116583e747d3d Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 7 Nov 2018 09:23:56 +0100 Subject: [PATCH] Issue #2996 Preserve default-context-path for quickstart (#3063) Signed-off-by: Jan Bartel --- .../QuickStartGeneratorConfiguration.java | 3 ++ .../jetty/quickstart/TestQuickStart.java | 45 ++++++++++++++++++- jetty-quickstart/src/test/resources/web.xml | 16 +++++++ .../webapp/StandardDescriptorProcessor.java | 4 +- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 jetty-quickstart/src/test/resources/web.xml diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartGeneratorConfiguration.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartGeneratorConfiguration.java index 2fa474ee967..2b83f999782 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartGeneratorConfiguration.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartGeneratorConfiguration.java @@ -203,6 +203,9 @@ public class QuickStartGeneratorConfiguration extends AbstractConfiguration //the META-INF/resources discovered addContextParamFromAttribute(context,out,MetaInfConfiguration.METAINF_RESOURCES,normalizer); + // the default-context-path, if presernt + String defaultContextPath = (String) context.getAttribute("default-context-path"); + if (defaultContextPath != null) out.tag("default-context-path", defaultContextPath); //add the name of the origin attribute, if it is being used if (_generateOrigin) diff --git a/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java b/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java index f71f7c33f7f..340e54fb285 100644 --- a/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java +++ b/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java @@ -22,6 +22,8 @@ package org.eclipse.jetty.quickstart; import static org.junit.jupiter.api.Assertions.*; import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ListenerHolder; @@ -53,7 +55,6 @@ public class TestQuickStart @Test public void testProgrammaticOverrideOfDefaultServletMapping() throws Exception { - File quickstartXml = new File(webInf, "quickstart-web.xml"); assertFalse(quickstartXml.exists()); @@ -81,7 +82,7 @@ public class TestQuickStart QuickStartWebApp webapp = new QuickStartWebApp(); webapp.setResourceBase(testDir.getAbsolutePath()); webapp.setMode(QuickStartConfiguration.Mode.QUICKSTART); - webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); //only necessary for junit testing + webapp.setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader())); server.setHandler(webapp); server.start(); @@ -90,7 +91,47 @@ public class TestQuickStart ServletHolder sh = webapp.getServletHandler().getMappedServlet("/").getResource(); assertNotNull(sh); assertEquals("foo", sh.getName()); + server.stop(); } + @Test + public void testDefaultContextPath() throws Exception + { + File quickstartXml = new File(webInf, "quickstart-web.xml"); + assertFalse(quickstartXml.exists()); + + Server server = new Server(); + + // generate a quickstart-web.xml + QuickStartWebApp quickstart = new QuickStartWebApp(); + quickstart.setResourceBase(testDir.getAbsolutePath()); + quickstart.setMode(QuickStartConfiguration.Mode.GENERATE); + quickstart.setGenerateOrigin(true); + quickstart.setDescriptor(MavenTestingUtils.getTestResourceFile("web.xml").getAbsolutePath()); + quickstart.setContextPath("/foo"); + server.setHandler(quickstart); + server.start(); + + assertEquals("/foo", quickstart.getContextPath()); + assertFalse(quickstart.isContextPathDefault()); + server.stop(); + + assertTrue(quickstartXml.exists()); + + // quick start + QuickStartWebApp webapp = new QuickStartWebApp(); + webapp.setResourceBase(testDir.getAbsolutePath()); + webapp.setMode(QuickStartConfiguration.Mode.QUICKSTART); + webapp.setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader())); + server.setHandler(webapp); + + server.start(); + + // verify the context path is the default-context-path + assertEquals("/thisIsTheDefault", webapp.getContextPath()); + assertTrue(webapp.isContextPathDefault()); + + server.stop(); + } } diff --git a/jetty-quickstart/src/test/resources/web.xml b/jetty-quickstart/src/test/resources/web.xml new file mode 100644 index 00000000000..68b02eec221 --- /dev/null +++ b/jetty-quickstart/src/test/resources/web.xml @@ -0,0 +1,16 @@ + + + + Test WebApp + + + /thisIsTheDefault + + + + diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java index 847d988901a..710b0212218 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java @@ -1967,9 +1967,11 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor { if (!(descriptor instanceof FragmentDescriptor)) { + String path = node.toString(false, true); + context.setAttribute("default-context-path", path); if (context.isContextPathDefault()) { - context.setContextPath(node.toString(false, true)); + context.setDefaultContextPath(path); context.getMetaData().setOrigin("default-context-path", descriptor); } }