/** URI constructor function without checked exceptions. */ public static URI uri(String uri) { try { return new URI(uri); } catch (Exception e) { return chuck(e); } }
/** URL constructor function without checked exceptions. */ public static URL url(String url) { try { return new URL(url); } catch (Exception e) { return chuck(e); } }
/** Like {@link IOUtils#toString(java.net.URL, String)} but without checked exception. */ public static String readToString(URL url, String encoding) { try { return IOUtils.toString(url, encoding); } catch (IOException e) { return chuck(e); } }
private static Map parse(String json) { try { return (Map) new JSONParser().parse(json); } catch (ParseException e) { return chuck(e); } }
private JAXBContext init(String contextPath) { try { return JAXBContext.newInstance(contextPath, this.getClass().getClassLoader()); } catch (JAXBException e) { return chuck(e); } }
/** Create a new DOM document. */ private static Document newDocument() { final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); try { return docBuilderFactory.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { return chuck(e); } }
@Override public Date apply(String a) { final Date date = EncodingSchemeUtils.decodeDate(a); return date != null ? date : Misc.<Date>chuck(new RuntimeException(a + " does not conform to W3C-DTF encoding scheme.")); } });
/** Same like {@link #parseAcl(String)} but throws runtime exceptions in case of an error. */ public static AccessControlList parseAclSilent(String serializedForm) { try { return parseAcl(serializedForm); } catch (Exception e) { return chuck(e); } }
/** Serializes a media package to a {@link Document} without any further processing. */ public static Document getAsXmlDocument(MediaPackage mp) { try { final Marshaller marshaller = MediaPackageImpl.context.createMarshaller(); final Document doc = newDocument(); marshaller.marshal(mp, doc); return doc; } catch (JAXBException e) { return chuck(e); } }
@Override public Long apply(DublinCoreValue a) { final Long extent = EncodingSchemeUtils.decodeDuration(a); return extent != null ? extent : Misc.<Long>chuck(new RuntimeException(a + " does not conform to ISO8601 encoding scheme for durations.")); } });
@Override public final C apply(A a, B b) { try { return xapply(a, b); } catch (Exception e) { return chuck(e); } }
@Override public final A apply() { try { return xapply(); } catch (Exception e) { return chuck(e); } }
public static Properties loadPropertiesFromFile(final String path) { try { return loadPropertiesFromStream(new FileInputStream(path)); } catch (FileNotFoundException e) { return chuck(e); } }
public static Properties loadPropertiesFromUrl(final URL url) { try { return loadPropertiesFromStream(url.openStream()); } catch (IOException e) { return chuck(e); } }
@Override public String apply(Tuple<String, String> a) { try { return a.getA() + "=" + URLEncoder.encode(a.getB(), "UTF-8"); } catch (UnsupportedEncodingException e) { return chuck(e); } } }).mkString("&")).mkString("?");
@Override public String getMountPoint() { for (HttpResponse r : option(getResponse(get("mountpoint"), HttpStatus.SC_OK))) { try { return EntityUtils.toString(r.getEntity()); } catch (IOException e) { logger.error("Cannot contact remote service", e); return chuck(e); } } throw new RuntimeException("Cannot contact remote service"); } }
private static void setFormParams(HttpEntityEnclosingRequest r, NameValuePair[] formParams) { final List<NameValuePair> params = list(formParams); try { r.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); } catch (UnsupportedEncodingException e) { chuck(e); } }
@Override public Job nop() { try { return getServiceRegistry().createJob(getJobType(), "nop"); } catch (ServiceRegistryException e) { return chuck(e); } } }
@Override public Option<Interval> apply(DublinCoreValue v) { final DCMIPeriod p = EncodingSchemeUtils.decodePeriod(v); return p != null ? some(Interval.fromValues(p.getStart(), p.getEnd())) : Misc.<Option<Interval>>chuck(new RuntimeException(v + " does not conform to W3C-DTF encoding scheme for periods")); } });
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()); } }