Skip to content

Commit 635688b

Browse files
committed
router: make router option a class
- remove enum
1 parent 9af5e36 commit 635688b

15 files changed

Lines changed: 225 additions & 84 deletions

File tree

jooby/src/main/java/io/jooby/Context.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,10 @@ Context responseWriter(
13901390

13911391
/**
13921392
* True if response headers are cleared on application error. If none set it uses the
1393-
* default/global value specified by {@link RouterOption#RESET_HEADERS_ON_ERROR}.
1393+
* default/global value specified by {@link RouterOptions#RESET_HEADERS_ON_ERROR}.
13941394
*
13951395
* @return True if response headers are cleared on application error. If none set it uses the
1396-
* default/global value specified by {@link RouterOption#RESET_HEADERS_ON_ERROR}.
1396+
* default/global value specified by {@link RouterOptions#RESET_HEADERS_ON_ERROR}.
13971397
*/
13981398
boolean getResetHeadersOnError();
13991399

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,11 @@ public Jooby() {
139139
}
140140

141141
@NonNull @Override
142-
public Set<RouterOption> getRouterOptions() {
142+
public RouterOptions getRouterOptions() {
143143
return router.getRouterOptions();
144144
}
145145

146-
@NonNull @Override
147-
public Jooby setRouterOptions(@NonNull RouterOption... options) {
146+
@NonNull public Jooby setRouterOptions(@NonNull RouterOptions options) {
148147
router.setRouterOptions(options);
149148
return this;
150149
}

jooby/src/main/java/io/jooby/Route.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.Serializable;
99
import java.lang.invoke.MethodHandle;
1010
import java.lang.invoke.MethodHandles;
11-
import java.lang.invoke.MethodType;
1211
import java.lang.reflect.Method;
1312
import java.util.ArrayList;
1413
import java.util.Arrays;
@@ -404,17 +403,25 @@ public Method toMethod() {
404403
/**
405404
* Convert to {@link MethodHandle}.
406405
*
406+
* @param lookup Lookup to use.
407407
* @return A {@link MethodHandle}.
408408
*/
409-
public MethodHandle toMethodHandle() {
410-
var lookup = MethodHandles.publicLookup();
411-
var methodType = MethodType.methodType(returnType, parameterTypes);
409+
public MethodHandle toMethodHandle(MethodHandles.Lookup lookup) {
412410
try {
413-
return lookup.findVirtual(declaringClass, name, methodType);
414-
} catch (NoSuchMethodException | IllegalAccessException e) {
411+
return lookup.unreflect(toMethod());
412+
} catch (IllegalAccessException e) {
415413
throw SneakyThrows.propagate(e);
416414
}
417415
}
416+
417+
/**
418+
* Convert to {@link MethodHandle} using a public lookup.
419+
*
420+
* @return A {@link MethodHandle}.
421+
*/
422+
public MethodHandle toMethodHandle() {
423+
return toMethodHandle(MethodHandles.publicLookup());
424+
}
418425
}
419426

420427
/** Favicon handler as a silent 404 error. */

jooby/src/main/java/io/jooby/Router.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Locale;
1919
import java.util.Map;
2020
import java.util.Optional;
21-
import java.util.Set;
2221
import java.util.concurrent.Executor;
2322
import java.util.concurrent.atomic.AtomicInteger;
2423
import java.util.function.BiConsumer;
@@ -845,15 +844,15 @@ default Object execute(@NonNull Context context) {
845844
*
846845
* @return Router options.
847846
*/
848-
@NonNull Set<RouterOption> getRouterOptions();
847+
@NonNull RouterOptions getRouterOptions();
849848

850849
/**
851850
* Set router options.
852851
*
853852
* @param options router options.
854853
* @return This router.
855854
*/
856-
@NonNull Router setRouterOptions(@NonNull RouterOption... options);
855+
@NonNull Router setRouterOptions(@NonNull RouterOptions options);
857856

858857
/**
859858
* Session store. Default use a cookie ID with a memory storage.

jooby/src/main/java/io/jooby/RouterOption.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby;
7+
8+
/**
9+
* Router options:
10+
*
11+
* <ul>
12+
* <li>ignoreCase: Indicates whenever routing algorithm does case-sensitive matching on an
13+
* incoming request path. Default is <code> false</code> (case sensitive).
14+
* <li>ignoreTrailingSlash: Indicates whenever a trailing slash is ignored on an incoming request
15+
* path.
16+
* <li>normalizeSlash: Normalize an incoming request path by removing consecutive <code>/</code>
17+
* (slashes).
18+
* <li>resetHeadersOnError: Indicates whenever response headers are clear/reset in case of
19+
* exception.
20+
* </ul>
21+
*
22+
* @author edgar
23+
* @since 2.4.0
24+
*/
25+
public class RouterOptions {
26+
/**
27+
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
28+
* Default is <code>case sensitive</code>.
29+
*/
30+
private boolean ignoreCase;
31+
32+
/** Indicates whenever a trailing slash is ignored on an incoming request path. */
33+
private boolean ignoreTrailingSlash;
34+
35+
/** Normalize an incoming request path by removing multiple slash sequences. */
36+
private boolean normalizeSlash;
37+
38+
/** Indicates whenever response headers are clear/reset in case of exception. */
39+
private boolean resetHeadersOnError;
40+
41+
/**
42+
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
43+
* Default is <code>false (case sensitive)</code>.
44+
*
45+
* @return True when case is ignored.
46+
*/
47+
public boolean isIgnoreCase() {
48+
return ignoreCase;
49+
}
50+
51+
/**
52+
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
53+
* Default is <code>false (case sensitive)</code>.
54+
*
55+
* @param ignoreCase True for case-insensitive.
56+
* @return This options.
57+
*/
58+
public RouterOptions setIgnoreCase(boolean ignoreCase) {
59+
this.ignoreCase = ignoreCase;
60+
return this;
61+
}
62+
63+
/**
64+
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
65+
* Default is <code>false (case sensitive)</code>.
66+
*
67+
* @param ignoreCase True for case-insensitive.
68+
* @return This options.
69+
*/
70+
public RouterOptions ignoreCase(boolean ignoreCase) {
71+
return setIgnoreCase(ignoreCase);
72+
}
73+
74+
/**
75+
* Indicates whenever a trailing slash is ignored on an incoming request path.
76+
*
77+
* @return Indicates whenever a trailing slash is ignored on an incoming request path.
78+
*/
79+
public boolean isIgnoreTrailingSlash() {
80+
return ignoreTrailingSlash;
81+
}
82+
83+
/**
84+
* Set whenever a trailing slash is ignored on an incoming request path.
85+
*
86+
* @param ignoreTrailingSlash whenever a trailing slash is ignored on an incoming request path.
87+
* @return This options.
88+
*/
89+
public RouterOptions setIgnoreTrailingSlash(boolean ignoreTrailingSlash) {
90+
this.ignoreTrailingSlash = ignoreTrailingSlash;
91+
return this;
92+
}
93+
94+
/**
95+
* Set whenever a trailing slash is ignored on an incoming request path.
96+
*
97+
* @param ignoreTrailingSlash whenever a trailing slash is ignored on an incoming request path.
98+
* @return This options.
99+
*/
100+
public RouterOptions ignoreTrailingSlash(boolean ignoreTrailingSlash) {
101+
return setIgnoreTrailingSlash(ignoreTrailingSlash);
102+
}
103+
104+
/**
105+
* Normalize an incoming request path by removing multiple slash sequences.
106+
*
107+
* @return Normalize an incoming request path by removing multiple slash sequences.
108+
*/
109+
public boolean isNormalizeSlash() {
110+
return normalizeSlash;
111+
}
112+
113+
/**
114+
* Set whenever normalize an incoming request path by removing multiple slash sequences.
115+
*
116+
* @param normalizeSlash True for normalize a path.
117+
* @return This options.
118+
*/
119+
public RouterOptions setNormalizeSlash(boolean normalizeSlash) {
120+
this.normalizeSlash = normalizeSlash;
121+
return this;
122+
}
123+
124+
/**
125+
* Set whenever normalize an incoming request path by removing multiple slash sequences.
126+
*
127+
* @param normalizeSlash True for normalize a path.
128+
* @return This options.
129+
*/
130+
public RouterOptions normalizeSlash(boolean normalizeSlash) {
131+
return setNormalizeSlash(normalizeSlash);
132+
}
133+
134+
/**
135+
* Indicates whenever response headers are clear/reset in case of exception.
136+
*
137+
* @return Indicates whenever response headers are clear/reset in case of exception.
138+
*/
139+
public boolean isResetHeadersOnError() {
140+
return resetHeadersOnError;
141+
}
142+
143+
/**
144+
* Set whenever response headers are clear/reset in case of exception.
145+
*
146+
* @param resetHeadersOnError whenever response headers are clear/reset in case of exception.
147+
* @return This options.
148+
*/
149+
public RouterOptions setResetHeadersOnError(boolean resetHeadersOnError) {
150+
this.resetHeadersOnError = resetHeadersOnError;
151+
return this;
152+
}
153+
154+
/**
155+
* Set whenever response headers are clear/reset in case of exception.
156+
*
157+
* @param resetHeadersOnError whenever response headers are clear/reset in case of exception.
158+
* @return This options.
159+
*/
160+
public RouterOptions resetHeaderOnError(boolean resetHeadersOnError) {
161+
return setResetHeadersOnError(resetHeadersOnError);
162+
}
163+
164+
/**
165+
* Case-sensitive with reset headers on error enabled.
166+
*
167+
* @return Default options.
168+
*/
169+
public static RouterOptions defaults() {
170+
return new RouterOptions().resetHeaderOnError(true);
171+
}
172+
173+
/**
174+
* Case-inSensitive with reset headers on error enabled.
175+
*
176+
* @return Default options.
177+
*/
178+
public static RouterOptions caseInsensitive() {
179+
return new RouterOptions().ignoreCase(true).resetHeaderOnError(true);
180+
}
181+
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.nio.file.Paths;
1515
import java.util.ArrayList;
1616
import java.util.Collections;
17-
import java.util.EnumSet;
1817
import java.util.HashMap;
1918
import java.util.LinkedHashMap;
2019
import java.util.LinkedList;
@@ -23,7 +22,6 @@
2322
import java.util.Map;
2423
import java.util.NoSuchElementException;
2524
import java.util.Optional;
26-
import java.util.Set;
2725
import java.util.concurrent.ConcurrentHashMap;
2826
import java.util.concurrent.Executor;
2927
import java.util.function.BiConsumer;
@@ -160,7 +158,7 @@ public Stack executor(Executor executor) {
160158

161159
private ContextInitializer postDispatchInitializer;
162160

163-
private Set<RouterOption> routerOptions = EnumSet.of(RouterOption.RESET_HEADERS_ON_ERROR);
161+
private RouterOptions routerOptions = RouterOptions.defaults();
164162

165163
private boolean trustProxy;
166164

@@ -199,19 +197,19 @@ public Map<String, Object> getAttributes() {
199197
}
200198

201199
@NonNull @Override
202-
public Set<RouterOption> getRouterOptions() {
200+
public RouterOptions getRouterOptions() {
203201
return routerOptions;
204202
}
205203

206204
@NonNull @Override
207-
public Router setRouterOptions(@NonNull RouterOption... options) {
208-
Stream.of(options).forEach(routerOptions::add);
205+
public Router setRouterOptions(@NonNull RouterOptions options) {
206+
this.routerOptions = options;
209207
return this;
210208
}
211209

212210
@NonNull @Override
213211
public Router setContextPath(@NonNull String basePath) {
214-
if (routes.size() > 0) {
212+
if (!routes.isEmpty()) {
215213
throw new IllegalStateException("Base path must be set before adding any routes.");
216214
}
217215
this.basePath = Router.leadingSlash(basePath);
@@ -499,7 +497,7 @@ private Route newRoute(
499497
String finalPattern =
500498
basePath == null ? safePattern : new PathBuilder(basePath, safePattern).toString();
501499

502-
if (routerOptions.contains(RouterOption.IGNORE_CASE)) {
500+
if (routerOptions.isIgnoreCase()) {
503501
finalPattern = finalPattern.toLowerCase();
504502
}
505503

@@ -604,13 +602,13 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
604602
((Chi) chi).setEncoder(encoder);
605603

606604
/** router options: */
607-
if (routerOptions.contains(RouterOption.IGNORE_CASE)) {
605+
if (routerOptions.isIgnoreCase()) {
608606
chi = new RouteTreeLowerCasePath(chi);
609607
}
610-
if (routerOptions.contains(RouterOption.IGNORE_TRAILING_SLASH)) {
608+
if (routerOptions.isIgnoreTrailingSlash()) {
611609
chi = new RouteTreeIgnoreTrailingSlash(chi);
612610
}
613-
if (routerOptions.contains(RouterOption.NORMALIZE_SLASH)) {
611+
if (routerOptions.isNormalizeSlash()) {
614612
chi = new RouteTreeNormPath(chi);
615613
}
616614

0 commit comments

Comments
 (0)