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

com.vtence.molecule.routing.DynamicRoutes Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package com.vtence.molecule.routing;

import com.vtence.molecule.Application;
import com.vtence.molecule.http.HttpMethod;
import com.vtence.molecule.lib.matchers.Matcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.vtence.molecule.http.HttpMethod.*;
import static com.vtence.molecule.lib.matchers.Matchers.anyOf;
import static com.vtence.molecule.lib.matchers.Matchers.anything;
import static com.vtence.molecule.lib.matchers.Matchers.equalTo;

public class DynamicRoutes implements RouteBuilder {

    private final Collection routes = new ArrayList<>();

    public void build(RouteSet routeSet) {
        for (Definition definition : this.routes) {
            routeSet.add(definition.toRoute());
        }
    }

    public ViaClause map(String path) {
        return openRoute(new DynamicPath(path));
    }

    public ViaClause map(Matcher path) {
        return openRoute(path);
    }

    public ToClause get(String path) {
        return get(new DynamicPath(path));
    }

    public ToClause get(Matcher path) {
        return map(path).via(GET);
    }

    public ToClause post(String path) {
        return post(new DynamicPath(path));
    }

    public ToClause post(Matcher path) {
        return map(path).via(POST);
    }

    public ToClause put(String path) {
        return put(new DynamicPath(path));
    }

    public ToClause put(Matcher path) {
        return map(path).via(PUT);
    }

    public ToClause delete(String path) {
        return delete(new DynamicPath(path));
    }

    public ToClause delete(Matcher path) {
        return map(path).via(DELETE);
    }

    public ToClause patch(String path) {
        return patch(new DynamicPath(path));
    }

    public ToClause patch(Matcher path) {
        return map(path).via(PATCH);
    }

    public ToClause head(String path) {
        return head(new DynamicPath(path));
    }

    public ToClause head(Matcher path) {
        return map(path).via(HEAD);
    }

    public ToClause options(String path) {
        return options(new DynamicPath(path));
    }

    public ToClause options(Matcher path) {
        return map(path).via(OPTIONS);
    }

    private Definition openRoute(Matcher path) {
        Definition definition = new Definition(path);
        routes.add(definition);
        return definition;
    }

    private static class Definition implements ViaClause {

        private final Matcher path;

        private Matcher method = anything();
        private Application app;

        public Definition(Matcher path) {
            this.path = path;
        }

        public Definition via(HttpMethod... methods) {
            return via(oneOf(methods));
        }

        public Definition via(Matcher method) {
            this.method = method;
            return this;
        }

        public Definition to(Application application) {
            this.app = application;
            return this;
        }

        public DynamicRoute toRoute() {
            return new DynamicRoute(path, method, app);
        }

        private Matcher oneOf(HttpMethod... methods) {
            List> matchMethods = new ArrayList<>();
            for (HttpMethod httpMethod : methods) {
                matchMethods.add(equalTo(httpMethod));
            }
            return anyOf(matchMethods);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy