@Override public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append(method()).append(" ").append(pattern()).append("\n"); buffer.append(" name: ").append(name()).append("\n"); buffer.append(" excludes: ").append(excludes).append("\n"); buffer.append(" consumes: ").append(consumes()).append("\n"); buffer.append(" produces: ").append(produces()).append("\n"); return buffer.toString(); }
@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 RouteMethod toRouteMethod(final Route.Definition route) { Method handler = ((Route.MethodHandler) route.filter()).method(); return new RouteMethod(route.method(), route.pattern(), new RouteResponse(handler.getGenericReturnType())) .name(route.name()) .parameters(Arrays.asList(handler.getParameters()).stream() .filter(SKIP) .map(it -> BytecodeRouteParser.toRouteParameter(route.pattern(), it)) .filter(Objects::nonNull) .collect(Collectors.toList())); }
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"); 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.Definition result = new Route.Definition(r.method(), p + r.pattern(), r.filter()); result.consumes(r.consumes()); result.produces(r.produces());
/** * 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; }
} else if (route.vars().contains(name)) { paramType = RouteParamType.PATH; } else if (route.method().equals("GET")) { paramType = RouteParamType.QUERY; } else {
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); }
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())); }
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); }); }
@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 RouteMethod toRouteMethod(final Route.Definition route) { Method handler = ((Route.MethodHandler) route.filter()).method(); return new RouteMethod(route.method(), route.pattern(), new RouteResponse(handler.getGenericReturnType())) .name(route.name()) .parameters(Arrays.asList(handler.getParameters()).stream() .filter(SKIP) .map(it -> BytecodeRouteParser.toRouteParameter(route.pattern(), it)) .filter(Objects::nonNull) .collect(Collectors.toList())); }
@Override public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append(method()).append(" ").append(pattern()).append("\n"); buffer.append(" name: ").append(name()).append("\n"); buffer.append(" excludes: ").append(excludes).append("\n"); buffer.append(" consumes: ").append(consumes()).append("\n"); buffer.append(" produces: ").append(produces()).append("\n"); return buffer.toString(); }
Route.Definition result = new Route.Definition(r.method(), p + r.pattern(), r.filter()); result.consumes(r.consumes()); result.produces(r.produces());
/** * 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; }
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); }
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); }); }