You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2026/09/23 11:47:38 UTC

(juneau) branch master updated: Have `<@console>` emit context-root-absolute `/juneau-console/chrome.css` and theme CSS via `context:` instead of `servlet:`, so pages under `/rest/...` no longer 404 those assets. HTTP-check those URLs in `ConsoleDirective_ChromeAssetUrls_Test` and `console-chrome-pagetabs.ftlh`.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new defbd4369a Have `<@console>` emit context-root-absolute `/juneau-console/chrome.css` and theme CSS via `context:` instead of `servlet:`, so pages under `/rest/...` no longer 404 those assets. HTTP-check those URLs in `ConsoleDirective_ChromeAssetUrls_Test` and `console-chrome-pagetabs.ftlh`.
defbd4369a is described below

commit defbd4369a6b7cecff43a1dc3aed23459ff109d9
Author: James Bognar <ja...@apache.org>
AuthorDate: Wed Sep 23 07:47:31 2026 -0400

    Have `<@console>` emit context-root-absolute `/juneau-console/chrome.css` and theme CSS via `context:` instead of `servlet:`, so pages under `/rest/...` no longer 404 those assets. HTTP-check those URLs in `ConsoleDirective_ChromeAssetUrls_Test` and `console-chrome-pagetabs.ftlh`.
---
 .../ConsoleDirective_ChromeAssetUrls_Test.java     | 145 +++++++++++++++++++++
 .../freemarker/console/ConsoleDirective_Test.java  |  22 +++-
 .../templates/admin/console-chrome-pagetabs.ftlh   |  23 ++++
 .../rest/server/console/ConsoleChromeMixin.java    |  62 +++++----
 4 files changed, 224 insertions(+), 28 deletions(-)

