|
86 | 86 | import javax.net.ssl.SSLContext; |
87 | 87 |
|
88 | 88 | import org.jooby.Route.Definition; |
| 89 | +import org.jooby.Route.Mapper; |
89 | 90 | import org.jooby.Session.Store; |
90 | 91 | import org.jooby.handlers.AssetHandler; |
91 | 92 | import org.jooby.internal.AppPrinter; |
|
125 | 126 |
|
126 | 127 | import com.google.common.base.Strings; |
127 | 128 | import com.google.common.collect.ImmutableList; |
| 129 | +import com.google.common.collect.ImmutableMap; |
128 | 130 | import com.google.common.collect.ImmutableSet; |
129 | 131 | import com.google.inject.Binder; |
130 | 132 | import com.google.inject.Guice; |
@@ -457,15 +459,83 @@ default Config config() { |
457 | 459 |
|
458 | 460 | } |
459 | 461 |
|
460 | | - private static class RouteClass { |
| 462 | + private static class MvcClass implements Route.Props<MvcClass> { |
461 | 463 | Class<?> routeClass; |
462 | 464 |
|
463 | 465 | String path; |
464 | 466 |
|
465 | | - public RouteClass(final Class<?> routeClass, final String path) { |
| 467 | + ImmutableMap.Builder<String, Object> attrs = ImmutableMap.builder(); |
| 468 | + |
| 469 | + private List<MediaType> consumes; |
| 470 | + |
| 471 | + private String name; |
| 472 | + |
| 473 | + private List<MediaType> produces; |
| 474 | + |
| 475 | + private List<String> excludes; |
| 476 | + |
| 477 | + private Mapper<?> mapper; |
| 478 | + |
| 479 | + public MvcClass(final Class<?> routeClass, final String path) { |
466 | 480 | this.routeClass = routeClass; |
467 | 481 | this.path = path; |
468 | 482 | } |
| 483 | + |
| 484 | + @Override |
| 485 | + public MvcClass attr(final String name, final Object value) { |
| 486 | + attrs.put(name, value); |
| 487 | + return this; |
| 488 | + } |
| 489 | + |
| 490 | + @Override |
| 491 | + public MvcClass name(final String name) { |
| 492 | + this.name = name; |
| 493 | + return this; |
| 494 | + } |
| 495 | + |
| 496 | + @Override |
| 497 | + public MvcClass consumes(final List<MediaType> consumes) { |
| 498 | + this.consumes = consumes; |
| 499 | + return this; |
| 500 | + } |
| 501 | + |
| 502 | + @Override |
| 503 | + public MvcClass produces(final List<MediaType> produces) { |
| 504 | + this.produces = produces; |
| 505 | + return this; |
| 506 | + } |
| 507 | + |
| 508 | + @Override |
| 509 | + public MvcClass excludes(final List<String> excludes) { |
| 510 | + this.excludes = excludes; |
| 511 | + return this; |
| 512 | + } |
| 513 | + |
| 514 | + @Override |
| 515 | + public MvcClass map(final Mapper<?> mapper) { |
| 516 | + this.mapper = mapper; |
| 517 | + return this; |
| 518 | + } |
| 519 | + |
| 520 | + public Route.Definition apply(final Route.Definition route) { |
| 521 | + attrs.build().forEach(route::attr); |
| 522 | + if (name != null) { |
| 523 | + route.name(name); |
| 524 | + } |
| 525 | + if (consumes != null) { |
| 526 | + route.consumes(consumes); |
| 527 | + } |
| 528 | + if (produces != null) { |
| 529 | + route.produces(produces); |
| 530 | + } |
| 531 | + if (excludes != null) { |
| 532 | + route.excludes(excludes); |
| 533 | + } |
| 534 | + if (mapper != null) { |
| 535 | + route.map(mapper); |
| 536 | + } |
| 537 | + return route; |
| 538 | + } |
469 | 539 | } |
470 | 540 |
|
471 | 541 | private static class EnvDep { |
@@ -585,8 +655,8 @@ private Jooby use(final Optional<String> path, final Jooby app) { |
585 | 655 | this.bag.add(rewrite.apply((Definition) it)); |
586 | 656 | } else if (it instanceof Route.Group) { |
587 | 657 | ((Route.Group) it).routes().forEach(r -> this.bag.add(rewrite.apply(r))); |
588 | | - } else if (it instanceof RouteClass) { |
589 | | - Object routes = path.<Object> map(p -> new RouteClass(((RouteClass) it).routeClass, p)) |
| 658 | + } else if (it instanceof MvcClass) { |
| 659 | + Object routes = path.<Object> map(p -> new MvcClass(((MvcClass) it).routeClass, p)) |
590 | 660 | .orElse(it); |
591 | 661 | this.bag.add(routes); |
592 | 662 | } else if (it instanceof EnvDep) { |
@@ -3031,10 +3101,11 @@ public Route.Definition assets(final String path, final AssetHandler handler) { |
3031 | 3101 | * @return This jooby instance. |
3032 | 3102 | */ |
3033 | 3103 | @Override |
3034 | | - public Jooby use(final Class<?> routeClass) { |
| 3104 | + public Route.Collection use(final Class<?> routeClass) { |
3035 | 3105 | requireNonNull(routeClass, "Route class is required."); |
3036 | | - bag.add(new RouteClass(routeClass, "")); |
3037 | | - return this; |
| 3106 | + MvcClass mvc = new MvcClass(routeClass, ""); |
| 3107 | + bag.add(mvc); |
| 3108 | + return new Route.Collection(mvc); |
3038 | 3109 | } |
3039 | 3110 |
|
3040 | 3111 | /** |
@@ -3125,18 +3196,19 @@ public Route.Definition sse(final String path, final Sse.Handler1 handler) { |
3125 | 3196 | return appendDefinition(new Route.Definition("GET", path, handler)).consumes(MediaType.sse); |
3126 | 3197 | } |
3127 | 3198 |
|
| 3199 | + @SuppressWarnings("rawtypes") |
3128 | 3200 | @Override |
3129 | 3201 | public Route.Collection with(final Runnable callback) { |
3130 | 3202 | // hacky way of doing what we want... but we do simplify developer life |
3131 | 3203 | int size = this.bag.size(); |
3132 | 3204 | callback.run(); |
3133 | | - // collect latest routes and apply collection attr |
3134 | | - List<Route.Definition> local = this.bag.stream() |
| 3205 | + // collect latest routes and apply route props |
| 3206 | + List<Route.Props> local = this.bag.stream() |
3135 | 3207 | .skip(size) |
3136 | | - .filter(Predicates.instanceOf(Route.Definition.class)) |
3137 | | - .map(r -> (Route.Definition) r) |
| 3208 | + .filter(Predicates.instanceOf(Route.Props.class)) |
| 3209 | + .map(r -> (Route.Props) r) |
3138 | 3210 | .collect(Collectors.toList()); |
3139 | | - return new Route.Collection(local.toArray(new Route.Definition[local.size()])); |
| 3211 | + return new Route.Collection(local.toArray(new Route.Props[local.size()])); |
3140 | 3212 | } |
3141 | 3213 |
|
3142 | 3214 | /** |
@@ -3385,10 +3457,12 @@ private static List<Object> normalize(final List<Object> services, final Env env |
3385 | 3457 | } else if (candidate instanceof Route.Group) { |
3386 | 3458 | ((Route.Group) candidate).routes() |
3387 | 3459 | .forEach(r -> result.add(r)); |
3388 | | - } else if (candidate instanceof RouteClass) { |
3389 | | - Class<?> routeClass = ((RouteClass) candidate).routeClass; |
3390 | | - String path = ((RouteClass) candidate).path; |
3391 | | - MvcRoutes.routes(env, classInfo, path, routeClass).forEach(route -> { |
| 3460 | + } else if (candidate instanceof MvcClass) { |
| 3461 | + MvcClass mvcRoute = ((MvcClass) candidate); |
| 3462 | + Class<?> mvcClass = mvcRoute.routeClass; |
| 3463 | + String path = ((MvcClass) candidate).path; |
| 3464 | + MvcRoutes.routes(env, classInfo, path, mvcClass).forEach(route -> { |
| 3465 | + mvcRoute.apply(route); |
3392 | 3466 | if (prefix != null) { |
3393 | 3467 | route.name(prefix + "/" + route.name()); |
3394 | 3468 | } |
|
0 commit comments