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

io.github.kamilszewc.simplerouter.Route Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
package io.github.kamilszewc.simplerouter;

import java.util.HashMap;
import java.util.Map;

public class Route {

    private final SimpleRouter.Method method;
    private final String path;
    private final MethodHandler methodHandler;

    public Route(SimpleRouter.Method method, String path, MethodHandler methodHandler) {
        this.method = method;
        this.path = path;
        this.methodHandler = methodHandler;
    }

    public String getPath() {
        return path;
    }

    public SimpleRouter.Method getMethod() {
        return method;
    }

    public Object callMethod(RoutingContext routingContext) {
        return methodHandler.handler(routingContext);
    }

    public RoutingContext checkRouting(Request request) throws NoRoutingException {
        // First check if method fits
        if (method != SimpleRouter.Method.ANY) {
            var requestMethod = request.getMethod();
            if (method != requestMethod) {
                throw new NoRoutingException();
            }
        }

        // Get request path
        var requestPath = request.getPath();

        // Extract path elements
        var requestPathElements = requestPath.split("/");
        var pathElements = path.split("/");

        Map pathVariables = new HashMap<>();

        int i=0;
        for (i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy