Skip to content

Commit

Permalink
Merge pull request #41448 from phillip-kruger/undertow-404
Browse files Browse the repository at this point in the history
Make 404 page work on Undertow
  • Loading branch information
phillip-kruger authored Jun 27, 2024
2 parents 250a855 + 2e37b5f commit 058be55
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public TemplateHtmlBuilder staticResourcePath(String title, String description)
}

public TemplateHtmlBuilder servletMapping(String title) {
return resourcePath(title, false, false, null);
return resourcePath(title, false, true, null);
}

private TemplateHtmlBuilder resourcePath(String title, boolean withListStart, boolean withAnchor, String description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.HttpCompressionHandler;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundHandler;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;
import io.quarkus.vertx.http.runtime.devmode.RouteDescription;
import io.quarkus.vertx.http.runtime.devmode.RouteMethodDescription;
import io.quarkus.vertx.http.runtime.security.HttpSecurityRecorder.DefaultAuthFailureHandler;
Expand Down Expand Up @@ -107,7 +107,7 @@ public Handler<RoutingContext> vertxRequestHandler(Supplier<Vertx> vertx, Execut
if (LaunchMode.current() == LaunchMode.DEVELOPMENT) {
// For Not Found Screen
Registry registry = deployment.getRegistry();
ResourceNotFoundHandler.runtimeRoutes = fromBoundResourceInvokers(registry, nonJaxRsClassNameToMethodPaths);
ResourceNotFoundData.setRuntimeRoutes(fromBoundResourceInvokers(registry, nonJaxRsClassNameToMethodPaths));
}

return handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundHandler;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;
import io.quarkus.vertx.http.runtime.devmode.RouteDescription;
import io.quarkus.vertx.http.runtime.devmode.RouteMethodDescription;
import io.quarkus.vertx.http.runtime.security.HttpSecurityRecorder.DefaultAuthFailureHandler;
Expand Down Expand Up @@ -158,7 +158,7 @@ closeTaskHandler, contextFactory, new ArcThreadSetupAction(beanContainer.request

if (LaunchMode.current() == LaunchMode.DEVELOPMENT) {
// For Not Found Screen
ResourceNotFoundHandler.runtimeRoutes = fromClassMappers(deployment.getClassMappers());
ResourceNotFoundData.setRuntimeRoutes(fromClassMappers(deployment.getClassMappers()));
// For Dev UI Screen
RuntimeResourceVisitor.visitRuntimeResources(deployment.getClassMappers(), ScoreSystem.ScoreVisitor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.undertow.runtime;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import jakarta.enterprise.inject.spi.CDI;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;
import io.vertx.core.json.Json;

public class QuarkusNotFoundServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ResourceNotFoundData resourceNotFoundData = CDI.current().select(ResourceNotFoundData.class).get();
String accept = req.getHeader("Accept");
if (accept != null && accept.contains("application/json")) {
resp.setContentType("application/json");
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
resp.getWriter().write(Json.encodePrettily(resourceNotFoundData.getJsonContent()));
} else {
//We default to HTML representation
resp.setContentType("text/html");
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
resp.getWriter().write(resourceNotFoundData.getHTMLContent());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import io.quarkus.vertx.http.runtime.HttpCompressionHandler;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundHandler;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser;
import io.undertow.httpcore.BufferAllocator;
import io.undertow.httpcore.StatusCodes;
Expand Down Expand Up @@ -278,7 +278,7 @@ public void addServletMapping(RuntimeValue<DeploymentInfo> info, String name, St
if (sv != null) {
sv.addMapping(mapping);
if (LaunchMode.current() == LaunchMode.DEVELOPMENT) {
ResourceNotFoundHandler.addServlet(mapping);
ResourceNotFoundData.addServlet(mapping);
}
}
}
Expand Down Expand Up @@ -463,20 +463,27 @@ public DeploymentManager bootServletContainer(RuntimeValue<DeploymentInfo> info,
if (info.getValue().getExceptionHandler() == null) {
//if a 500 error page has not been mapped we change the default to our more modern one, with a UID in the
//log. If this is not production we also include the stack trace
boolean alreadyMapped = false;
boolean alreadyMapped500 = false;
boolean alreadyMapped404 = false;
for (ErrorPage i : info.getValue().getErrorPages()) {
if (i.getErrorCode() != null && i.getErrorCode() == StatusCodes.INTERNAL_SERVER_ERROR) {
alreadyMapped = true;
break;
alreadyMapped500 = true;
} else if (i.getErrorCode() != null && i.getErrorCode() == StatusCodes.NOT_FOUND) {
alreadyMapped404 = true;
}
}
if (!alreadyMapped || launchMode.isDevOrTest()) {
if (!alreadyMapped500 || launchMode.isDevOrTest()) {
info.getValue().setExceptionHandler(new QuarkusExceptionHandler());
info.getValue().addErrorPage(new ErrorPage("/@QuarkusError", StatusCodes.INTERNAL_SERVER_ERROR));
info.getValue().addServlet(new ServletInfo("@QuarkusError", QuarkusErrorServlet.class)
.addMapping("/@QuarkusError").setAsyncSupported(true)
.addInitParam(QuarkusErrorServlet.SHOW_STACK, Boolean.toString(launchMode.isDevOrTest())));
}
if (!alreadyMapped404 && launchMode.equals(LaunchMode.DEVELOPMENT)) {
info.getValue().addErrorPage(new ErrorPage("/@QuarkusNotFound", StatusCodes.NOT_FOUND));
info.getValue().addServlet(new ServletInfo("@QuarkusNotFound", QuarkusNotFoundServlet.class)
.addMapping("/@QuarkusNotFound").setAsyncSupported(true));
}
}
setupRequestScope(info.getValue(), beanContainer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.devui.deployment.InternalPageBuildItem;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.devui.spi.page.Page;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;

/**
* This creates Endpoints Page
*/
public class EndpointsProcessor {
private static final String NAMESPACE = "devui-endpoints";
private static final String DEVUI = "dev-ui";

@BuildStep(onlyIf = IsDevelopment.class)
Expand All @@ -23,17 +26,22 @@ InternalPageBuildItem createEndpointsPage(NonApplicationRootPathBuildItem nonApp

// Page
endpointsPage.addPage(Page.webComponentPageBuilder()
.namespace("devui-endpoints")
.namespace(NAMESPACE)
.title("Endpoints")
.icon("font-awesome-solid:plug")
.componentLink("qwc-endpoints.js"));

endpointsPage.addPage(Page.webComponentPageBuilder()
.namespace("devui-endpoints")
.namespace(NAMESPACE)
.title("Routes")
.icon("font-awesome-solid:route")
.componentLink("qwc-routes.js"));

return endpointsPage;
}

@BuildStep(onlyIf = IsDevelopment.class)
JsonRPCProvidersBuildItem createJsonRPCService() {
return new JsonRPCProvidersBuildItem(NAMESPACE, ResourceNotFoundData.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
Expand All @@ -20,6 +22,7 @@
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.VertxWebRouterBuildItem;
import io.quarkus.vertx.http.runtime.devmode.AdditionalRouteDescription;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundData;
import io.quarkus.vertx.http.runtime.devmode.ResourceNotFoundRecorder;
import io.quarkus.vertx.http.runtime.devmode.RouteDescription;
import io.vertx.core.Handler;
Expand All @@ -29,11 +32,19 @@ public class NotFoundProcessor {

private static final String META_INF_RESOURCES = "META-INF/resources";

@BuildStep(onlyIf = IsDevelopment.class)
AdditionalBeanBuildItem resourceNotFoundDataAvailable() {
return AdditionalBeanBuildItem.builder()
.addBeanClass(ResourceNotFoundData.class)
.setUnremovable().build();
}

@BuildStep(onlyIf = IsDevelopment.class)
@Record(RUNTIME_INIT)
void routeNotFound(ResourceNotFoundRecorder recorder,
VertxWebRouterBuildItem router,
HttpRootPathBuildItem httpRoot,
BeanContainerBuildItem beanContainer,
LaunchModeBuildItem launchMode,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
List<RouteDescriptionBuildItem> routeDescriptions,
Expand Down Expand Up @@ -66,6 +77,7 @@ void routeNotFound(ResourceNotFoundRecorder recorder,
router.getHttpRouter(),
router.getMainRouter(),
router.getManagementRouter(),
beanContainer.getValue(),
getBaseUrl(launchMode),
httpRoot.getRootPath(),
routes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { LitElement, html, css} from 'lit';
import { basepath } from 'devui-data';
import '@vaadin/progress-bar';
import '@vaadin/grid';
import { columnBodyRenderer } from '@vaadin/grid/lit.js';
import '@vaadin/grid/vaadin-grid-sort-column.js';
import { JsonRpc } from 'jsonrpc';

/**
* This component show all available endpoints
*/
export class QwcEndpoints extends LitElement {
jsonRpc = new JsonRpc(this);

static styles = css`
.infogrid {
Expand Down Expand Up @@ -44,25 +45,15 @@ export class QwcEndpoints extends LitElement {
this._info = null;
}

async connectedCallback() {
connectedCallback() {
super.connectedCallback();
await this.load();
this.jsonRpc.getJsonContent().then(jsonRpcResponse => {
this._info = jsonRpcResponse.result;
});
}

async load() {
const response = await fetch("/quarkus404", {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
const data = await response.json();
this._info = data;
}


render() {
if (this._info) {

const typeTemplates = [];
for (const [type, list] of Object.entries(this._info)) {
typeTemplates.push(html`${this._renderType(type,list)}`);
Expand Down
Loading

0 comments on commit 058be55

Please sign in to comment.