Skip to content

Commit 2287b82

Browse files
tsegismontgaol
andauthored
[Issue-1506] Provide a method to get sub router if current route acts as a sub router (#2512) (#2859)
Signed-off-by: Lin Gao <[email protected]> Co-authored-by: Lin Gao <[email protected]>
1 parent 21f5abf commit 2287b82

4 files changed

Lines changed: 132 additions & 30 deletions

File tree

vertx-web/src/main/java/io/vertx/ext/web/Route.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ public interface Route {
162162
@Fluent
163163
Route subRouter(Router subRouter);
164164

165+
/**
166+
* @return the sub router if this route acts as a sub router, <code>null</code> if it does not.
167+
*/
168+
@Nullable
169+
Router getSubRouter();
170+
165171
/**
166172
* Specify a blocking request handler for the route.
167173
* This method works just like {@link #handler(Handler)} excepted that it will run the blocking handler on a worker thread

vertx-web/src/main/java/io/vertx/ext/web/impl/RouteImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public synchronized Route subRouter(Router subRouter) {
183183
validateMount(subRouter);
184184

185185
// mark the route as exclusive from now on
186-
this.state = state.setExclusive(true);
186+
this.state = state.setExclusive(true).setSubRouter(subRouter);
187187
return this;
188188
}
189189

@@ -426,5 +426,11 @@ private void validateMount(Router router) {
426426
}
427427
}
428428
}
429+
430+
@Override
431+
public Router getSubRouter() {
432+
return this.state.getSubRouter();
433+
}
434+
429435
}
430436

vertx-web/src/main/java/io/vertx/ext/web/impl/RouteState.java

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.vertx.core.net.HostAndPort;
2424
import io.vertx.core.net.impl.URIDecoder;
2525
import io.vertx.ext.web.MIMEHeader;
26+
import io.vertx.ext.web.Router;
2627
import io.vertx.ext.web.RoutingContext;
2728
import io.vertx.ext.web.handler.*;
2829

@@ -101,8 +102,9 @@ private static Priority weight(Handler<RoutingContext> handler) {
101102
private final boolean pathEndsWithSlash;
102103
private final boolean exclusive;
103104
private final boolean exactPath;
105+
private final Router subRouter;
104106

105-
private RouteState(RouteImpl route, Map<String, Object> metadata, String path, String name, int order, boolean enabled, Set<HttpMethod> methods, Set<MIMEHeader> consumes, boolean emptyBodyPermittedWithConsumes, Set<MIMEHeader> produces, List<Handler<RoutingContext>> contextHandlers, List<Handler<RoutingContext>> failureHandlers, boolean added, Pattern pattern, List<String> groups, boolean useNormalizedPath, Set<String> namedGroupsInRegex, Pattern virtualHostPattern, boolean pathEndsWithSlash, boolean exclusive, boolean exactPath) {
107+
private RouteState(RouteImpl route, Map<String, Object> metadata, String path, String name, int order, boolean enabled, Set<HttpMethod> methods, Set<MIMEHeader> consumes, boolean emptyBodyPermittedWithConsumes, Set<MIMEHeader> produces, List<Handler<RoutingContext>> contextHandlers, List<Handler<RoutingContext>> failureHandlers, boolean added, Pattern pattern, List<String> groups, boolean useNormalizedPath, Set<String> namedGroupsInRegex, Pattern virtualHostPattern, boolean pathEndsWithSlash, boolean exclusive, boolean exactPath, Router subRouter) {
106108
this.route = route;
107109
this.metadata = metadata;
108110
this.path = path;
@@ -124,6 +126,7 @@ private RouteState(RouteImpl route, Map<String, Object> metadata, String path, S
124126
this.pathEndsWithSlash = pathEndsWithSlash;
125127
this.exclusive = exclusive;
126128
this.exactPath = exactPath;
129+
this.subRouter = subRouter;
127130
}
128131

129132
RouteState(RouteImpl route, int order) {
@@ -148,7 +151,8 @@ private RouteState(RouteImpl route, Map<String, Object> metadata, String path, S
148151
null,
149152
false,
150153
false,
151-
true);
154+
true,
155+
null);
152156
}
153157

154158
public RouteImpl getRoute() {
@@ -188,7 +192,8 @@ public RouteState putMetadata(String key, Object value) {
188192
this.virtualHostPattern,
189193
this.pathEndsWithSlash,
190194
this.exclusive,
191-
this.exactPath);
195+
this.exactPath,
196+
this.subRouter);
192197
}
193198

194199
public Map<String, Object> getMetadata() {
@@ -221,7 +226,8 @@ RouteState setPath(String path) {
221226
this.virtualHostPattern,
222227
this.pathEndsWithSlash,
223228
this.exclusive,
224-
this.exactPath);
229+
this.exactPath,
230+
this.subRouter);
225231
}
226232

227233
public int getOrder() {
@@ -250,7 +256,8 @@ RouteState setOrder(int order) {
250256
this.virtualHostPattern,
251257
this.pathEndsWithSlash,
252258
this.exclusive,
253-
this.exactPath);
259+
this.exactPath,
260+
this.subRouter);
254261
}
255262

256263
public boolean isEnabled() {
@@ -279,7 +286,8 @@ RouteState setEnabled(boolean enabled) {
279286
this.virtualHostPattern,
280287
this.pathEndsWithSlash,
281288
this.exclusive,
282-
this.exactPath);
289+
this.exactPath,
290+
this.subRouter);
283291
}
284292

285293
public Set<HttpMethod> getMethods() {
@@ -308,7 +316,8 @@ RouteState setMethods(Set<HttpMethod> methods) {
308316
this.virtualHostPattern,
309317
this.pathEndsWithSlash,
310318
this.exclusive,
311-
this.exactPath);
319+
this.exactPath,
320+
this.subRouter);
312321
}
313322

314323
public RouteState addMethod(HttpMethod method) {
@@ -333,7 +342,8 @@ public RouteState addMethod(HttpMethod method) {
333342
this.virtualHostPattern,
334343
this.pathEndsWithSlash,
335344
this.exclusive,
336-
this.exactPath);
345+
this.exactPath,
346+
this.subRouter);
337347

338348
newState.methods.add(method);
339349
return newState;
@@ -365,7 +375,8 @@ RouteState setConsumes(Set<MIMEHeader> consumes) {
365375
this.virtualHostPattern,
366376
this.pathEndsWithSlash,
367377
this.exclusive,
368-
this.exactPath);
378+
this.exactPath,
379+
this.subRouter);
369380
}
370381

371382
RouteState addConsume(MIMEHeader mime) {
@@ -390,7 +401,8 @@ RouteState addConsume(MIMEHeader mime) {
390401
this.virtualHostPattern,
391402
this.pathEndsWithSlash,
392403
this.exclusive,
393-
this.exactPath);
404+
this.exactPath,
405+
this.subRouter);
394406

395407
newState.consumes.add(mime);
396408
return newState;
@@ -422,7 +434,8 @@ RouteState setEmptyBodyPermittedWithConsumes(boolean emptyBodyPermittedWithConsu
422434
this.virtualHostPattern,
423435
this.pathEndsWithSlash,
424436
this.exclusive,
425-
this.exactPath);
437+
this.exactPath,
438+
this.subRouter);
426439
}
427440

428441
public Set<MIMEHeader> getProduces() {
@@ -451,7 +464,8 @@ RouteState setProduces(Set<MIMEHeader> produces) {
451464
this.virtualHostPattern,
452465
this.pathEndsWithSlash,
453466
this.exclusive,
454-
this.exactPath);
467+
this.exactPath,
468+
this.subRouter);
455469
}
456470

457471
RouteState addProduce(MIMEHeader mime) {
@@ -476,7 +490,8 @@ RouteState addProduce(MIMEHeader mime) {
476490
this.virtualHostPattern,
477491
this.pathEndsWithSlash,
478492
this.exclusive,
479-
this.exactPath);
493+
this.exactPath,
494+
this.subRouter);
480495

481496
newState.produces.add(mime);
482497
return newState;
@@ -512,7 +527,8 @@ RouteState setContextHandlers(List<Handler<RoutingContext>> contextHandlers) {
512527
this.virtualHostPattern,
513528
this.pathEndsWithSlash,
514529
this.exclusive,
515-
this.exactPath);
530+
this.exactPath,
531+
this.subRouter);
516532
}
517533

518534
RouteState addContextHandler(Handler<RoutingContext> contextHandler) {
@@ -537,7 +553,8 @@ RouteState addContextHandler(Handler<RoutingContext> contextHandler) {
537553
this.virtualHostPattern,
538554
this.pathEndsWithSlash,
539555
this.exclusive,
540-
this.exactPath);
556+
this.exactPath,
557+
this.subRouter);
541558

542559
int len = newState.contextHandlers.size();
543560
final Priority weight = weight(contextHandler);
@@ -603,7 +620,8 @@ RouteState setFailureHandlers(List<Handler<RoutingContext>> failureHandlers) {
603620
this.virtualHostPattern,
604621
this.pathEndsWithSlash,
605622
this.exclusive,
606-
this.exactPath);
623+
this.exactPath,
624+
this.subRouter);
607625
}
608626

609627
RouteState addFailureHandler(Handler<RoutingContext> failureHandler) {
@@ -628,7 +646,8 @@ RouteState addFailureHandler(Handler<RoutingContext> failureHandler) {
628646
this.virtualHostPattern,
629647
this.pathEndsWithSlash,
630648
this.exclusive,
631-
this.exactPath);
649+
this.exactPath,
650+
this.subRouter);
632651

633652
newState.failureHandlers.add(failureHandler);
634653
return newState;
@@ -660,7 +679,8 @@ RouteState setAdded(boolean added) {
660679
this.virtualHostPattern,
661680
this.pathEndsWithSlash,
662681
this.exclusive,
663-
this.exactPath);
682+
this.exactPath,
683+
this.subRouter);
664684
}
665685

666686
public Pattern getPattern() {
@@ -689,7 +709,8 @@ RouteState setPattern(Pattern pattern) {
689709
this.virtualHostPattern,
690710
this.pathEndsWithSlash,
691711
this.exclusive,
692-
this.exactPath);
712+
this.exactPath,
713+
this.subRouter);
693714
}
694715

695716
public List<String> getGroups() {
@@ -718,7 +739,8 @@ RouteState setGroups(List<String> groups) {
718739
this.virtualHostPattern,
719740
this.pathEndsWithSlash,
720741
this.exclusive,
721-
this.exactPath);
742+
this.exactPath,
743+
this.subRouter);
722744
}
723745

724746
RouteState addGroup(String group) {
@@ -743,7 +765,8 @@ RouteState addGroup(String group) {
743765
this.virtualHostPattern,
744766
this.pathEndsWithSlash,
745767
this.exclusive,
746-
this.exactPath);
768+
this.exactPath,
769+
this.subRouter);
747770

748771
newState.groups.add(group);
749772
return newState;
@@ -775,7 +798,8 @@ RouteState setUseNormalizedPath(boolean useNormalizedPath) {
775798
this.virtualHostPattern,
776799
this.pathEndsWithSlash,
777800
this.exclusive,
778-
this.exactPath);
801+
this.exactPath,
802+
this.subRouter);
779803
}
780804

781805
public Set<String> getNamedGroupsInRegex() {
@@ -804,7 +828,8 @@ RouteState setNamedGroupsInRegex(Set<String> namedGroupsInRegex) {
804828
this.virtualHostPattern,
805829
this.pathEndsWithSlash,
806830
this.exclusive,
807-
this.exactPath);
831+
this.exactPath,
832+
this.subRouter);
808833
}
809834

810835
RouteState addNamedGroupInRegex(String namedGroupInRegex) {
@@ -829,7 +854,8 @@ RouteState addNamedGroupInRegex(String namedGroupInRegex) {
829854
this.virtualHostPattern,
830855
this.pathEndsWithSlash,
831856
this.exclusive,
832-
this.exactPath);
857+
this.exactPath,
858+
this.subRouter);
833859

834860
newState.namedGroupsInRegex.add(namedGroupInRegex);
835861
return newState;
@@ -861,7 +887,8 @@ RouteState setVirtualHostPattern(Pattern virtualHostPattern) {
861887
virtualHostPattern,
862888
this.pathEndsWithSlash,
863889
this.exclusive,
864-
this.exactPath);
890+
this.exactPath,
891+
this.subRouter);
865892
}
866893

867894
public boolean isPathEndsWithSlash() {
@@ -890,13 +917,44 @@ RouteState setPathEndsWithSlash(boolean pathEndsWithSlash) {
890917
this.virtualHostPattern,
891918
pathEndsWithSlash,
892919
this.exclusive,
893-
this.exactPath);
920+
this.exactPath,
921+
this.subRouter);
894922
}
895923

896924
public boolean isExclusive() {
897925
return exclusive;
898926
}
899927

928+
public Router getSubRouter() {
929+
return subRouter;
930+
}
931+
932+
RouteState setSubRouter(Router subRouter) {
933+
return new RouteState(
934+
this.route,
935+
this.metadata,
936+
this.path,
937+
this.name,
938+
this.order,
939+
this.enabled,
940+
this.methods,
941+
this.consumes,
942+
this.emptyBodyPermittedWithConsumes,
943+
this.produces,
944+
this.contextHandlers,
945+
this.failureHandlers,
946+
this.added,
947+
this.pattern,
948+
this.groups,
949+
this.useNormalizedPath,
950+
this.namedGroupsInRegex,
951+
this.virtualHostPattern,
952+
this.pathEndsWithSlash,
953+
this.exclusive,
954+
this.exactPath,
955+
subRouter);
956+
}
957+
900958
RouteState setExclusive(boolean exclusive) {
901959
return new RouteState(
902960
this.route,
@@ -919,7 +977,8 @@ RouteState setExclusive(boolean exclusive) {
919977
this.virtualHostPattern,
920978
this.pathEndsWithSlash,
921979
exclusive,
922-
this.exactPath);
980+
this.exactPath,
981+
this.subRouter);
923982
}
924983

925984
public boolean isExactPath() {
@@ -948,7 +1007,8 @@ RouteState setExactPath(boolean exactPath) {
9481007
this.virtualHostPattern,
9491008
this.pathEndsWithSlash,
9501009
this.exclusive,
951-
exactPath);
1010+
exactPath,
1011+
this.subRouter);
9521012
}
9531013
RouteState setName(String name) {
9541014
return new RouteState(
@@ -972,7 +1032,8 @@ RouteState setName(String name) {
9721032
this.virtualHostPattern,
9731033
this.pathEndsWithSlash,
9741034
this.exclusive,
975-
this.exactPath);
1035+
this.exactPath,
1036+
this.subRouter);
9761037
}
9771038

9781039
private boolean containsMethod(HttpServerRequest request) {
@@ -1325,6 +1386,7 @@ public String toString() {
13251386
", pathEndsWithSlash=" + pathEndsWithSlash +
13261387
", exclusive=" + exclusive +
13271388
", exactPath=" + exactPath +
1389+
", subRouter=" + subRouter +
13281390
'}';
13291391
}
13301392
}

0 commit comments

Comments
 (0)