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

fix(webclientassets): produce 404 instead of 500 when minimal #628

Merged
merged 1 commit into from
Aug 6, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,34 @@
*/
package io.cryostat.net.web.http.generic;

import java.nio.file.Path;

import javax.inject.Inject;

import io.cryostat.net.web.WebServer;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.RequestHandler;
import io.cryostat.net.web.http.api.ApiVersion;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;

class WebClientAssetsGetHandler implements RequestHandler {

static final String WEB_CLIENT_ASSETS_BASE =
WebServer.class.getPackageName().replaceAll("\\.", "/");
private static final Path INDEX_HTML =
Path.of(WEB_CLIENT_ASSETS_BASE, "index.html").normalize();

private final boolean hasIndexHtml;

@Inject
WebClientAssetsGetHandler() {}
WebClientAssetsGetHandler(Vertx vertx) {
this.hasIndexHtml = vertx.fileSystem().existsBlocking(INDEX_HTML.toString());
}

@Override
public ApiVersion apiVersion() {
Expand All @@ -78,7 +88,10 @@ public String path() {

@Override
public void handle(RoutingContext ctx) {
if (!hasIndexHtml) {
throw new HttpStatusException(404);
}
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.HTML.mime());
ctx.response().sendFile(WEB_CLIENT_ASSETS_BASE + "/index.html");
ctx.response().sendFile(INDEX_HTML.toString());
}
}