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 @@ -55,8 +55,8 @@ static Jex.Server start(Jex jex, SpiRoutes routes) {

final var scheme = config.scheme();
final var contextPath = config.contextPath();
SpiServiceManager serviceManager = SpiServiceManager.create(jex);
final var handler = new RoutingHandler(routes, serviceManager, config.compression());
ServiceManager serviceManager = ServiceManager.create(jex);
final var handler = new RoutingHandler(routes, serviceManager);

server.setExecutor(config.executor());
server.createContext(contextPath, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

/** Core implementation of SpiServiceManager provided to specific implementations like jetty etc. */
/** Loads SPI Services. */
final class CoreServiceLoader {

private static final CoreServiceLoader INSTANCE = new CoreServiceLoader();
Expand Down
21 changes: 0 additions & 21 deletions avaje-jex/src/main/java/io/avaje/jex/core/HttpMethodMap.java

This file was deleted.

19 changes: 4 additions & 15 deletions avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import com.sun.net.httpserver.HttpsExchange;

import io.avaje.jex.Context;
import io.avaje.jex.compression.CompressedOutputStream;
import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.http.ErrorCode;
import io.avaje.jex.http.RedirectException;
import io.avaje.jex.security.BasicAuthCredentials;
Expand All @@ -46,8 +44,7 @@ final class JdkContext implements Context {
private static final int SC_MOVED_TEMPORARILY = 302;
private static final String SET_COOKIE = "Set-Cookie";
private static final String COOKIE = "Cookie";
private final SpiServiceManager mgr;
private final CompressionConfig compressionConfig;
private final ServiceManager mgr;
private final String path;
private final Map<String, String> pathParams;
private final Map<String, Object> attributes = new HashMap<>();
Expand All @@ -62,14 +59,12 @@ final class JdkContext implements Context {
private String characterEncoding;

JdkContext(
SpiServiceManager mgr,
CompressionConfig compressionConfig,
ServiceManager mgr,
HttpExchange exchange,
String path,
Map<String, String> pathParams,
Set<Role> roles) {
this.mgr = mgr;
this.compressionConfig = compressionConfig;
this.roles = roles;
this.exchange = exchange;
this.path = path;
Expand All @@ -78,13 +73,11 @@ final class JdkContext implements Context {

/** Create when no route matched. */
JdkContext(
SpiServiceManager mgr,
CompressionConfig compressionConfig,
ServiceManager mgr,
HttpExchange exchange,
String path,
Set<Role> roles) {
this.mgr = mgr;
this.compressionConfig = compressionConfig;
this.roles = roles;
this.exchange = exchange;
this.path = path;
Expand Down Expand Up @@ -327,11 +320,7 @@ public String method() {

@Override
public OutputStream outputStream() {
var out = mgr.createOutputStream(this);
if (compressionConfig.compressionEnabled()) {
return new CompressedOutputStream(compressionConfig, this, out);
}
return out;
return mgr.createOutputStream(this);
}

private Map<String, String> parseCookies() {
Expand Down
12 changes: 4 additions & 8 deletions avaje-jex/src/main/java/io/avaje/jex/core/RoutingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
final class RoutingHandler implements HttpHandler {

private final SpiRoutes routes;
private final SpiServiceManager mgr;
private final CompressionConfig compressionConfig;
private final ServiceManager mgr;
private final List<HttpFilter> filters;

RoutingHandler(SpiRoutes routes, SpiServiceManager mgr, CompressionConfig compressionConfig) {
RoutingHandler(SpiRoutes routes, ServiceManager mgr) {
this.mgr = mgr;
this.routes = routes;
this.filters = routes.filters();
this.compressionConfig = compressionConfig;
}

void waitForIdle(long maxSeconds) {
Expand All @@ -38,7 +36,7 @@ public void handle(HttpExchange exchange) {
final var route = routes.match(routeType, uri);

if (route == null) {
var ctx = new JdkContext(mgr, compressionConfig, exchange, uri, Set.of());
var ctx = new JdkContext(mgr, exchange, uri, Set.of());
handleException(
ctx,
new NotFoundException(
Expand All @@ -47,9 +45,7 @@ public void handle(HttpExchange exchange) {
route.inc();
try {
final Map<String, String> params = route.pathParams(uri);
JdkContext ctx =
new JdkContext(
mgr, compressionConfig, exchange, route.matchPath(), params, route.roles());
JdkContext ctx = new JdkContext(mgr, exchange, route.matchPath(), params, route.roles());
try {
ctx.setMode(Mode.BEFORE);
new BaseFilterChain(filters, route.handler(), ctx).proceed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
import io.avaje.jex.Context;
import io.avaje.jex.Jex;
import io.avaje.jex.Routing;
import io.avaje.jex.compression.CompressedOutputStream;
import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.core.json.JacksonJsonService;
import io.avaje.jex.core.json.JsonbJsonService;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

/** Core service methods available to Context implementations. */
final class SpiServiceManager {
final class ServiceManager {

private static final System.Logger log = System.getLogger("io.avaje.jex");
static final String UTF_8 = "UTF-8";
private static final String UTF_8 = "UTF-8";

private final HttpMethodMap methodMap = new HttpMethodMap();
private final CompressionConfig compressionConfig;
private final JsonService jsonService;
private final ExceptionManager exceptionHandler;
private final TemplateManager templateManager;
Expand All @@ -38,18 +40,20 @@ final class SpiServiceManager {
private final int bufferInitial;
private final long bufferMax;

static SpiServiceManager create(Jex jex) {
static ServiceManager create(Jex jex) {
return new Builder(jex).build();
}

SpiServiceManager(
ServiceManager(
CompressionConfig compressionConfig,
JsonService jsonService,
ExceptionManager manager,
TemplateManager templateManager,
String scheme,
String contextPath,
long bufferMax,
int bufferInitial) {
this.compressionConfig = compressionConfig;
this.jsonService = jsonService;
this.exceptionHandler = manager;
this.templateManager = templateManager;
Expand All @@ -60,7 +64,11 @@ static SpiServiceManager create(Jex jex) {
}

OutputStream createOutputStream(JdkContext jdkContext) {
return new BufferedOutStream(jdkContext, bufferInitial, bufferMax);
var out = new BufferedOutStream(jdkContext, bufferInitial, bufferMax);
if (compressionConfig.compressionEnabled()) {
return new CompressedOutputStream(compressionConfig, jdkContext, out);
}
return out;
}

<T> T fromJson(Class<T> type, InputStream is) {
Expand Down Expand Up @@ -100,7 +108,11 @@ void maybeClose(Object iterator) {
}

Routing.Type lookupRoutingType(String method) {
return methodMap.get(method);
try {
return Routing.Type.valueOf(method);
} catch (Exception e) {
return null;
}
}

void handleException(JdkContext ctx, Exception e) {
Expand Down Expand Up @@ -165,8 +177,9 @@ private static final class Builder {
this.jex = jex;
}

SpiServiceManager build() {
return new SpiServiceManager(
ServiceManager build() {
return new ServiceManager(
jex.config().compression(),
initJsonService(),
new ExceptionManager(jex.routing().errorHandlers()),
initTemplateMgr(),
Expand Down
16 changes: 9 additions & 7 deletions avaje-jex/src/test/java/io/avaje/jex/core/ContextUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;

class ContextUtilTest {

@Test
void parseCharset_defaults() {
assertThat(SpiServiceManager.parseCharset("")).isEqualTo(SpiServiceManager.UTF_8);
assertThat(SpiServiceManager.parseCharset("junk")).isEqualTo(SpiServiceManager.UTF_8);
assertThat(ServiceManager.parseCharset("")).isEqualTo(StandardCharsets.UTF_8.name());
assertThat(ServiceManager.parseCharset("junk")).isEqualTo(StandardCharsets.UTF_8.name());
}

@Test
void parseCharset_caseCheck() {
assertThat(SpiServiceManager.parseCharset("app/foo; charset=ME")).isEqualTo("ME");
assertThat(SpiServiceManager.parseCharset("app/foo;charset=ME")).isEqualTo("ME");
assertThat(SpiServiceManager.parseCharset("app/foo;charset = ME ")).isEqualTo("ME");
assertThat(SpiServiceManager.parseCharset("app/foo;charset = ME;")).isEqualTo("ME");
assertThat(SpiServiceManager.parseCharset("app/foo;charset = ME;other=junk")).isEqualTo("ME");
assertThat(ServiceManager.parseCharset("app/foo; charset=ME")).isEqualTo("ME");
assertThat(ServiceManager.parseCharset("app/foo;charset=ME")).isEqualTo("ME");
assertThat(ServiceManager.parseCharset("app/foo;charset = ME ")).isEqualTo("ME");
assertThat(ServiceManager.parseCharset("app/foo;charset = ME;")).isEqualTo("ME");
assertThat(ServiceManager.parseCharset("app/foo;charset = ME;other=junk")).isEqualTo("ME");
}
}
Loading