@Override public <B> Either<A, B> left(B right) { return Either.left(a); }
@Override public <B> Either<B, A> right(B left) { return Either.left(left); }
/** Create a single access control entry. */ public static Either<AccessControlEntry, List<AccessControlEntry>> entry(String role, String action, boolean allow) { return left(new AccessControlEntry(role, action, allow)); }
/** Parsing of <code>src</code> without namespaces. */ public static Either<Exception, Document> parse(InputSource src) { try { DocumentBuilder docBuilder = dbf.newDocumentBuilder(); return right(docBuilder.parse(src)); } catch (Exception e) { return left(e); } }
/** Namespace aware parsing of <code>src</code>. */ public static Either<Exception, Document> parseNs(InputSource src) { try { DocumentBuilder docBuilder = nsDbf.newDocumentBuilder(); return right(docBuilder.parse(src)); } catch (Exception e) { return left(e); } }
@Override public Either<Exception, B> apply(A a) { try { return right(xapply(a)); } catch (Exception e) { return left(e); } } };
@Override public Either<Exception, AccessControlList> apply(String s) { try { return right(parseAcl(s)); } catch (Exception e) { return left(e); } } };
@Override public List<AccessControlEntry> apply(List<AccessControlEntry> sum, Either<AccessControlEntry, List<AccessControlEntry>> current) { if (current.isLeft()) sum.add(current.left().value()); else sum.addAll(current.right().value()); return sum; } });
/** * Wait for a certain status of a resource. * * @return either an exception or the status code of the last http response */ public static Either<Exception, Integer> waitForResource(final TrustedHttpClient http, final URI resourceUri, final int expectedStatus, final long timeout, final long pollingInterval) { long now = 0L; while (true) { final HttpHead head = new HttpHead(resourceUri); final Either<Exception, Integer> result = http.<Integer> run(head).apply(getStatusCode); for (final Integer status : result.right()) { if (eq(status, expectedStatus) || now >= timeout) { return right(status); } else if (now < timeout) { if (!sleep(pollingInterval)) { return left(new Exception("Interrupted")); } else { now = now + pollingInterval; } } } for (Exception e : result.left()) { return left(e); } } } }
@Override public final Either<Err, B> apply(A a) { try { return right(xapply(a)); } catch (Exception e) { return left(toErr.apply(e)); } }
@Override public <A> Either<F, A> tx(Function<EntityManager, A> transactional) { try { return Either.right(penv.tx(transactional)); } catch (Exception e) { return Either.left(exHandler.apply(e)); } }
@Override public Either<C, B> apply(A a) { try { B b = f.apply(a); return right(b); } catch (Exception e) { C c = handler.apply(e); return left(c); } } };
/** * Handle the HTTP response. * * @return either a token to initiate a follow-up request or a file or none if the requested URI cannot be found * @throws IOException * in case of any IO related issues */ private Either<String, Option<File>> handleDownloadResponse(HttpResponse response, URI src, File dst) throws IOException { final String url = src.toString(); final int status = response.getStatusLine().getStatusCode(); switch (status) { case HttpServletResponse.SC_NOT_FOUND: return right(none(File.class)); case HttpServletResponse.SC_NOT_MODIFIED: logger.debug("{} has not been modified.", url); return right(some(dst)); case HttpServletResponse.SC_ACCEPTED: logger.debug("{} is not ready, try again later.", url); return left(response.getHeaders("token")[0].getValue()); case HttpServletResponse.SC_OK: logger.info("Downloading {} to {}", url, dst.getAbsolutePath()); return right(some(downloadTo(response, dst))); default: logger.warn(format("Received unexpected response status %s while trying to download from %s", status, url)); FileUtils.deleteQuietly(dst); return right(none(File.class)); } }
/** * Load the SMIL document identified by <code>mpe</code>. Throws an exception if it does not exist or cannot be loaded * by any reason. * * @return the document */ public static Document loadSmilDocument(InputStream in, MediaPackageElement mpe) { try { Either<Exception, org.w3c.dom.Document> eitherDocument = XmlUtil.parseNs(new InputSource(in)); if (eitherDocument.isRight()) return eitherDocument.right().value(); throw eitherDocument.left().value(); } catch (Exception e) { logger.warn("Unable to load smil document from catalog '{}'", mpe, e); return Misc.chuck(e); } }
@Override public Either<Exception, A> run(Function<HttpResponse, A> f) { HttpResponse response = null; try { response = client.execute(req); return right(f.apply(response)); } catch (Exception e) { return left(e); } finally { if (response != null) { client.close(response); } } } };
@Override public Either<Exception, A> apply(Function<HttpResponse, A> responseHandler) { HttpResponse response = null; try { response = client.execute(httpUriRequest); return right(responseHandler.apply(response)); } catch (Exception e) { return left(e); } finally { if (response != null) { client.close(response); } } } };
/** Get the value or throw the exception. */ public static <A> A getOrError(Either<Exception, A> response) { if (response.isRight()) { return response.right().value(); } else { return chuck(response.left().value()); } }
/** * Handle a stream inside <code>f</code> and ensure that <code>s</code> gets closed properly. * * @param s * the stream creation function * @param toErr * error handler transforming an exception into something else * @param f * stream handler * @deprecated use * {@link #withResource(org.opencastproject.util.data.Function0, org.opencastproject.util.data.Function, org.opencastproject.util.data.Function)} * instead */ @Deprecated public static <A, Err> Either<Err, A> withStream(Function0<InputStream> s, Function<Exception, Err> toErr, Function<InputStream, A> f) { InputStream in = null; try { in = s.apply(); return right(f.apply(in)); } catch (Exception e) { return left(toErr.apply(e)); } finally { IoSupport.closeQuietly(in); } }
/** * Handle a closeable resource inside <code>f</code> and ensure that <code>r</code> gets closed properly. * * @param r * resource creation function * @param toErr * error handler transforming an exception into something else * @param f * resource handler */ public static <A, Err, B extends Closeable> Either<Err, A> withResource(Function0<B> r, Function<Exception, Err> toErr, Function<B, A> f) { B b = null; try { b = r.apply(); return right(f.apply(b)); } catch (Exception e) { return left(toErr.apply(e)); } finally { IoSupport.closeQuietly(b); } }
public static String getContentOn(final RequestRunner<String> runner, final int... status) { final Either<Exception, String> res = runner.run(getContentOn(status)); if (res.isRight()) { return res.right().value(); } else { return chuck(res.left().value()); } }