Skip to content

Commit

Permalink
Change Handler Signatures to handle Checked Exceptions (#119)
Browse files Browse the repository at this point in the history
* Handlers can now handle checked exceptions

* handle checked exceptions
  • Loading branch information
SentryMan authored Dec 2, 2024
1 parent e595450 commit b25367f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
16 changes: 7 additions & 9 deletions avaje-jex-htmx/src/main/java/io/avaje/jex/htmx/DHxHandler.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.avaje.jex.htmx;

import static io.avaje.jex.htmx.HxHeaders.HX_REQUEST;
import static io.avaje.jex.htmx.HxHeaders.HX_TARGET;
import static io.avaje.jex.htmx.HxHeaders.HX_TRIGGER;
import static io.avaje.jex.htmx.HxHeaders.HX_TRIGGER_NAME;

import io.avaje.jex.Context;
import io.avaje.jex.ExchangeHandler;

import java.io.IOException;

import static io.avaje.jex.htmx.HxHeaders.*;

final class DHxHandler implements ExchangeHandler {

private final ExchangeHandler delegate;
Expand All @@ -22,17 +23,14 @@ final class DHxHandler implements ExchangeHandler {
}

@Override
public void handle(Context ctx) throws IOException {
public void handle(Context ctx) throws Exception {
if (ctx.header(HX_REQUEST) != null && matched(ctx)) {
delegate.handle(ctx);
}
}

private boolean matched(Context ctx) {
if (target != null && notMatched(ctx.header(HX_TARGET), target)) {
return false;
}
if (trigger != null && notMatched(ctx.header(HX_TRIGGER), trigger)) {
if ((target != null && notMatched(ctx.header(HX_TARGET), target)) || (trigger != null && notMatched(ctx.header(HX_TRIGGER), trigger))) {
return false;
}
return triggerName == null || !notMatched(ctx.header(HX_TRIGGER_NAME), triggerName);
Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/ExchangeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public interface ExchangeHandler {
* @param ctx The context object containing the request and response details.
* @throws IOException if an I/O error occurs during request processing or response generation.
*/
void handle(Context ctx) throws IOException;
void handle(Context ctx) throws Exception;
}
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/HttpFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface HttpFilter {
* @param ctx the {@code Context} of the current request
* @param chain the {@code FilterChain} which allows the next filter to be invoked
*/
void filter(Context ctx, FilterChain chain) throws IOException;
void filter(Context ctx, FilterChain chain) throws Exception;

/**
* Filter chain that contains all subsequent filters that are configured, as well as the final
Expand All @@ -54,6 +54,6 @@ interface FilterChain {
*
* @throws IOException if an I/O error occurs
*/
void proceed() throws IOException;
void proceed() throws Exception;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.avaje.jex.core;

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;

Expand All @@ -21,7 +20,7 @@ final class BaseFilterChain implements FilterChain {
}

@Override
public void proceed() throws IOException {
public void proceed() throws Exception {
if (!iter.hasNext()) {
handler.handle(ctx);
ctx.setMode(Mode.AFTER);
Expand Down
12 changes: 7 additions & 5 deletions avaje-jex/src/main/java/io/avaje/jex/core/RoutingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.sun.net.httpserver.HttpHandler;

import io.avaje.jex.HttpFilter;
import io.avaje.jex.Routing;
import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.http.NotFoundException;
import io.avaje.jex.routes.SpiRoutes;
Expand All @@ -34,13 +33,16 @@ void waitForIdle(long maxSeconds) {

@Override
public void handle(HttpExchange exchange) {
final String uri = exchange.getRequestURI().getPath();
final Routing.Type routeType = mgr.lookupRoutingType(exchange.getRequestMethod());
final SpiRoutes.Entry route = routes.match(routeType, uri);
final var uri = exchange.getRequestURI().getPath();
final var routeType = mgr.lookupRoutingType(exchange.getRequestMethod());
final var route = routes.match(routeType, uri);

if (route == null) {
var ctx = new JdkContext(mgr, compressionConfig, exchange, uri, Set.of());
handleException(ctx, new NotFoundException("uri: " + uri));
handleException(
ctx,
new NotFoundException(
"No route matching http method %s, with path %s".formatted(routeType.name(), uri)));
} else {
route.inc();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import io.avaje.jex.Context;
import io.avaje.jex.ExchangeHandler;

import java.io.IOException;

final class MultiHandler implements ExchangeHandler {

private final ExchangeHandler[] handlers;
Expand All @@ -14,7 +12,7 @@ final class MultiHandler implements ExchangeHandler {
}

@Override
public void handle(Context ctx) throws IOException {
public void handle(Context ctx) throws Exception {
for (ExchangeHandler handler : handlers) {
handler.handle(ctx);
if (ctx.responseSent()) {
Expand Down

0 comments on commit b25367f

Please sign in to comment.