...74 public static PredicatedConfig matching(Predicate<HttpRequest> predicate) {75 return new PredicatedConfig(Require.nonNull("Predicate", predicate));76 }77 public static TemplatizedRouteConfig delete(String template) {78 UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));79 return new TemplatizedRouteConfig(80 new MatchesHttpMethod(DELETE).and(new MatchesTemplate(urlTemplate)),81 urlTemplate);82 }83 public static TemplatizedRouteConfig get(String template) {84 UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));85 return new TemplatizedRouteConfig(86 new MatchesHttpMethod(GET).and(new MatchesTemplate(urlTemplate)),87 urlTemplate);88 }89 public static TemplatizedRouteConfig post(String template) {90 UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));91 return new TemplatizedRouteConfig(92 new MatchesHttpMethod(POST).and(new MatchesTemplate(urlTemplate)),93 urlTemplate);94 }95 public static TemplatizedRouteConfig options(String template) {96 UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));97 return new TemplatizedRouteConfig(98 new MatchesHttpMethod(OPTIONS).and(new MatchesTemplate(urlTemplate)),99 urlTemplate);100 }101 public static NestedRouteConfig prefix(String prefix) {102 Require.nonNull("Prefix", prefix);103 Require.stateCondition(!prefix.isEmpty(), "Prefix to use must not be of 0 length");104 return new NestedRouteConfig(prefix);105 }106 public static Route combine(Routable first, Routable... others) {107 Require.nonNull("At least one route", first);108 return new CombinedRoute(Stream.concat(Stream.of(first), Stream.of(others)));109 }110 public static Route combine(Iterable<Routable> routes) {111 Require.nonNull("At least one route", routes);112 return new CombinedRoute(StreamSupport.stream(routes.spliterator(), false));113 }114 public static class TemplatizedRouteConfig {115 private final Predicate<HttpRequest> predicate;116 private final UrlTemplate template;117 private TemplatizedRouteConfig(Predicate<HttpRequest> predicate, UrlTemplate template) {118 this.predicate = Require.nonNull("Predicate", predicate);119 this.template = Require.nonNull("URL template", template);120 }121 public Route to(Supplier<HttpHandler> handler) {122 Require.nonNull("Handler supplier", handler);123 return to(params -> handler.get());124 }125 public Route to(Function<Map<String, String>, HttpHandler> handlerFunc) {126 Require.nonNull("Handler creator", handlerFunc);127 return new TemplatizedRoute(template, predicate, handlerFunc);128 }129 }130 private static class TemplatizedRoute extends Route {131 private final UrlTemplate template;132 private final Predicate<HttpRequest> predicate;133 private final Function<Map<String, String>, HttpHandler> handlerFunction;134 private TemplatizedRoute(135 UrlTemplate template,136 Predicate<HttpRequest> predicate,137 Function<Map<String, String>, HttpHandler> handlerFunction) {138 this.template = Require.nonNull("URL template", template);139 this.predicate = Require.nonNull("Predicate", predicate);140 this.handlerFunction = Require.nonNull("Handler function", handlerFunction);141 }142 @Override143 public boolean matches(HttpRequest request) {144 return predicate.test(request);145 }146 @Override147 protected HttpResponse handle(HttpRequest req) {148 UrlTemplate.Match match = template.match(req.getUri());149 HttpHandler handler = handlerFunction.apply(150 match == null ? ImmutableMap.of() : match.getParameters());151 if (handler == null) {152 return new HttpResponse()153 .setStatus(HTTP_INTERNAL_ERROR)154 .setContent(utf8String("Unable to find handler for " + req));155 }156 return handler.execute(req);157 }158 }159 private static class MatchesHttpMethod implements Predicate<HttpRequest> {160 private final HttpMethod method;161 private MatchesHttpMethod(HttpMethod method) {162 this.method = Require.nonNull("HTTP method to test", method);163 }164 @Override165 public boolean test(HttpRequest request) {166 return method == request.getMethod();167 }168 }169 private static class MatchesTemplate implements Predicate<HttpRequest> {170 private final UrlTemplate template;171 private MatchesTemplate(UrlTemplate template) {172 this.template = Require.nonNull("URL template to test", template);173 }174 @Override175 public boolean test(HttpRequest request) {176 return template.match(request.getUri()) != null;177 }178 }179 public static class NestedRouteConfig {180 private final String prefix;181 private NestedRouteConfig(String prefix) {182 this.prefix = Require.nonNull("Prefix", prefix);183 }184 public Route to(Route route) {185 Require.nonNull("Target for requests", route);...