|
29 | 29 | import java.util.List; |
30 | 30 | import java.util.Map; |
31 | 31 | import java.util.Optional; |
| 32 | +import java.util.Set; |
32 | 33 | import java.util.stream.Collectors; |
33 | 34 |
|
34 | 35 | import org.jooby.internal.AssetProxy; |
|
41 | 42 | import com.google.common.collect.ImmutableList; |
42 | 43 | import com.google.common.collect.ImmutableMap; |
43 | 44 | import com.google.common.collect.Lists; |
| 45 | +import com.google.inject.Key; |
| 46 | +import com.google.inject.TypeLiteral; |
44 | 47 |
|
45 | 48 | /** |
46 | 49 | * Routes are a key concept in Jooby. Routes are executed in the same order they are defined |
@@ -927,6 +930,26 @@ public List<String> vars() { |
927 | 930 | return cpattern.vars(); |
928 | 931 | } |
929 | 932 |
|
| 933 | + /** |
| 934 | + * Recreate a route path and apply the given variables. |
| 935 | + * |
| 936 | + * @param vars Path variables. |
| 937 | + * @return A route pattern. |
| 938 | + */ |
| 939 | + public String reverse(final Map<String, Object> vars) { |
| 940 | + return cpattern.reverse(vars); |
| 941 | + } |
| 942 | + |
| 943 | + /** |
| 944 | + * Recreate a route path and apply the given variables. |
| 945 | + * |
| 946 | + * @param values Path variable values. |
| 947 | + * @return A route pattern. |
| 948 | + */ |
| 949 | + public String reverse(final Object... values) { |
| 950 | + return cpattern.reverse(values); |
| 951 | + } |
| 952 | + |
930 | 953 | /** |
931 | 954 | * Set route attribute. |
932 | 955 | * |
@@ -1032,7 +1055,7 @@ public String name() { |
1032 | 1055 | */ |
1033 | 1056 | public Definition name(final String name) { |
1034 | 1057 | checkArgument(!Strings.isNullOrEmpty(name), "A route's name is required."); |
1035 | | - this.name = RoutePattern.normalize(name); |
| 1058 | + this.name = normalize(name); |
1036 | 1059 | return this; |
1037 | 1060 | } |
1038 | 1061 |
|
@@ -1705,7 +1728,8 @@ public void send(final Result result) throws Exception { |
1705 | 1728 | * The goal of the <code>complete</code> handler is to probably cleanup request object and log |
1706 | 1729 | * responses. |
1707 | 1730 | * |
1708 | | - * Please note that the <code>complete</code> handler is just syntax sugar for {@link Route.Filter}. |
| 1731 | + * Please note that the <code>complete</code> handler is just syntax sugar for |
| 1732 | + * {@link Route.Filter}. |
1709 | 1733 | * For example, the <code>complete</code> handler was implemented as: |
1710 | 1734 | * |
1711 | 1735 | * <pre>{@code |
@@ -1845,6 +1869,10 @@ default void next(final Request req, final Response rsp) throws Throwable { |
1845 | 1869 |
|
1846 | 1870 | } |
1847 | 1871 |
|
| 1872 | + /** Renderer key. */ |
| 1873 | + Key<Set<Route.Definition>> KEY = Key.get(new TypeLiteral<Set<Route.Definition>>() { |
| 1874 | + }); |
| 1875 | + |
1848 | 1876 | String GET = "GET"; |
1849 | 1877 |
|
1850 | 1878 | String POST = "POST"; |
@@ -1957,4 +1985,13 @@ default String attr(final String name) { |
1957 | 1985 | return attributes().get(name); |
1958 | 1986 | } |
1959 | 1987 |
|
| 1988 | + /** |
| 1989 | + * Normalize a path by removing double or trailing slashes. |
| 1990 | + * |
| 1991 | + * @param path A path to normalize. |
| 1992 | + * @return A normalized path. |
| 1993 | + */ |
| 1994 | + static String normalize(final String path) { |
| 1995 | + return RoutePattern.normalize(path); |
| 1996 | + } |
1960 | 1997 | } |
0 commit comments