.orElse(ConfigFactory.empty()); Config $hikari = dbconf.apply(key + ".hikari") .withFallback(dbconf.apply("db." + db + ".hikari")) .withFallback(dbconf.apply("hikari")); .entrySet().forEach(entry -> dumper.accept("", entry))); dbconf.apply(key) .withoutPath("hikari") .entrySet().forEach(entry -> dumper.accept("dataSource.", entry));
Swagger base = Json.mapper().convertValue(hash, Swagger.class); Swagger swagger = new SwaggerBuilder(options.tagger) .build(base, provider.apply(req.require(M))); boolean json = req.path().endsWith(".json"); if (configurer != null) {
Map<String, Object> hash = conf.getConfig("raml").root().unwrapped(); Raml base = Json.mapper().convertValue(hash, Raml.class); Raml raml = Raml.build(base, provider.apply(req.require(M))); if (configurer != null) { configurer.accept(raml);
@Override public Session get(final Builder builder) { ResultSet rs = session .execute(new BoundStatement(selectSQL.apply(tableName)).bind(builder.sessionId())); return Optional.ofNullable(rs.one()) .map(row -> { long createdAt = row.getTimestamp(CREATED_AT).getTime(); long accessedAt = row.getTimestamp(ACCESSED_AT).getTime(); long savedAt = row.getTimestamp(SAVED_AT).getTime(); Map<String, String> attributes = row.getMap(ATTRIBUTES, String.class, String.class); Session session = builder .accessedAt(accessedAt) .createdAt(createdAt) .savedAt(savedAt) .set(attributes) .build(); // touch ttl if (timeout > 0) { save(session); } return session; }) .orElse(null); }
@Override public void handle(final Request req, final Response rsp) { compiler.fileset().forEach(asset -> { /** Styles */ List<String> css = this.styles.apply(asset); String styles = css.stream().reduce(new StringBuilder(), (buff, it) -> buff.append("<link href=\"") .append(cpath) .append(it) .append("\" rel=\"stylesheet\">\n"), StringBuilder::append) .toString(); req.set(asset + "_css", css); req.set(asset + "_styles", styles); /** Scripts */ List<String> js = this.scripts.apply(asset); String scripts = js.stream().reduce(new StringBuilder(), (buff, it) -> buff.append("<script src=\"") .append(cpath) .append(it) .append("\"></script>\n"), StringBuilder::append) .toString(); req.set(asset + "_js", js); req.set(asset + "_scripts", scripts); }); }
@Override public void configure(final Env env, final Config conf, final Binder binder) throws Exception { boolean useFile = Stream.of(swaggerOptions, ramlOptions) .filter(Objects::nonNull) .filter(it -> it.file != null) .findFirst() .isPresent(); Throwing.Function<Set<Route.Definition>, List<RouteMethod>> provider = null; if (!useFile) { Path dir = Optional.ofNullable(basedir).orElse(Paths.get(conf.getString("user.dir"))); ApiParser parser = new ApiParser(dir, filter); customizer.forEach(parser::modify); provider = routes -> parser .parseFully(conf.getString("application.class"), new ArrayList<>(routes)); if (!env.name().equals("dev")) { provider = provider.memoized(); } binder.bind(ApiParser.class).toInstance(parser); } String contextPath = conf.getString("application.path"); if (swaggerOptions != null) { swagger(contextPath, env.router(), swaggerOptions, conf, swagger, provider); } if (ramlOptions != null) { raml(contextPath, env.router(), ramlOptions, raml, provider); } }
/** * Recover from failure if and only if the exception is a subclass of the given exception filter. * The recover function will be executed in case of failure. * * @param exception Exception filter. * @param fn Recover function. * @param <X> Exception type. * @return This try on success, a new success try from recover or a failure try in case of exception. */ public <X extends Throwable> Value<V> recoverWith(Class<X> exception, Throwing.Function<X, Value<V>> fn) { return (Value<V>) getCause() .filter(exception::isInstance) .map(x -> { try { return fn.apply((X) x); } catch (Throwable ex) { return failure(ex); } }) .orElse(this); }
public Object newBean(final Throwing.Function<RequestParam, Object> lookup, final Set<String> params) throws Throwable { log.debug("instantiating object {}", constructor); Object[] args = new Object[parameters.size()]; List<String> names = new ArrayList<>(params); // remove constructor injected params for (int i = 0; i < args.length; i++) { RequestParam param = parameters.get(i); args[i] = lookup.apply(param); // skip constructor injected param (don't override) names.remove(param.name); } Object bean = constructor.newInstance(args); List<BeanPath> paths = compile(names.stream().sorted().iterator(), beanType); for (BeanPath path : paths) { String rawpath = path.toString(); log.debug(" setting {}", rawpath); path.set(bean, lookup.apply(new RequestParam(path.setelem(), rawpath, path.settype()))); } return bean; }
/** * In case of failure wrap an exception matching the given predicate to something else. * * @param predicate Exception predicate. * @param wrapper Exception mapper. * @param <X> Exception type. * @return This try for success or a new failure with exception wrapped. */ public <X extends Throwable> Try wrap(Throwing.Predicate<X> predicate, Throwing.Function<X, Throwable> wrapper) { try { return getCause() .filter(x -> predicate.test((X) x)) .map(x -> (Try) Try.failure(wrapper.apply((X) x))) .orElse(this); } catch (Throwable x) { return failure(x); } }
/** * Apply the resource and produces an output. * * @param fn Function to apply. * @param <V> Output type. * @return A new try result. */ public <V> Value<V> apply(Throwing.Function<R, V> fn) { return Try.apply(() -> { try (R r1 = this.r.get()) { if (r1 instanceof ProxyCloseable) { return fn.apply((R) ((ProxyCloseable) r1).resource); } return fn.apply(r1); } }); }
/** * Flat map the success value. * * @param mapper Mapper. * @param <T> New type. * @return A new try value for success or failure. */ public <T> Value<T> flatMap(Throwing.Function<V, Value<T>> mapper) { if (isFailure()) { return (Value<T>) this; } try { return mapper.apply(get()); } catch (Throwable x) { return new Failure<>(x); } }
@Override public R tryGet() throws Throwable { if (parent instanceof ProxyCloseable) { ProxyCloseable proxy = (ProxyCloseable) parent; if (proxy.resource == null) { proxy.get(); } this.parentResource = (P) proxy.resource; } else { this.parentResource = parent.get(); } this.resource = mapper.apply(parentResource); return (R) this; } }
public CassandraSessionStore(final com.datastax.driver.core.Session session, final int timeout) { this.session = requireNonNull(session, "Session required."); this.timeout = timeout; createTableIfNotExists(session, tableName, log); this.insertSQL = Throwing.<String, PreparedStatement>throwingFunction(table -> session.prepare(insertSQL(table, timeout)) ).memoized(); this.selectSQL = Throwing.<String, PreparedStatement>throwingFunction(table -> session.prepare(selectSQL(table)) ).memoized(); this.deleteSQL = Throwing.<String, PreparedStatement>throwingFunction(table -> session.prepare(deleteSQL(table)) ).memoized(); }
@Override public <T> T apply(final Throwing.Function<Session, T> callback) throws Throwable { try { begin(); T value = callback.apply(session); return value; } catch (Throwable x) { rollbackOnly = true; throw x; } finally { end(); } }
@SuppressWarnings("rawtypes") private <T> T find(final ClientFinder finder, final Clients clients, final WebContext ctx, final Class<? extends Client<?, ?>> clientType, final String clientName, final Throwing.Function<Client, T> fn) throws Throwable { List<Client> result = finder.find(clients, ctx, clientName); for (Client client : result) { T value = fn.apply(client); if (value != null) { return value; } } return null; }
@Override public void delete(final String id) { session.execute(new BoundStatement(deleteSQL.apply(tableName)).bind(id)); }
@Override public Scheduler getNewThreadScheduler() { return schedulers.apply("newThread"); }
@Override public Object map(final T value) throws Throwable { return fn.apply(value); }