diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandler.java index f6c1a58..aa9c4da 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandler.java @@ -6,16 +6,40 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +/** + * Abstract base class for handling requests of a specific type. + * + * @param the type of request this handler processes, must extend {@link Request} + */ public abstract class CommandHandler { private final List checks = new ArrayList<>(); + /** + * Adds a handler check to be performed before request execution. + * + *

The {@code execute} Method is only invoked, if all specified checks passed. + * + * @param check the {@link HandlerCheck} to add + */ protected final void addCheck(HandlerCheck check) { checks.add(check); } + /** + * Returns an unmodifiable list of all registered handler checks. + * + * @return an unmodifiable list of {@link HandlerCheck} objects + */ public final List getChecks() { return Collections.unmodifiableList(checks); } + /** + * Executes the request. + * + *

Subclasses should override this method to implement their specific request handling logic. + * + * @param request the request to execute, of type T + */ public void execute(T request) {} } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandlerExecutor.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandlerExecutor.java index 7526c1b..6f097c9 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandlerExecutor.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandHandlerExecutor.java @@ -6,13 +6,33 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher; import java.util.Optional; +/** + * Invokes execution of the {@link CommandHandler} if all defined pre-execution checks pass. + * + *

If any check fails, the response returned by the check is dispatched immediately and execution + * is aborted. Otherwise, the handler will invoke execution normally. + */ public class CommandHandlerExecutor { private final ResponseDispatcher responseDispatcher; + /** + * Creates a new CommandHandlerExecutor to execute pre-execution checks and invoke the {@link + * CommandHandler} if all checks pass. + * + * @param responseDispatcher to dispatch the response with if an check fails + */ public CommandHandlerExecutor(ResponseDispatcher responseDispatcher) { this.responseDispatcher = responseDispatcher; } + /** + * Executes a command handler if all defined pre-execution checks pass. + * + * @param handler the command handler containing checks and execution logic + * @param request the incoming request to validate and process + * @param the type of request being processed + * @throws NullPointerException if handler or request is null + */ public void execute(CommandHandler handler, Request request) { for (HandlerCheck check : handler.getChecks()) { Optional result = check.check(request); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandRouter.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandRouter.java index 366db11..5b177ed 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandRouter.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/CommandRouter.java @@ -4,18 +4,46 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request; import java.util.HashMap; import java.util.Map; +/** Routes incoming requests to their corresponding {@link CommandHandler} implementations. */ public class CommandRouter { private final Map, CommandHandler> handlers = new HashMap<>(); private final CommandHandlerExecutor handlerExecutor; + /** + * Creates a new CommandRouter with the specified handler executor. + * + * @param commandHandlerExecutor to delegate execution of checks and invocation of {@link + * CommandHandler} to + */ public CommandRouter(CommandHandlerExecutor commandHandlerExecutor) { this.handlerExecutor = commandHandlerExecutor; } + /** + * Registers a {@link CommandHandler} for a specific request type. + * + *

This method establishes a type-safe mapping between a request class and its handler. + * + *

Only one handler can be registered per request type; subsequent registrations will + * overwrite previous ones. + * + * @param the type of request handled by the provided handler + * @param request the request class to associate with the given command handler + * @param handler the command handler for the given request type + */ public void register(Class request, CommandHandler handler) { handlers.put(request, handler); } + /** + * Executes the appropriate handler for the given request. + * + *

Looks up the handler registered for the request's class and executes it through the + * configured {@link CommandHandlerExecutor}. + * + * @param request the request to be executed + * @throws UnknownRequestException if no handler is registered for the request type + */ // Safe, because during registration, it's ensured that the provided CommandHandler only // receives requests it can handle. @SuppressWarnings("unchecked") diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/checks/HandlerCheck.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/checks/HandlerCheck.java index cabe7f3..5db599f 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/checks/HandlerCheck.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/command/execution/checks/HandlerCheck.java @@ -1,10 +1,23 @@ package ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.checks; +import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request; +import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response; import java.util.Optional; +/** + * Functional interface used by pre-execution checks. Executed before the execute method of the + * {@link CommandHandler} is called + */ @FunctionalInterface public interface HandlerCheck { + /** + * Checks that the requirements outlined by this HandlerCheck are fulfilled. + * + * @param request to execute the check with + * @return Optional containing no value if the check was successful. And a {@link ErrorResponse} + * if the check failed, the response will be dispatched to the client. + */ Optional check(Request request); }