Skip to content

Commit

Permalink
Fix not-found links when management enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Nov 27, 2024
1 parent d99e142 commit 9c5adf7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ function changeViewMode() {
</script>
""";

private static final String NOT_FOUND_SORTING = """
<script>
let isAlphabetical = false;
function sortNotFoundLinks() {
const resourcesDiv = document.querySelector('.sortable');
const h3Elements = Array.from(resourcesDiv.querySelectorAll('h3'));
h3Elements.sort((a, b) => {
const urlA = a.querySelector('a').href;
const urlB = b.querySelector('a').href;
if (isAlphabetical) {
return urlA.localeCompare(urlB); // Sort alphabetically
} else {
const slashCountA = (urlA.match(/\\//g) || []).length;
const slashCountB = (urlB.match(/\\//g) || []).length;
return slashCountA - slashCountB; // Sort by number of slashes
}});
// Reattach sorted elements to the container
h3Elements.forEach(h3=>resourcesDiv.appendChild(h3));
// Toggle sort mode
isAlphabetical=!isAlphabetical;}</script>""";

private static final String SORT_NOT_FOUND_LINKS = """
<p class="sorticon" onclick="sortNotFoundLinks();" title="Sort">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.6.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path fill="#555555" d="M0 96C0 78.3 14.3 64 32 64l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 128C14.3 128 0 113.7 0 96zM64 256c0-17.7 14.3-32 32-32l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L96 288c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32L32 448c-17.7 0-32-14.3-32-32s14.3-32 32-32l384 0c17.7 0 32 14.3 32 32z"/></svg>
</p>""";

private static final String UTILITIES = """
<div id="utilities-container">
<p class="clipboard tooltip" onclick="changeViewMode();">
Expand Down Expand Up @@ -108,6 +139,7 @@ function changeViewMode() {
" <meta charset=\"utf-8\">\n" +
" <style>%3$s</style>\n" +
SCRIPT_STACKTRACE_MANIPULATION +
NOT_FOUND_SORTING +
"</head>\n" +
"<body onload=\"showDefaultStackTraceOrder();\">\n" +
"<div class=\"header\">\n" +
Expand Down Expand Up @@ -185,9 +217,12 @@ function changeViewMode() {

private static final String RESOURCES_START = "<div class=\"intro\">%1$s</div><div class=\"resources\">";

private static final String RESOURCES_START_WITH_CLASS = "<div class=\"intro\">%1$s " + SORT_NOT_FOUND_LINKS
+ "</div><div class=\"resources %2$s\">";

private static final String ANCHOR_TEMPLATE_ABSOLUTE = "<a href=\"%1$s\">%2$s</a>";

private static final String DESCRIPTION_TEMPLATE = "%1$s %2$s";
private static final String DESCRIPTION_TEMPLATE = "%1$s <span>%2$s</span>";

private static final String RESOURCE_TEMPLATE = "<h3>%1$s</h3>\n";

Expand Down Expand Up @@ -372,6 +407,11 @@ public TemplateHtmlBuilder resourcesStart(String title) {
return this;
}

public TemplateHtmlBuilder resourcesStart(String title, String cssclass) {
result.append(String.format(RESOURCES_START_WITH_CLASS, title, cssclass));
return this;
}

public TemplateHtmlBuilder resourcesEnd() {
result.append(RESOURCES_END);
return this;
Expand Down Expand Up @@ -401,20 +441,14 @@ public TemplateHtmlBuilder servletMapping(String title) {
private TemplateHtmlBuilder resourcePath(String title, boolean withListStart, boolean withAnchor, String description) {
String content;
if (withAnchor) {
String text = title;
if (title.startsWith("/")) {
title = title.substring(1);
}

if (!title.startsWith("http") && baseUrl != null) {
title = baseUrl + title;
}
if (title.startsWith("http")) {
int firstSlashIndex = title.indexOf("/", title.indexOf("//") + 2);
text = title.substring(firstSlashIndex);
}

content = String.format(ANCHOR_TEMPLATE_ABSOLUTE, title, escapeHtml(text));
content = String.format(ANCHOR_TEMPLATE_ABSOLUTE, title, escapeHtml(title));
} else {
content = escapeHtml(title);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ private String concatenateUrl(String part1, String part2) {
return part2;
if (part1 != null && part2 == null)
return part1;
if (part2.startsWith("http://") || part2.startsWith("https://"))
return part2;
if (part1.endsWith("/") && part2.startsWith("/")) {
return part1.substring(0, part1.length() - 1) + part2;
} else if (!part1.endsWith("/") && !part2.startsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ public String getHTMLContent() {

// Additional Endpoints
if (!this.additionalEndpoints.isEmpty()) {
List<AdditionalRouteDescription> endpoints = getSortedAdditionalRouteDescriptions();
builder.resourcesStart(ADDITIONAL_ENDPOINTS);
for (AdditionalRouteDescription additionalEndpoint : endpoints) {
builder.resourcesStart(ADDITIONAL_ENDPOINTS, "sortable");
for (AdditionalRouteDescription additionalEndpoint : this.additionalEndpoints) {
builder.staticResourcePath(additionalEndpoint.getUri(), additionalEndpoint.getDescription());
}
builder.resourcesEnd();
Expand Down Expand Up @@ -190,8 +189,7 @@ public JsonObject getJsonContent() {
// Additional Endpoints
if (!this.additionalEndpoints.isEmpty()) {
JsonArray ae = new JsonArray();
List<AdditionalRouteDescription> endpoints = getSortedAdditionalRouteDescriptions();
for (AdditionalRouteDescription additionalEndpoint : endpoints) {
for (AdditionalRouteDescription additionalEndpoint : this.additionalEndpoints) {
ae.add(JsonObject.of(URI, additionalEndpoint.getUri(), DESCRIPTION, additionalEndpoint.getDescription()));
}
infoMap.put(ADDITIONAL_ENDPOINTS, ae);
Expand Down Expand Up @@ -252,8 +250,7 @@ public String getTextContent() {
// Additional Endpoints
if (!this.additionalEndpoints.isEmpty()) {
sw.write(ADDITIONAL_ENDPOINTS + NL);
List<AdditionalRouteDescription> endpoints = getSortedAdditionalRouteDescriptions();
for (AdditionalRouteDescription additionalEndpoint : endpoints) {
for (AdditionalRouteDescription additionalEndpoint : this.additionalEndpoints) {
sw.write(TAB + "- " + additionalEndpoint.getUri() + NL);
sw.write(TAB + TAB + "- " + additionalEndpoint.getDescription() + NL);
}
Expand All @@ -269,8 +266,8 @@ public String getTextContent() {
private List<RouteDescription> getCombinedRoutes() {
// Endpoints
List<RouteDescription> combinedRoutes = new ArrayList<>();
if (this.runtimeRoutes != null) {
combinedRoutes.addAll(this.runtimeRoutes);
if (ResourceNotFoundData.runtimeRoutes != null) {
combinedRoutes.addAll(ResourceNotFoundData.runtimeRoutes);
}
if (endpointRoutes != null) {
combinedRoutes.addAll(this.endpointRoutes);
Expand Down Expand Up @@ -344,13 +341,6 @@ private boolean isHtmlFileName(String fileName) {
return fileName.endsWith(".html") || fileName.endsWith(".htm") || fileName.endsWith(".xhtml");
}

private List<AdditionalRouteDescription> getSortedAdditionalRouteDescriptions() {
return this.additionalEndpoints.stream().sorted(
Comparator.comparingInt((AdditionalRouteDescription desc) -> desc.getUri().split("/").length)
.thenComparing(AdditionalRouteDescription::getUri))
.toList();
}

private static final String HEADING = "404 - Resource Not Found";
private static final String RESOURCE_ENDPOINTS = "Resource Endpoints";
private static final String SERVLET_MAPPINGS = "Servlet mappings";
Expand Down

0 comments on commit 9c5adf7

Please sign in to comment.