static final Object strToObject(final String value) { if (value == null || !value.startsWith(PREFIX)) { return value; } return Try.apply(() -> { byte[] bytes = base64().decode(value.substring(PREFIX.length())); // TODO: sanitization of arguments return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); }).get(); }
private static List<URI> expandPath(final ClassLoader loader, final String pattern) { List<URI> result = new ArrayList<>(); File file = new File(pattern); if (file.exists()) { result.add(file.toURI()); } Try.run(() -> Collections.list(loader.getResources(pattern)) .stream() .map(it -> Try.apply(it::toURI).get()) .forEach(result::add)); return result; }
public static final Object strToObject(final String value) { if (value == null || !value.startsWith(PREFIX)) { return value; } return Try.apply(() -> { byte[] bytes = BaseEncoding.base64().decode(value.substring(PREFIX.length())); // TODO: sanitization of arguments return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); }) .wrap(x -> new IllegalArgumentException("Can't de-serialize value " + value, x)) .get(); }
public static Object getOrGenId(final Object bean, final Supplier<Long> next) { Field fid = field(bean); Object id = getId(bean, fid); if (id == null && fid.isAnnotationPresent(GeneratedValue.class)) { if (fid.getType() != Long.class) { throw new IllegalArgumentException("Generated value must be of type Long: " + fid); } id = Try.apply(() -> { Long seq = next.get(); fid.set(bean, seq); return seq; }).wrap(x -> new IllegalStateException("Can't generate id for: " + fid, x)) .get(); } return id; }
@Override public String getRequestContent() { return Try.apply(() -> req.body().value()).get(); }
public Object invoke(final Request req, final Response rsp, Route.Chain chain) { return Try.apply(() -> { Object target = req.require(implementingClass); List<RequestParam> parameters = provider.parameters(handler); Object[] args = new Object[parameters.size()]; for (int i = 0; i < parameters.size(); i++) { args[i] = parameters.get(i).value(req, rsp, chain); } final Object result = handler.invoke(target, args); return result; }).unwrap(InvocationTargetException.class) .get(); } }
private static Object getId(final Object bean, final Field id) { return Try.apply(() -> id.get(bean)).get(); }
public static final String objToStr(final Object value) { if (value instanceof CharSequence || Primitives.isWrapperType(value.getClass())) { return value.toString(); } return Try.apply(() -> { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(bytes); stream.writeObject(value); stream.flush(); return PREFIX + BaseEncoding.base64().encode(bytes.toByteArray()); }) .wrap(x -> new IllegalArgumentException("Can't serialize value " + value, x)) .get(); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public Object exec(final List arguments) throws TemplateModelException { List<String> args = (List<String>) arguments.stream() .map(it -> Try.apply(() -> ((TemplateScalarModel) it).getAsString()).get()) .collect(Collectors.toList()); String[] xss = args.subList(1, args.size()) .toArray(new String[arguments.size() - 1]); return env.xss(xss).apply(args.get(0)); }
private static Field field(final Object bean) { return Try.apply(() -> CACHE.getUnchecked(bean.getClass())) .unwrap(UncheckedExecutionException.class) .get(); }
static String readString(final ClassLoader loader, final String path) { return Try.with(() -> loader.getResourceAsStream("whoops/" + path)) .apply(stream -> new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8)) .get(); }
static final String objToStr(final Object value) { if (value instanceof CharSequence || Primitives.isWrapperType(value.getClass())) { return value.toString(); } return PREFIX + Try.apply(() -> { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(bytes); stream.writeObject(value); stream.flush(); return base64().encode(bytes.toByteArray()); }).get(); } }
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 File extra(final List<Artifact> artifacts, final String name) { return Try.apply(() -> { for (Artifact artifact : artifacts) { for (String tail : artifact.getDependencyTrail()) { if (tail.contains(name)) { return artifact.getFile(); } } } throw new FileNotFoundException(name); }).get(); }
/** * Decode a path by delegating to {@link URLDecoder#decode(String, String)}. * * @param path Path to decoded. * @return Decode a path by delegating to {@link URLDecoder#decode(String, String)}. */ static String decode(String path) { return Try.apply(() -> URLDecoder.decode(path, "UTF-8")).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); }
private Map<String, Object> md(final Executable exec) { return Try.apply(() -> cache.getUnchecked(exec.getDeclaringClass())) .unwrap(UncheckedExecutionException.class) .get(); }
/** * Prepare and startup a {@link Jooby} application. * * @param app Application supplier. * @param args Application arguments. */ public static void run(final Class<? extends Jooby> app, final String... args) { run(() -> Try.apply(() -> app.newInstance()).get(), args); }