Skip to content

Commit

Permalink
Create a text based 404 based on useragent
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored and gsmet committed Jul 20, 2024
1 parent 6ece690 commit 3eff48e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.quarkus.runtime.TemplateHtmlBuilder.adjustRoot;

import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand Down Expand Up @@ -74,7 +75,7 @@ public String getHTMLContent() {

List<RouteDescription> combinedRoutes = getCombinedRoutes();
TemplateHtmlBuilder builder = new TemplateHtmlBuilder(this.baseUrl,
"404 - Resource Not Found", "", "Resources overview");
HEADING, "", "Resources overview");

builder.resourcesStart(RESOURCE_ENDPOINTS);

Expand Down Expand Up @@ -198,6 +199,70 @@ public JsonObject getJsonContent() {

}

@NonBlocking
public String getTextContent() {
List<RouteDescription> combinedRoutes = getCombinedRoutes();
try (StringWriter sw = new StringWriter()) {
sw.write(NL + HEADING + NL);
sw.write("------------------------" + NL);
sw.write(NL);
// REST Endpoints
if (!combinedRoutes.isEmpty()) {
sw.write(RESOURCE_ENDPOINTS + NL);
for (RouteDescription resource : combinedRoutes) {
for (RouteMethodDescription method : resource.getCalls()) {
String description = method.getHttpMethod();
if (method.getConsumes() != null) {
description = description + " (consumes: " + method.getConsumes() + ")";
}
if (method.getProduces() != null) {
description = description + " (produces:" + method.getProduces() + ")";
}
if (method.getJavaMethod() != null) {
description = description + " (java:" + method.getJavaMethod() + ")";
}
sw.write(TAB + "- " + adjustRoot(this.httpRoot, method.getFullPath()) + NL);
sw.write(TAB + TAB + "- " + description + NL);
}
}
sw.write(NL);
}
// Servlets
if (!servletMappings.isEmpty()) {
sw.write(SERVLET_MAPPINGS + NL);
for (String servletMapping : servletMappings) {
sw.write(TAB + "- " + adjustRoot(this.httpRoot, servletMapping) + NL);
}
sw.write(NL);
}
// Static Resources
if (!this.staticRoots.isEmpty()) {
List<String> resources = findRealResources();
if (!resources.isEmpty()) {
sw.write(STATIC_RESOURCES + NL);
for (String staticResource : resources) {
sw.write(TAB + "- " + adjustRoot(this.httpRoot, staticResource) + NL);
}
sw.write(NL);
}
}

// Additional Endpoints
if (!this.additionalEndpoints.isEmpty()) {
sw.write(ADDITIONAL_ENDPOINTS + NL);
for (AdditionalRouteDescription additionalEndpoint : this.additionalEndpoints) {
sw.write(TAB + "- " + additionalEndpoint.getUri() + NL);
sw.write(TAB + TAB + "- " + additionalEndpoint.getDescription() + NL);
}
sw.write(NL);
}

return sw.toString();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

private List<RouteDescription> getCombinedRoutes() {
// Endpoints
List<RouteDescription> combinedRoutes = new ArrayList<>();
Expand Down Expand Up @@ -276,12 +341,15 @@ private boolean isHtmlFileName(String fileName) {
return fileName.endsWith(".html") || fileName.endsWith(".htm") || fileName.endsWith(".xhtml");
}

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";
private static final String STATIC_RESOURCES = "Static resources";
private static final String ADDITIONAL_ENDPOINTS = "Additional endpoints";
private static final String URI = "uri";
private static final String DESCRIPTION = "description";
private static final String EMPTY = "";
private static final String NL = "\n";
private static final String TAB = "\t";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public void handle(RoutingContext routingContext) {
if (header != null && header.startsWith("application/json")) {
handleJson(routingContext);
} else {
handleHTML(routingContext);
// If not explicitly asked for json, let determine based on the user agent
String userAgent = routingContext.request().getHeader("User-Agent");
if (userAgent != null && (userAgent.contains("Wget") || userAgent.contains("curl"))) {
handleText(routingContext);
} else {
handleHTML(routingContext);
}
}
}

Expand All @@ -34,6 +40,13 @@ private void handleJson(RoutingContext routingContext) {
.end(Json.encodePrettily(resourceNotFoundData.getJsonContent()));
}

private void handleText(RoutingContext routingContext) {
routingContext.response()
.setStatusCode(404)
.putHeader("content-type", "text/plain; charset=utf-8")
.end(resourceNotFoundData.getTextContent());
}

private void handleHTML(RoutingContext routingContext) {
routingContext.response()
.setStatusCode(404)
Expand Down

0 comments on commit 3eff48e

Please sign in to comment.