Skip to content

Commit

Permalink
Ensure request context path is added to client library URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 10, 2024
1 parent e9f8e40 commit c2aadf2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<body>

<release version="1.3.2" date="not released">
<action type="update" dev="sseifert">
Ensure request context path is added to client library URLs.
</action>
<action type="update" dev="sseifert">
Switch to AEM 6.5.17 as minimum version.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class CSSInclude {
private static final Set<String> REL_ALLOWED_VALUES = Set.of(
"prefetch", "preload");

@SlingObject
private SlingHttpServletRequest request;
@SlingObject
private ResourceResolver resourceResolver;
@OSGiService
Expand Down Expand Up @@ -114,7 +116,7 @@ private void activate() {
HtmlTagBuilder builder = new HtmlTagBuilder("link", false, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("href", libraryPath);
builder.setAttr("href", request.getContextPath() + libraryPath);
markup.append(builder.build());
}
return markup.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ else if (categories != null && categories.getClass().isArray()) {
path = "/etc.clientlibs" + path.substring(5);
}
else if (resourceResolver.getResource(library.getPath()) == null) {
// current render resourcer resolver has no access to the client library - ignore it
// current render resource resolver has no access to the client library - ignore it
path = null;
}
return path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class JSInclude {
private static final Set<String> TYPE_ALLOWED_VALUES = Set.of(
"text/javascript", "module");

@SlingObject
private SlingHttpServletRequest request;
@SlingObject
private ResourceResolver resourceResolver;
@OSGiService
Expand Down Expand Up @@ -150,7 +152,7 @@ private void activate() {
HtmlTagBuilder builder = new HtmlTagBuilder("script", true, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("src", libraryPath);
builder.setAttr("src", request.getContextPath() + libraryPath);
markup.append(builder.build());
}
return markup.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ void testSingle() {
underTest.getInclude());
}

@Test
void testSingle_ContextPath() {
context.request().setContextPath("/mycontext");
context.request().setAttribute("categories", CATEGORY_SINGLE);
CSSInclude underTest = AdaptTo.notNull(context.request(), CSSInclude.class);
assertEquals("<link href=\"/mycontext/etc/clientlibs/app1/clientlib1.min.css\" rel=\"stylesheet\" type=\"text/css\">\n",
underTest.getInclude());
}

@ParameterizedTest
@ValueSource(strings = { "preload", "prefetch" })
void testSingle_rel_valid(String validRelParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ void testSingleNoAttributes() {
assertEquals("<script src=\"/etc/clientlibs/app1/clientlib1.min.js\"></script>\n", underTest.getInclude());
}

@Test
void testSingleNoAttributes_ContextPath() {
context.request().setContextPath("/mycontext");
context.request().setAttribute("categories", CATEGORY_SINGLE);
JSInclude underTest = AdaptTo.notNull(context.request(), JSInclude.class);
assertEquals("<script src=\"/mycontext/etc/clientlibs/app1/clientlib1.min.js\"></script>\n", underTest.getInclude());
}

@Test
void testSingleNoAttributesUnminified() {
when(htmlLibraryManager.isMinifyEnabled()).thenReturn(false);
Expand Down

0 comments on commit c2aadf2

Please sign in to comment.