Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure request context path is added to client library URLs. #6

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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="fix" dev="sseifert" issue="6">
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", IncludeUtil.appendRequestPath(libraryPath, request));
markup.append(builder.build());
}
return markup.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -97,7 +98,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 Expand Up @@ -133,4 +134,14 @@ else if (resourceResolver.getResource(library.getPath()) == null) {
return result;
}

/**
* Appends context path from current request.
* @param path Path
* @param request Current request
* @return Path with context path
*/
public static @NotNull String appendRequestPath(@NotNull String path, @NotNull SlingHttpServletRequest request) {
return request.getContextPath() + path;
}

}
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", IncludeUtil.appendRequestPath(libraryPath, request));
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