public static List<CrashFSDriver> parse(final ClassLoader loader, final List<CrashPredicate> paths) { List<CrashFSDriver> drivers = new ArrayList<>(); boolean loginFound = false; for (CrashPredicate path : paths) { for (URI uri : expandPath(loader, path.name)) { // classpath first CrashFSDriver driver = Try .apply(() -> FileSystems.newFileSystem(uri, Collections.emptyMap())) .map(it -> Try.apply(() -> new CrashFSDriver(uri, it, path)).get()) // file system fallback .recover(x -> Try.apply(() -> new CrashFSDriver(uri, Paths.get(uri), path)).get()) .get(); // HACK to make sure login.groovy takes precedence over default one driver.log.debug("driver created: {}", driver); if (loginFound) { driver.filter = FLOGIN.negate().and(driver.filter); } else if (driver.exists(FLOGIN)) { driver.log.debug(" login.groovy found: {}", driver); loginFound = true; } drivers.add(driver); } } return drivers; }
private CompletableFuture<Optional<Object>> send(final Event event) { List<MediaType> produces = event.type().<List<MediaType>>map(ImmutableList::of) .orElse(this.produces); SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locale, locals); return Try.apply(() -> { byte[] bytes = ctx.format(event); return send(event.id(), bytes); }).recover(x -> { CompletableFuture<Optional<Object>> future = new CompletableFuture<>(); future.completeExceptionally(x); return future; }) .get(); }
private Library loadLibrary(final URI lib) { return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap())) .map(it -> Try.apply(() -> new Library(it)).get()) .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get()) .get(); }
/** * Parse value as {@link Duration}. If the value is already a number then it uses as seconds. * Otherwise, it parse expressions like: 8m, 1h, 365d, etc... * * @param maxAge Set the cache header max-age value in seconds. * @return This handler. */ public AssetHandler maxAge(final String maxAge) { Try.apply(() -> Long.parseLong(maxAge)) .recover(x -> ConfigFactory.empty() .withValue("v", ConfigValueFactory.fromAnyRef(maxAge)) .getDuration("v") .getSeconds()) .onSuccess(this::maxAge); return this; }
private CompletableFuture<Optional<Object>> send(final Event event) { List<MediaType> produces = event.type().<List<MediaType>>map(ImmutableList::of) .orElse(this.produces); SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locale, locals); return Try.apply(() -> { byte[] bytes = ctx.format(event); return send(event.id(), bytes); }).recover(x -> { CompletableFuture<Optional<Object>> future = new CompletableFuture<>(); future.completeExceptionally(x); return future; }) .get(); }
/** * Recover from failure. The recover function will be executed in case of failure. * * @param fn Recover function. * @return This try on success, a new success try from recover or a failure try in case of exception. */ public Value<V> recover(Throwing.Function<Throwable, V> fn) { return recover(Throwable.class, fn); }
@Override public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable { Object input = supplier.apply(req, rsp, chain); Object output = Try .apply(() -> mapper.map(input)) .recover(ClassCastException.class, input) .get(); rsp.send(output); chain.next(req, rsp); }
private Object value(final RequestParam param, final Request req, final Response rsp, final Route.Chain chain) throws Throwable { return Try.apply(() -> param.value(req, rsp, chain)) .recover(x -> recoverMissing.apply(x).get()) .get(); }
private Library loadLibrary(final URI lib) { return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap())) .map(it -> Try.apply(() -> new Library(it)).get()) .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get()) .get(); }
/** * Parse value as {@link Duration}. If the value is already a number then it uses as seconds. * Otherwise, it parse expressions like: 8m, 1h, 365d, etc... * * @param maxAge Set the cache header max-age value in seconds. * @return This handler. */ public AssetHandler maxAge(final String maxAge) { Try.apply(() -> Long.parseLong(maxAge)) .recover(x -> ConfigFactory.empty() .withValue("v", ConfigValueFactory.fromAnyRef(maxAge)) .getDuration("v") .getSeconds()) .onSuccess(this::maxAge); return this; }
private Object value(final RequestParam param, final Request req, final Response rsp, final Route.Chain chain) throws Throwable { return Try.apply(() -> param.value(req, rsp, chain)) .recover(x -> recoverMissing.apply(x).get()) .get(); }
@Override public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable { Object input = supplier.apply(req, rsp, chain); Object output = Try .apply(() -> mapper.map(input)) .recover(ClassCastException.class, input) .get(); rsp.send(output); chain.next(req, rsp); }