Skip to content
Merged
Show file tree
Hide file tree
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 @@ -12,7 +12,7 @@
import io.avaje.jex.http.NotFoundException;

abstract sealed class AbstractStaticHandler implements ExchangeHandler
permits StaticFileHandler, PathResourceHandler, JarResourceHandler {
permits StaticFileHandler, StaticClassResourceHandler {

protected final Map<String, String> mimeTypes;
protected final String filesystemRoot;
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/ClassResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static ClassResourceLoader fromClass(Class<?> clazz) {
}

/** Return the URL for the given resource or return null if it cannot be found. */
URL getResource(String resourcePath);
URL loadResource(String resourcePath);

InputStream getResourceAsStream(String resourcePath);
InputStream loadResourceAsStream(String resourcePath);
}
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ default List<String> formParams(String key) {
/**
* Return the underlying JDK {@link HttpExchange} object backing the context
*/
HttpExchange jdkExchange();
HttpExchange exchange();

/**
* Return the request scheme.
Expand Down
34 changes: 26 additions & 8 deletions avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer;

import javax.net.ssl.SSLContext;

import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

Expand All @@ -16,12 +21,13 @@ final class DJexConfig implements JexConfig {
private String contextPath = "/";
private boolean health = true;
private boolean ignoreTrailingSlashes = true;
private ThreadFactory factory;
private Executor executor;

private boolean preCompressStaticFiles;
private JsonService jsonService;
private final Map<String, TemplateRender> renderers = new HashMap<>();
private SSLContext sslContext;
private final CompressionConfig compression= new CompressionConfig();

@Override
public JexConfig port(int port) {
Expand Down Expand Up @@ -72,17 +78,18 @@ public JexConfig renderer(String extension, TemplateRender renderer) {
}

@Override
public ThreadFactory threadFactory() {
if (factory == null) {
factory =
Thread.ofVirtual().name("avaje-jex-http-", 0).factory();
public Executor executor() {
if (executor == null) {
executor =
Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().name("avaje-jex-http-", 0).factory());
}
return factory;
return executor;
}

@Override
public JexConfig threadFactory(ThreadFactory factory) {
this.factory = factory;
public JexConfig executor(Executor factory) {
this.executor = factory;
return this;
}

Expand Down Expand Up @@ -136,4 +143,15 @@ public JexConfig sslContext(SSLContext ssl) {
this.sslContext = ssl;
return this;
}

@Override
public JexConfig compression(Consumer<CompressionConfig> consumer) {
consumer.accept(compression);
return this;
}

@Override
public CompressionConfig compression() {
return compression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ final class DefaultResourceLoader implements ClassResourceLoader {

private final Class<?> clazz;

DefaultResourceLoader() {

this.clazz = DefaultResourceLoader.class;
}

DefaultResourceLoader(Class<?> clazz) {

this.clazz = clazz;
}

@Override
public URL getResource(String resourcePath) {
public URL loadResource(String resourcePath) {

var url = clazz.getResource(resourcePath);
if (url == null) {
Expand All @@ -29,7 +34,7 @@ public URL getResource(String resourcePath) {
}

@Override
public InputStream getResourceAsStream(String resourcePath) {
public InputStream loadResourceAsStream(String resourcePath) {

var url = clazz.getResourceAsStream(resourcePath);
if (url == null) {
Expand Down
15 changes: 11 additions & 4 deletions avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.avaje.jex;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer;

import javax.net.ssl.SSLContext;

import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

Expand Down Expand Up @@ -58,14 +61,14 @@ public sealed interface JexConfig permits DJexConfig {
JexConfig renderer(String extension, TemplateRender renderer);

/**
* ThreadFactory for serving requests. Defaults to a {@link Thread#ofVirtual()} factory
* Set executor for serving requests.
*/
JexConfig threadFactory(ThreadFactory executor);
JexConfig executor(Executor executor);

/**
* Executor for serving requests. Defaults to {@link Executors#newVirtualThreadPerTaskExecutor()}
* Executor for serving requests. Defaults to a {@link Executors#newVirtualThreadPerTaskExecutor()}
*/
ThreadFactory threadFactory();
Executor executor();

/**
* Return the port to use.
Expand Down Expand Up @@ -113,4 +116,8 @@ public sealed interface JexConfig permits DJexConfig {
*/
Map<String, TemplateRender> renderers();

JexConfig compression(Consumer<CompressionConfig> consumer);

CompressionConfig compression();

}
84 changes: 0 additions & 84 deletions avaje-jex/src/main/java/io/avaje/jex/PathResourceHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.Map;
import java.util.function.Predicate;

final class JarResourceHandler extends AbstractStaticHandler implements ExchangeHandler {
final class StaticClassResourceHandler extends AbstractStaticHandler implements ExchangeHandler {

private final URL indexFile;
private final URL singleFile;
private final ClassResourceLoader resourceLoader;

JarResourceHandler(
StaticClassResourceHandler(
String urlPrefix,
String filesystemRoot,
Map<String, String> mimeTypes,
Expand All @@ -31,7 +31,7 @@ final class JarResourceHandler extends AbstractStaticHandler implements Exchange
@Override
public void handle(Context ctx) throws IOException {

final var jdkExchange = ctx.jdkExchange();
final var jdkExchange = ctx.exchange();

if (singleFile != null) {
sendURL(ctx, singleFile.getPath(), singleFile);
Expand All @@ -58,12 +58,12 @@ public void handle(Context ctx) throws IOException {
reportPathTraversal();
}

try (var fis = resourceLoader.getResourceAsStream(normalizedPath)) {
try (var fis = resourceLoader.loadResourceAsStream(normalizedPath)) {
ctx.header("Content-Type", lookupMime(normalizedPath));
ctx.headers(headers);
ctx.write(fis);
} catch (final Exception e) {
throw404(ctx.jdkExchange());
throw404(ctx.exchange());
}
}

Expand All @@ -74,7 +74,7 @@ private void sendURL(Context ctx, String urlPath, URL path) throws IOException {
ctx.headers(headers);
ctx.write(fis);
} catch (final Exception e) {
throw404(ctx.jdkExchange());
throw404(ctx.exchange());
}
}
}
5 changes: 2 additions & 3 deletions avaje-jex/src/main/java/io/avaje/jex/StaticFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class StaticFileHandler extends AbstractStaticHandler implements ExchangeH
@Override
public void handle(Context ctx) throws IOException {

final var jdkExchange = ctx.jdkExchange();
final var jdkExchange = ctx.exchange();

if (singleFile != null) {
sendFile(ctx, jdkExchange, singleFile.getPath(), singleFile);
Expand Down Expand Up @@ -77,8 +77,7 @@ private void sendFile(Context ctx, HttpExchange jdkExchange, String urlPath, Fil
String mimeType = lookupMime(urlPath);
ctx.header("Content-Type", mimeType);
ctx.headers(headers);
jdkExchange.sendResponseHeaders(200, canonicalFile.length());
fis.transferTo(jdkExchange.getResponseBody());
ctx.write(fis);
} catch (FileNotFoundException e) {
throw404(jdkExchange);
}
Expand Down
Loading