Skip to content

Commit

Permalink
get built in mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Nov 25, 2024
1 parent 13aae48 commit 2202b48
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
19 changes: 16 additions & 3 deletions avaje-jex/src/main/java/io/avaje/jex/AbstractStaticHandler.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package io.avaje.jex;

import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

import com.sun.net.httpserver.HttpExchange;

import io.avaje.jex.http.BadRequestException;
import io.avaje.jex.http.NotFoundException;

abstract sealed class AbstractStaticHandler implements ExchangeHandler permits StaticFileHandler, JrtResourceHandler {
abstract sealed class AbstractStaticHandler implements ExchangeHandler
permits StaticFileHandler, JrtResourceHandler {

protected final Map<String, String> mimeTypes;
protected final String filesystemRoot;
protected final String urlPrefix;
protected final Predicate<Context> skipFilePredicate;
protected final Map<String, String> headers;
private static final FileNameMap mimeMap = URLConnection.getFileNameMap();

protected AbstractStaticHandler(
String urlPrefix,
Expand Down Expand Up @@ -50,7 +56,14 @@ protected String getExt(String path) {
}

protected String lookupMime(String path) {
String ext = getExt(path).toLowerCase();
return mimeTypes.getOrDefault(ext, "application/octet-stream");
var lower = path.toLowerCase();

return Objects.requireNonNullElseGet(
mimeMap.getContentTypeFor(path),
() -> {
String ext = getExt(lower);

return mimeTypes.getOrDefault(ext, "application/octet-stream");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,12 @@ final class StaticResourceHandlerBuilder implements StaticContentConfig {

static final Predicate<Context> NO_OP_PREDICATE = ctx -> false;
private static final String TEXT_PLAIN = "text/plain";
private static final Map<String, String> MIME_MAP =
Map.ofEntries(
entry("css", "text/css"),
entry("gif", "image/gif"),
entry("html", "text/html"),
entry("js", "application/javascript"),
entry("json", "application/json"),
entry("jpg", "image/jpeg"),
entry("jpeg", "image/jpeg"),
entry("mp4", "video/mp4"),
entry("pdf", "application/pdf"),
entry("png", "image/png"),
entry("svg", "image/svg+xml"),
entry("xlsm", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
entry("xml", "application/xml"),
entry("zip", "application/zip"),
entry("md", TEXT_PLAIN),
entry("txt", TEXT_PLAIN),
entry("php", TEXT_PLAIN));

private String path = "/";
private String root = "/public/";
private String directoryIndex = null;
private StaticResourceLoader resourceLoader = CoreServiceLoader.resourceLoader();
private final Map<String, String> mimeTypes = new HashMap<>(MIME_MAP);
private final Map<String, String> mimeTypes = new HashMap<>();
private final Map<String, String> headers = new HashMap<>();
private Predicate<Context> skipFilePredicate = NO_OP_PREDICATE;
private ResourceLocation location = CLASS_PATH;
Expand Down
6 changes: 4 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;

Expand All @@ -23,6 +23,7 @@ public class JdkServerStart {
public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {

try {
// SimpleFileServer server;
final HttpServer server;

var port = new InetSocketAddress(jex.config().port());
Expand All @@ -49,7 +50,8 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana
server.start();

jex.lifecycle().status(AppLifecycle.Status.STARTED);
log.log(Level.INFO, "started com.sun.net.httpserver.HttpServer server on port %s".formatted(port));
String jexVersion = Jex.class.getPackage().getImplementationVersion();
log.log(Level.INFO, "started server on port {0,number,#} version {1}", port, jexVersion);
return new JdkJexServer(server, jex.lifecycle(), handler);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down

0 comments on commit 2202b48

Please sign in to comment.