private static List<Route> findRoutes(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType type, final List<MediaType> accept) { List<Route> routes = new ArrayList<>(); for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, type, accept); if (route.isPresent()) { routes.add(route.get()); } } return routes; }
private List<Route.Filter> pipeline(final String method, final String path, final MediaType contentType, final List<MediaType> accept) { List<Route.Filter> routes = new ArrayList<>(); for (Route.Definition routeDef : this.routes) { Optional<Route> route = routeDef.matches(method, path, contentType, accept); if (route.isPresent()) { routes.add(routeDef.filter()); } } return routes; }
@Override public void handle(final Request req, final Response rsp) throws Exception { if (!rsp.header(ALLOW).isSet()) { Set<String> allow = new LinkedHashSet<>(); Set<String> methods = new LinkedHashSet<>(Route.METHODS); String path = req.path(); methods.remove(req.method()); for (String method : methods) { routes.stream() .filter(route -> route.matches(method, path, MediaType.all, MediaType.ALL).isPresent()) .forEach(route -> allow.add(route.method())); } rsp.header(ALLOW, Joiner.on(SEP).join(allow)) .length(0) .status(Status.OK); } }
@Override public void handle(final Request req, final Response rsp, final Route.Chain chain) throws Throwable { String path = req.path(); for (Route.Definition router : routes) { // ignore glob route if (!router.glob()) { Optional<Route> ifRoute = router .matches(Route.GET, path, MediaType.all, MediaType.ALL); if (ifRoute.isPresent()) { // route found rsp.length(0); ((RouteImpl) ifRoute.get()).handle(req, rsp, chain); return; } } } // not handled, just call next chain.next(req, rsp); } }
@Override public void handle(final Request req, final Response rsp) throws Exception { if (!rsp.header(ALLOW).isSet()) { Set<String> allow = new LinkedHashSet<>(); Set<String> methods = new LinkedHashSet<>(Route.METHODS); String path = req.path(); methods.remove(req.method()); for (String method : methods) { routes.stream() .filter(route -> route.matches(method, path, MediaType.all, MediaType.ALL).isPresent()) .forEach(route -> allow.add(route.method())); } rsp.header(ALLOW, Joiner.on(SEP).join(allow)) .length(0) .status(Status.OK); } }
private static Err handle406or415(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType contentType, final List<MediaType> accept) { for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, MediaType.all, MediaType.ALL); if (route.isPresent() && !route.get().pattern().contains("*")) { if (!routeDef.canProduce(accept)) { return new Err(Status.NOT_ACCEPTABLE, accept.stream() .map(MediaType::name) .collect(Collectors.joining(", "))); } if (!contentType.isAny()) { return new Err(Status.UNSUPPORTED_MEDIA_TYPE, contentType.name()); } } } return null; }
private static List<Route> findRoutes(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType type, final List<MediaType> accept) { List<Route> routes = new ArrayList<>(); for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, type, accept); if (route.isPresent()) { routes.add(route.get()); } } return routes; }
private List<Route.Filter> pipeline(final String method, final String path, final MediaType contentType, final List<MediaType> accept) { List<Route.Filter> routes = new ArrayList<>(); for (Route.Definition routeDef : this.routes) { Optional<Route> route = routeDef.matches(method, path, contentType, accept); if (route.isPresent()) { routes.add(routeDef.filter()); } } return routes; }
@Override public void handle(final Request req, final Response rsp, final Route.Chain chain) throws Throwable { String path = req.path(); for (Route.Definition router : routes) { // ignore glob route if (!router.glob()) { Optional<Route> ifRoute = router .matches(Route.GET, path, MediaType.all, MediaType.ALL); if (ifRoute.isPresent()) { // route found rsp.length(0); ((RouteImpl) ifRoute.get()).handle(req, rsp, chain); return; } } } // not handled, just call next chain.next(req, rsp); } }
private static Err handle406or415(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType contentType, final List<MediaType> accept) { for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, MediaType.all, MediaType.ALL); if (route.isPresent() && !route.get().pattern().contains("*")) { if (!routeDef.canProduce(accept)) { return new Err(Status.NOT_ACCEPTABLE, accept.stream() .map(MediaType::name) .collect(Collectors.joining(", "))); } if (!contentType.isAny()) { return new Err(Status.UNSUPPORTED_MEDIA_TYPE, contentType.name()); } } } return null; }