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
64 changes: 21 additions & 43 deletions avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ final class DefaultRouting implements Routing {
private final Deque<String> pathDeque = new ArrayDeque<>();
private final Map<Class<?>, ExceptionHandler<?>> exceptionHandlers = new HashMap<>();

/**
* Last entry that we can add permitted roles to.
*/
private Entry lastEntry;

@Override
public List<Routing.Entry> handlers() {
return handlers;
Expand Down Expand Up @@ -76,74 +71,60 @@ public Routing path(String path, Group group) {
return this;
}

@Override
public Routing withRoles(Set<Role> permittedRoles) {
if (lastEntry == null) {
throw new IllegalStateException("Must call withRoles() after adding a route");
}
lastEntry.withRoles(permittedRoles);
return this;
}

@Override
public Routing withRoles(Role... permittedRoles) {
return withRoles(Set.of(permittedRoles));
}

private void add(Type verb, String path, ExchangeHandler handler) {
lastEntry = new Entry(verb, path(path), handler);
handlers.add(lastEntry);
private void add(Type verb, String path, ExchangeHandler handler, Role... roles) {
var entry = new Entry(verb, path(path), handler, Set.of(roles));
handlers.add(entry);
}

// ********************************************************************************************
// HTTP verbs
// ********************************************************************************************

@Override
public Routing get(String path, ExchangeHandler handler) {
add(Type.GET, path, handler);
public Routing get(String path, ExchangeHandler handler, Role... roles) {
add(Type.GET, path, handler, roles);
return this;
}

@Override
public Routing post(String path, ExchangeHandler handler) {
add(Type.POST, path, handler);
public Routing post(String path, ExchangeHandler handler, Role... roles) {
add(Type.POST, path, handler, roles);
return this;
}

@Override
public Routing put(String path, ExchangeHandler handler) {
add(Type.PUT, path, handler);
public Routing put(String path, ExchangeHandler handler, Role... roles) {
add(Type.PUT, path, handler, roles);
return this;
}

@Override
public Routing patch(String path, ExchangeHandler handler) {
add(Type.PATCH, path, handler);
public Routing patch(String path, ExchangeHandler handler, Role... roles) {
add(Type.PATCH, path, handler, roles);
return this;
}

@Override
public Routing delete(String path, ExchangeHandler handler) {
add(Type.DELETE, path, handler);
public Routing delete(String path, ExchangeHandler handler, Role... roles) {
add(Type.DELETE, path, handler, roles);
return this;
}

@Override
public Routing head(String path, ExchangeHandler handler) {
add(Type.HEAD, path, handler);
public Routing head(String path, ExchangeHandler handler, Role... roles) {
add(Type.HEAD, path, handler, roles);
return this;
}

@Override
public Routing trace(String path, ExchangeHandler handler) {
add(Type.TRACE, path, handler);
public Routing trace(String path, ExchangeHandler handler, Role... roles) {
add(Type.TRACE, path, handler, roles);
return this;
}

@Override
public Routing options(String path, ExchangeHandler handler) {
add(Type.OPTIONS, path, handler);
public Routing options(String path, ExchangeHandler handler, Role... roles) {
add(Type.OPTIONS, path, handler, roles);
return this;
}

Expand All @@ -162,15 +143,12 @@ private static final class Entry implements Routing.Entry {
private final Type type;
private final String path;
private final ExchangeHandler handler;
private Set<Role> roles = Collections.emptySet();
private final Set<Role> roles;

Entry(Type type, String path, ExchangeHandler handler) {
Entry(Type type, String path, ExchangeHandler handler, Set<Role> roles) {
this.type = type;
this.path = path;
this.handler = handler;
}

void withRoles(Set<Role> roles) {
this.roles = roles;
}

Expand Down
82 changes: 62 additions & 20 deletions avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Consumer;

import io.avaje.inject.BeanScope;
import io.avaje.jex.security.Role;
import io.avaje.jex.spi.JexPlugin;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;
Expand Down Expand Up @@ -65,49 +66,91 @@ static Jex create() {
*/
Routing routing();

/** Add a GET handler. */
default Jex get(String path, ExchangeHandler handler) {
routing().get(path, handler);
/**
* Adds a GET handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when a GET request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex get(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
return this;
}

/** Add a POST handler. */
default Jex post(String path, ExchangeHandler handler) {
routing().get(path, handler);
/**
* Adds a POST handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when a POST request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex post(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
return this;
}

/** Add a PUT handler. */
default Jex put(String path, ExchangeHandler handler) {
routing().get(path, handler);
/**
* Adds a PUT handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when a PUT request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex put(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
return this;
}

/** Add a PATCH handler. */
default Jex patch(String path, ExchangeHandler handler) {
routing().get(path, handler);
/**
* Adds a PATCH handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when a PATCH request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex patch(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
return this;
}

/**
* Adds a DELETE handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when a DELETE request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex delete(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
return this;
}

/** Add a DELETE handler. */
default Jex delete(String path, ExchangeHandler handler) {
routing().get(path, handler);
/**
* Adds an OPTIONS handler to the route configuration.
*
* @param path The path pattern to match the request URI.
* @param handler The handler to invoke when an OPTIONS request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex options(String path, ExchangeHandler handler, Role... roles) {
routing().options(path, handler, roles);
return this;
}

/** Add a filter for all requests. */
/** Add a filter for all matched requests. */
default Jex filter(HttpFilter handler) {
routing().filter(handler);
return this;
}

/** Add a pre-processing filter for all requests. */
/** Add a pre-processing filter for all matched requests. */
default Jex before(Consumer<Context> handler) {
routing().before(handler);
return this;
}

/** Add a post-processing filter for all requests. */
/** Add a post-processing filter for all matched requests. */
default Jex after(Consumer<Context> handler) {
routing().after(handler);
return this;
Expand All @@ -121,8 +164,7 @@ default Jex after(Consumer<Context> handler) {
* @param handler the error handler
* @param <T> exception type
*/
default <T extends Exception> Jex error(
Class<T> exceptionClass, ExceptionHandler<T> handler) {
default <T extends Exception> Jex error(Class<T> exceptionClass, ExceptionHandler<T> handler) {
routing().error(exceptionClass, handler);
return this;
}
Expand Down
Loading
Loading