Apply code review suggestions

This commit is contained in:
chimp1984 2022-06-06 19:24:02 +02:00
parent a33975c446
commit 27b78b6217
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
3 changed files with 9 additions and 11 deletions

View file

@ -21,7 +21,7 @@ import lombok.Getter;
@Getter @Getter
public class ErrorMessage { public class ErrorMessage {
private String error; private final String error;
public ErrorMessage(String error) { public ErrorMessage(String error) {
this.error = error; this.error = error;

View file

@ -46,6 +46,7 @@ public class StatusException extends RuntimeException {
public static class StatusExceptionMapper implements ExceptionMapper<StatusException> { public static class StatusExceptionMapper implements ExceptionMapper<StatusException> {
@Override @Override
public Response toResponse(StatusException exception) { public Response toResponse(StatusException exception) {
log.error("", exception);
return Response.status(exception.getHttpStatus()) return Response.status(exception.getHttpStatus())
.entity(new ErrorMessage(exception.getMessage())) .entity(new ErrorMessage(exception.getMessage()))
.build(); .build();

View file

@ -26,9 +26,6 @@ import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -45,17 +42,16 @@ import com.sun.net.httpserver.HttpHandler;
* This handler is limited to html,css,json and javascript files. * This handler is limited to html,css,json and javascript files.
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor
public class StaticFileHandler implements HttpHandler { public class StaticFileHandler implements HttpHandler {
private static final String NOT_FOUND = "404 (Not Found)\n"; private static final String NOT_FOUND = "404 (Not Found)\n";
public static final String[] VALID_SUFFIX = {".html", ".json", ".css", ".js"}; public static final String[] VALID_SUFFIX = {".html", ".json", ".css", ".js"};
@Getter @Getter
@Setter protected final String rootContext;
@NonNull
protected String rootContext; public StaticFileHandler(String rootContext) {
ClassLoader classLoader = getClass().getClassLoader(); this.rootContext = rootContext;
}
public void handle(HttpExchange exchange) throws IOException { public void handle(HttpExchange exchange) throws IOException {
URI uri = exchange.getRequestURI(); URI uri = exchange.getRequestURI();
@ -74,7 +70,7 @@ public class StaticFileHandler implements HttpHandler {
} }
// we are using getResourceAsStream to ultimately prevent load from parent directories // we are using getResourceAsStream to ultimately prevent load from parent directories
try (InputStream resource = classLoader.getResourceAsStream(resourceName)) { try (InputStream resource = getClass().getClassLoader().getResourceAsStream(resourceName)) {
if (resource == null) { if (resource == null) {
respond404(exchange); respond404(exchange);
return; return;
@ -89,6 +85,7 @@ public class StaticFileHandler implements HttpHandler {
Headers headers = exchange.getResponseHeaders(); Headers headers = exchange.getResponseHeaders();
headers.set("Content-Type", mime); headers.set("Content-Type", mime);
headers.add("Cache-Control", "max-age=3600"); // cache static content on browser for 3600 seconds
exchange.sendResponseHeaders(200, 0); exchange.sendResponseHeaders(200, 0);
try (OutputStream outputStream = exchange.getResponseBody()) { try (OutputStream outputStream = exchange.getResponseBody()) {