All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.katharsis.dispatcher.registry.ControllerRegistry Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.katharsis.dispatcher.registry;

import io.katharsis.dispatcher.controller.BaseController;
import io.katharsis.request.path.JsonPath;
import io.katharsis.request.path.PathBuilder;

import java.util.LinkedList;
import java.util.List;

/**
 * Stores a list of controllers which are used to process the incoming requests.
 *
 * @see io.katharsis.dispatcher.RequestDispatcher
 */
public class ControllerRegistry {

    private final List controllers = new LinkedList<>();

    public ControllerRegistry(List baseControllers) {
        if (baseControllers != null) {
            controllers.addAll(baseControllers);
        }
    }

    /**
     * Adds Katharsis controller to the registry. Should be called at initialization time.
     *
     * @param controller a controller to be added
     */
    public void addController(BaseController controller) {
        controllers.add(controller);
    }

    /**
     * Iterate over all registered controllers to get the first suitable one.
     * @param jsonPath built JsonPath object mad from request path
     * @param requestType type of a HTTP request
     * @return suitable controller
     */
    public BaseController getController(JsonPath jsonPath, String requestType) {
        for (BaseController controller : controllers) {
            if (controller.isAcceptable(jsonPath, requestType)) {
                return controller;
            }
        }
        throw new MethodNotFoundException(PathBuilder.buildPath(jsonPath), requestType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy