router.get("/login", new Pac4jLoginForm(Route.normalize(contextPath + callbackPath))) .name("pac4j(LoginForm)"); router.use(conf.getString("pac4j.callback.method"), callbackPath, new Pac4jCallback(pac4j, callbackRedirectTo, multiProfile, renewSession)) .excludes(excludePaths) .name("pac4j(Callback)"); excludes.remove("/**"); router.use(conf.getString("pac4j.securityFilter.method"), pattern, filter) .excludes(excludes) .name("pac4j(" + filter + ")"); }); conf.getBoolean("pac4j.logout.local"), conf.getBoolean("pac4j.logout.central"))) .name("pac4j(Logout)");
private RouteMethod complement(Route.Definition route, final RouteMethod method) { method.attributes(route.attributes()); String name = route.name(); if (!name.equals("/anonymous")) { method.name(name); } BiConsumer<List<MediaType>, Consumer<List<String>>> types = (values, setter) -> setter.accept(values.stream() .map(MediaType::name) .filter(it -> !it.equals("*/*")) .collect(Collectors.toList())); types.accept(route.consumes(), method::consumes); types.accept(route.produces(), method::produces); return method; } }
Route.Filter h = rdef.filter(); if (h instanceof Route.MethodHandler) { Class<?> routeClass = ((Route.MethodHandler) h).implementingClass();
Method method = null; try { Route.Filter handler = route.filter(); log.debug(" found {} {}", route.method(), route.pattern()); Entry<Object, Node> entry = routeNodes.get(j++); route.method(), ctx); Map<Integer, String> codes = (Map<Integer, String>) doc.remove("@statusCodes"); String desc = (String) doc.remove("@text"); String name = route.name(); if (method == null) { params = new RouteParamCollector(doc, route.method(), route.pattern()) .accept(entry.getValue(), ctx); log.debug(" ignoring {} {} from {}", route.method(), route.pattern(), owner); } else { if (cursor == null) { log.debug("ignoring {} {} no source code available", route.method(), route.pattern(), ex); log.info("ignoring {} {} no source code available", route.method(), route.pattern()); } else { log.error("ignoring {} {} reason {}", route.method(), route.pattern(), ex);
Route.Filter h = rdef.filter(); if (h instanceof Route.MethodHandler) { Class<?> routeClass = ((Route.MethodHandler) h).implementingClass();
Definition definition = new Route.Definition( verb.getSimpleName(), rpath + "/" + path, new MvcHandler(method, routeClass, paramProvider), caseSensitiveRouting) .produces(produces) .consumes(consumes) .excludes(excludes) .declaringClass(routeClass.getName()) .line(classInfo.startAt(method) - 1) .name(name); localAttrs.forEach((n, v) -> definition.attr(n, v)); definitions.add(definition);
/** * Test if the route matches the given verb, path, content type and accept header. * * @param method A HTTP verb. * @param path Current HTTP path. * @param contentType The <code>Content-Type</code> header. * @param accept The <code>Accept</code> header. * @return A route or an empty optional. */ @Nonnull public Optional<Route> matches(final String method, final String path, final MediaType contentType, final List<MediaType> accept) { String fpath = method + path; if (excludes.size() > 0 && excludes(fpath)) { return Optional.empty(); } RouteMatcher matcher = cpattern.matcher(fpath); if (matcher.matches()) { List<MediaType> result = MediaType.matcher(accept).filter(this.produces); if (result.size() > 0 && canConsume(contentType)) { // keep accept when */* List<MediaType> produces = result.size() == 1 && result.get(0).name().equals("*/*") ? accept : this.produces; return Optional .of(asRoute(method, matcher, produces, new RouteSourceImpl(declaringClass, line))); } } return Optional.empty(); }
/** * Parse application bytecode, build route methods from it and merge output with the given routes. * * @param application Application to parse. * @param routes Application routes. * @return List of route methods. * @throws Exception If something goes wrong. */ public List<RouteMethod> parseFully(String application, List<Route.Definition> routes) throws Exception { List<RouteMethod> methods = parse(application); BiFunction<String, String, OptionalInt> routeIndex = (verb, pattern) -> { RouteMethod it = new RouteMethod(verb, pattern, new RouteResponse(void.class)); return IntStream.range(0, methods.size()) .filter(i -> methods.get(i).equals(it)) .findFirst(); }; List<RouteMethod> result = new ArrayList<>(routes.size()); for (Route.Definition route : routes) { routeIndex.apply(route.method(), route.pattern()) .ifPresent(i -> result.add(complement(route, methods.remove(i)))); } customizer.entrySet().forEach(it -> result.stream() .filter(it.getKey()) .forEach(it.getValue()) ); return result; }
public Route.Definition apply(final Route.Definition route) { attrs.build().forEach(route::attr); if (name != null) { route.name(name); } if (prefix != null) { route.name(prefix + "/" + route.name()); } if (consumes != null) { route.consumes(consumes); } if (produces != null) { route.produces(produces); } if (excludes != null) { route.excludes(excludes); } if (mapper != null) { route.map(mapper); } if (renderer != null) { route.renderer(renderer); } return route; } }
private void routes(final StringBuilder buffer) { Function<Route.Definition, String> p = route -> { Route.Filter filter = route.filter(); if (filter instanceof Route.Before) { return "{before}" + route.pattern(); } else if (filter instanceof Route.After) { return "{after}" + route.pattern(); } else if (filter instanceof Route.Complete) { return "{complete}" + route.pattern(); } return route.pattern(); }; int verbMax = 0, routeMax = 0, consumesMax = 0, producesMax = 0; for (Route.Definition route : routes) { verbMax = Math.max(verbMax, route.method().length()); routeMax = Math.max(routeMax, p.apply(route).length()); consumesMax = Math.max(consumesMax, route.consumes().toString().length()); producesMax = Math.max(producesMax, route.produces().toString().length()); } String format = " %-" + verbMax + "s %-" + routeMax + "s %" + consumesMax + "s %" + producesMax + "s (%s)\n"; for (Route.Definition route : routes) { buffer.append( String.format(format, route.method(), p.apply(route), route.consumes(), route.produces(), route.name())); } sockets(buffer, Math.max(verbMax, "WS".length()), routeMax, consumesMax, producesMax); }
/** * Keep track of routes in the order user define them. * * @param method Route method. * @param pattern Route pattern. * @param filter Route filter. * @param creator Route creator. * @return The same route definition. */ private <T extends Route.Definition> T appendDefinition(String method, String pattern, Route.Filter filter, Throwing.Function4<String, String, Route.Filter, Boolean, T> creator) { String pathPattern = prefixPath(pattern).orElse(pattern); T route = creator.apply(method, pathPattern, filter, caseSensitiveRouting); if (prefix != null) { route.prefix = prefix; // reset name will update the name if prefix != null route.name(route.name()); } bag.add(route); return route; }
private void mvcRoutes(String path, final Class type, Consumer<RouteMethod> callback) { Env env = Env.DEFAULT.build(ConfigFactory.empty() .withValue("application.env", ConfigValueFactory.fromAnyRef("dev"))); MvcRoutes.routes(env, new RouteMetadata(env), "", true, type) .forEach(r -> { RouteMethod method = toRouteMethod(r); javadoc(method, javadoc.pop(type.getName(), r.method(), r.pattern())); if (path.length() > 0) { method.pattern(Route.normalize(path) + method.pattern()); } // Set default tag Annotation rootPath = type.getAnnotation(org.jooby.mvc.Path.class); if (rootPath != null) { method.attribute("route.tag", mvcRouteTag(type.getSimpleName())); } callback.accept(method); }); }
public RouteSpecImpl(final Route.Definition route, final String name, final String summary, final String doc, final List<RouteParam> params, final RouteResponse rsp) { put("method", route.method()); put("pattern", route.pattern()); if (!name.equals("/anonymous")) { put("name", name); } put("consumes", route.consumes().stream() .map(MediaType::name) .collect(Collectors.toList())); put("produces", route.produces().stream() .map(MediaType::name) .collect(Collectors.toList())); put("summary", summary); put("doc", doc); put("params", params); put("response", rsp); put("attributes", new HashMap<>(route.attributes())); }
@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 configure(final Env env, final Config conf, final Binder binder) { Config $cookie = conf.getConfig("flash.cookie"); String cpath = $cookie.getString("path"); boolean chttp = $cookie.getBoolean("httpOnly"); boolean csecure = $cookie.getBoolean("secure"); Cookie.Definition cookie = this.cookie .orElseGet(() -> new Cookie.Definition($cookie.getString("name"))); // uses user provided or fallback to defaults cookie.path(cookie.path().orElse(cpath)) .httpOnly(cookie.httpOnly().orElse(chttp)) .secure(cookie.secure().orElse(csecure)); env.router() .use(method, path, new FlashScopeHandler(cookie, decoder, encoder)) .name("flash-scope"); }
private RouteMethod complement(Route.Definition route, final RouteMethod method) { method.attributes(route.attributes()); String name = route.name(); if (!name.equals("/anonymous")) { method.name(name); } BiConsumer<List<MediaType>, Consumer<List<String>>> types = (values, setter) -> setter.accept(values.stream() .map(MediaType::name) .filter(it -> !it.equals("*/*")) .collect(Collectors.toList())); types.accept(route.consumes(), method::consumes); types.accept(route.produces(), method::produces); return method; } }