Skip to content

Commit

Permalink
eliminate duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 10, 2024
1 parent 6d8d7aa commit a4e9c3c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void activate() {
HtmlTagBuilder builder = new HtmlTagBuilder("link", false, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("href", request.getContextPath() + libraryPath);
builder.setAttr("href", IncludeUtil.appendRequestPath(libraryPath, request));
markup.append(builder.build());
}
return markup.toString();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/wcm/wcm/ui/clientlibs/components/IncludeUtil.java
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 @@ -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 @@ -152,7 +152,7 @@ private void activate() {
HtmlTagBuilder builder = new HtmlTagBuilder("script", true, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("src", request.getContextPath() + libraryPath);
builder.setAttr("src", IncludeUtil.appendRequestPath(libraryPath, request));
markup.append(builder.build());
}
return markup.toString();
Expand Down

0 comments on commit a4e9c3c

Please sign in to comment.