diff --git a/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_ChromeAssetUrls_Test.java b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_ChromeAssetUrls_Test.java
new file mode 100644
index 0000000000..2b2fc44b6d
--- /dev/null
+++ b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_ChromeAssetUrls_Test.java
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juneau.rest.server.view.freemarker.console;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.net.*;
+import java.util.*;
+import java.util.regex.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.commons.inject.*;
+import org.apache.juneau.rest.mock.classic.*;
+import org.apache.juneau.rest.server.*;
+import org.apache.juneau.rest.server.console.*;
+import org.apache.juneau.rest.server.servlet.*;
+import org.apache.juneau.rest.server.view.*;
+import org.apache.juneau.rest.server.view.freemarker.*;
+import org.junit.jupiter.api.*;
+
+/**
+ * Nested-path console chrome CSS. A relative or page-prefixed {@code juneau-console/chrome.css}
+ * href on a page at {@code /rest/setup} 404s (the browser asks for
+ * {@code /rest/setup/juneau-console/...}) and the page paints with no chrome. Root-absolute
+ * {@code /juneau-console/chrome.css} is 200 and the chrome paints.
+ *
+ * <p>
+ * This is an HTTP render of a real {@code <@console>} page, not a string-contains check on the
+ * href (that substring also matches the buggy {@code /rest/setup/juneau-console/chrome.css}
+ * form). Juneau has no Playwright e2e suite; this is the closest existing harness
+ * ({@link MockRestClient} against the console-ui-freemarker module).
+ *
+ * @since 10.0.0
+ */
+@SuppressWarnings({
+	"resource" // MockRestClient/RestResponse are closed in try-with-resources; fluent assertStatus returns this.
+})
+class ConsoleDirective_ChromeAssetUrls_Test extends TestBase {
+
+	private static final Pattern STYLESHEET_HREF = Pattern.compile(
+		"<link\\s[^>]*rel=\"stylesheet\"[^>]*href=\"([^\"]+)\"",
+		Pattern.CASE_INSENSITIVE
+	);
+	private static final Pattern THEME_PATH = Pattern.compile(
+		"^/juneau-console/themes/juneau-theme-[a-z0-9-]+\\.css$"
+	);
+
+	@Rest(mixins={ FreemarkerMixin.class, ConsoleChromeMixin.class }, renderResponseStackTraces="true")
+	public static class NestedPageHost extends BasicRestServlet {
+		private static final long serialVersionUID = 1L;
+		@Bean public FreemarkerMixin freemarker() {
+			return ConsoleFreemarkerMixin.create()
+				.basePath("/templates/")
+				.chromeTemplate("admin/console-chrome-pagetabs.ftlh")
+				.build();
+		}
+		@RestGet(path="/rest/setup")
+		public View setup() {
+			return FreemarkerView.of("admin/page-naked.ftlh");
+		}
+	}
+
+	@Test void a01_nestedPage_chromeAndThemeHrefsAreSiteRootAndFetchable() throws Exception {
+		try (var c = MockRestClient.buildLax(NestedPageHost.class);
+			var page = c.get("/rest/setup").run()) {
+			page.assertStatus(200);
+			var html = page.getContent().asString();
+			var pageUri = URI.create("http://localhost/rest/setup");
+
+			var chromeHref = requiredHref(html, "chrome.css");
+			var themeHref = requiredHref(html, "juneau-theme-");
+			var chromePath = resolvedPath(pageUri, chromeHref);
+			var themePath = resolvedPath(pageUri, themeHref);
+
+			assertEquals("/juneau-console/chrome.css", chromePath,
+				() -> "chrome.css must be site-root, not under the page path; href=" + chromeHref);
+			assertTrue(THEME_PATH.matcher(themePath).matches(),
+				() -> "theme CSS must be site-root, not under the page path; href=" + themeHref + " path=" + themePath);
+
+			try (var chrome = c.get(chromePath).run()) {
+				chrome.assertStatus(200).assertHeader("Content-Type").isContains("text/css");
+				var css = chrome.getContent().asString();
+				assertTrue(css.contains(".jc-header"), () -> css);
+				assertTrue(css.contains(".juneau-page-nav"), () -> css);
+				assertTrue(css.contains("display: flex") || css.contains("display:flex"), () -> css);
+			}
+			try (var theme = c.get(themePath).run()) {
+				theme.assertStatus(200).assertHeader("Content-Type").isContains("text/css");
+			}
+
+			assertTrue(html.contains("<header class=\"jc-header\">"), () -> html);
+			assertTrue(html.contains("class=\"juneau-page-nav\""), () -> html);
+			assertEquals(3, count(html, "class=\"juneau-page-nav-section\""),
+				() -> "Home, Items, and Admin must be separate section links: " + html);
+			assertTrue(html.contains(">Home</a>"), () -> html);
+			assertTrue(html.contains(">Items</a>"), () -> html);
+			assertTrue(html.contains(">Admin</a>"), () -> html);
+			assertFalse(html.contains("slds-"), () -> html);
+		}
+	}
+
+	static String requiredHref(String html, String needle) {
+		for (var href : stylesheetHrefs(html)) {
+			if (href.contains(needle) && href.contains("juneau-console"))
+				return href;
+		}
+		fail("no juneau-console stylesheet href containing '" + needle + "' in:\n" + html);
+		return "";
+	}
+
+	static List<String> stylesheetHrefs(String html) {
+		var out = new ArrayList<String>();
+		var m = STYLESHEET_HREF.matcher(html);
+		while (m.find())
+			out.add(m.group(1));
+		return out;
+	}
+
+	static String resolvedPath(URI pageUri, String href) {
+		var resolved = pageUri.resolve(href);
+		var path = resolved.getPath();
+		return path == null ? "" : path;
+	}
+
+	static int count(String body, String needle) {
+		var n = 0;
+		for (var i = body.indexOf(needle); i >= 0; i = body.indexOf(needle, i + needle.length()))
+			n++;
+		return n;
+	}
+}
diff --git a/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_Test.java b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_Test.java
index e295da020f..d88446b7c1 100644
--- a/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_Test.java
+++ b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/java/org/apache/juneau/rest/server/view/freemarker/console/ConsoleDirective_Test.java
@@ -269,9 +269,11 @@ class ConsoleDirective_Test extends TestBase {
 		// Document <title> falls back to brand= when title= is absent.
 		assertTrue(body.contains("<title>My App</title>"), () -> body);
 
-		// Head cascade.
+		// Head cascade. Chrome/theme hrefs are context-root-absolute against the standalone
+		// /juneau-console/* mount (leading slash), not servlet-relative under the page resource.
 		assertTrue(body.contains("name=\"page-tab\"") && body.contains("releases"), () -> body);
-		assertTrue(body.contains("chrome.css"), () -> body);
+		assertTrue(body.contains("href=\"/juneau-console/chrome.css"), () -> body);
+		assertTrue(body.contains("href=\"/juneau-console/themes/juneau-theme-light-red.css"), () -> body);
 		assertEquals(1, count(body, "juneau-theme-light-red.css"), () -> body);
 		assertFalse(body.contains("juneau-theme-open.css"), () -> body);  // theme= wins; no default block
 		assertTrue(body.contains("href=\"a.css\"") && body.contains("href=\"b.css\""), () -> body);
@@ -402,6 +404,22 @@ class ConsoleDirective_Test extends TestBase {
 		assertTrue(body.indexOf("<main") < body.indexOf("jc-page-footer"), () -> body);
 	}
 
+	@Test void c13_consoleAssetHrefs_areContextRootAbsoluteUnderANestedPagePath() throws Exception {
+		// The live regression: a page resource at /rest/setup reports that path as its servlet path, so
+		// servlet:/juneau-console/chrome.css becomes /rest/setup/juneau-console/chrome.css (404). The shell
+		// must emit context-root-absolute /juneau-console/... regardless of the page resource's path.
+		String body;
+		try (var c = MockRestClient.createLax(ConsoleHost.class).servletPath("/rest/setup").build();
+			var rsp = c.get("/assets").run()) {
+			rsp.assertStatus(200);
+			body = rsp.getContent().asString();
+		}
+		assertTrue(body.contains("href=\"/juneau-console/chrome.css"), () -> body);
+		assertTrue(body.contains("href=\"/juneau-console/themes/juneau-theme-light-red.css"), () -> body);
+		assertFalse(body.contains("/rest/setup/juneau-console/"), () -> body);
+		assertFalse(body.contains("href=\"juneau-console/chrome.css"), () -> body);  // no leading-slash-less relative
+	}
+
 	@Test void c12_bareConsole_omitsHeaderEntirely() throws Exception {
 		String body;
 		try (var c = MockRestClient.buildLax(BareHost.class);
diff --git a/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/resources/templates/admin/console-chrome-pagetabs.ftlh b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/resources/templates/admin/console-chrome-pagetabs.ftlh
new file mode 100644
index 0000000000..fa9f400700
--- /dev/null
+++ b/juneau-rest/juneau-rest-server-console-ui-freemarker/src/test/resources/templates/admin/console-chrome-pagetabs.ftlh
@@ -0,0 +1,23 @@
+<#--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+--><@console theme="light-red" icon="/app/logo.svg" brand="My App" chrome="true">
+<@navigation>
+<@node id="home" label="Home" href="/home"/>
+<@node id="items" label="Items" href="/items"/>
+<@node id="admin" label="Admin" href="/admin"/>
+</...@navigation>
+<@main/>
+</...@console>
diff --git a/juneau-rest/juneau-rest-server-console-ui/src/main/java/org/apache/juneau/rest/server/console/ConsoleChromeMixin.java b/juneau-rest/juneau-rest-server-console-ui/src/main/java/org/apache/juneau/rest/server/console/ConsoleChromeMixin.java
index 72fa9f66ab..b7bf582e40 100644
--- a/juneau-rest/juneau-rest-server-console-ui/src/main/java/org/apache/juneau/rest/server/console/ConsoleChromeMixin.java
+++ b/juneau-rest/juneau-rest-server-console-ui/src/main/java/org/apache/juneau/rest/server/console/ConsoleChromeMixin.java
@@ -397,45 +397,55 @@ public class ConsoleChromeMixin {
 	}
 
 	/**
-	 * Returns a real, browser-fetchable URL for a shipped theme pack, resolved against the given request's context
-	 * path and mount and carrying the same {@code ?v=<buildVersion>-<hash8>} content-sensitive cache-buster the
-	 * logo/page-background overrides use (see {@link #buildBody(RestRequest)}).
+	 * Returns a real, browser-fetchable URL for a shipped theme pack, resolved against the request's
+	 * <b>context root</b> (not the page resource) and carrying the same {@code ?v=<buildVersion>-<hash8>}
+	 * content-sensitive cache-buster the logo/page-background overrides use (see {@link #buildBody(RestRequest)}).
 	 *
 	 * <p>
-	 * Mirrors {@link #assetUrl(RestRequest, String, String)}: under the standalone mount style the container has
-	 * already consumed {@code /juneau-console} into {@code servletPath}, so the unprefixed path is resolved; under
-	 * the composed style the prefixed path is. A template-rendering consumer (the FTL {@code <@theme>} directive)
-	 * sits downstream of Juneau's {@code servlet:}-rewriting serializer, so it needs the URL already resolved here.
-	 *
-	 * @param req The current request, supplying the context path/mount to resolve against.
+	 * The console document shell ({@code <@console>} / {@code <@theme>}) emits this as a {@code <link href>} to the
+	 * standalone {@code /juneau-console/*} mount. Resolving with {@code context:} rather than {@code servlet:} is
+	 * load-bearing: a page resource at {@code /rest/setup} reports that path as its servlet path, so
+	 * {@code servlet:/juneau-console/themes/…} would become {@code /rest/setup/juneau-console/themes/…} and 404.
+	 * A template-rendering consumer sits downstream of Juneau's {@code servlet:}-rewriting serializer, so it needs
+	 * the URL already resolved here.
+	 *
+	 * @param req The current request, supplying the context path to resolve against.
 	 * @param name One of the {@link #BUILTIN_THEME_NAMES}.
-	 * @return The absolute theme-pack URL with the version+content-hash cache-buster appended.
+	 * @return The context-root-absolute theme-pack URL with the version+content-hash cache-buster appended.
 	 */
 	public static String themeAssetUrl(RestRequest req, String name) {
 		var file = "juneau-theme-" + name + ".css";
-		var standalone = req.getServletPath().endsWith(MOUNT_PREFIX);
-		var base = req.getUriResolver().resolve("servlet:" + (standalone ? THEME_CSS_DIR_UNPREFIXED : THEME_CSS_DIR) + file);
-		return base + ASSET_CACHE.cacheBuster(THEME_CSS_RESOURCE_DIR + file);
+		return consoleMountUrl(req, THEME_CSS_DIR + file, THEME_CSS_RESOURCE_DIR + file);
 	}
 
 	/**
-	 * Returns a real, browser-fetchable URL for the shipped {@code chrome.css}, resolved against the given request's
-	 * context path and mount and carrying the same content-sensitive {@code ?v=…} cache-buster the theme packs use.
+	 * Returns a real, browser-fetchable URL for the shipped {@code chrome.css}, resolved against the request's
+	 * <b>context root</b> (not the page resource) and carrying the same content-sensitive {@code ?v=…} cache-buster
+	 * the theme packs use.
 	 *
 	 * <p>
-	 * Mirrors {@link #themeAssetUrl(RestRequest, String)}: under the standalone mount the container has already
-	 * consumed {@code /juneau-console} into {@code servletPath}, so the unprefixed path is resolved; under the
-	 * composed mount the prefixed path is. The console document shell ({@code <@console>} FTL directive) emits this
-	 * URL as the first stylesheet in its head cascade so apps no longer hand-write the {@code chrome.css}
-	 * {@code <link>}.
-	 *
-	 * @param req The current request, supplying the context path/mount to resolve against.
-	 * @return The absolute {@code chrome.css} URL with the version+content-hash cache-buster appended.
+	 * The console document shell ({@code <@console>}) emits this as the first stylesheet in its head cascade. See
+	 * {@link #themeAssetUrl(RestRequest, String)} for why the resolution is {@code context:} rather than
+	 * {@code servlet:}.
+	 *
+	 * @param req The current request, supplying the context path to resolve against.
+	 * @return The context-root-absolute {@code chrome.css} URL with the version+content-hash cache-buster appended.
 	 */
 	public static String chromeCssUrl(RestRequest req) {
-		var standalone = req.getServletPath().endsWith(MOUNT_PREFIX);
-		var base = req.getUriResolver().resolve("servlet:" + (standalone ? CHROME_CSS_PATH_UNPREFIXED : CHROME_CSS_PATH));
-		return base + ASSET_CACHE.cacheBuster(CHROME_CSS_RESOURCE);
+		return consoleMountUrl(req, CHROME_CSS_PATH, CHROME_CSS_RESOURCE);
+	}
+
+	/**
+	 * Context-root-absolute URL for an asset on the standalone {@code /juneau-console/*} mount.
+	 *
+	 * <p>
+	 * Always the prefixed path ({@link #CHROME_CSS_PATH} / {@link #THEME_CSS_DIR}), resolved with {@code context:}
+	 * so the href is {@code {contextPath}/juneau-console/…} regardless of which page resource rendered the shell.
+	 * Distinct from {@link #assetUrl(RestRequest, String, String)}, which is for {@code url()}s <i>inside</i> the
+	 * served {@code chrome.css} and must still track whichever mount served that stylesheet.
+	 */
+	private static String consoleMountUrl(RestRequest req, String prefixedPath, String resource) {
+		return req.getUriResolver().resolve("context:" + prefixedPath) + ASSET_CACHE.cacheBuster(resource);
 	}
 
 	/**