/** Answer an OutputStreamWriter that encodes in UTF-8. */ public static OutputStreamWriter asUTF8( OutputStream os ) { try { return new OutputStreamWriter( os, "UTF-8" ); } catch (UnsupportedEncodingException e) { throw new WrappedException( e ); } }
public static String toString(ByteArrayOutputStream os) { try { return os.toString("UTF-8"); } catch (IOException e) { throw new WrappedException( e ); } } }
private static URLforResource pathAsURLFactory(final ServletContext servCon) { return new URLforResource() { @Override public URL asResourceURL(String ePath) { try { if (ePath == null) throw new RuntimeException("problem: resource path in asResourceURL is null."); URL url = ePath.matches(STARTS_WITH_SCHEME) ? new URL(ePath) : ePath.startsWith("/") ? pathToURL(ePath) : pathToURL(getRealPath(servCon, "/" + ePath)) ; if (log.isDebugEnabled()) log.debug("mapped ePath '{}' to '{}'", ePath, url); return url; } catch (MalformedURLException e) { throw new WrappedException(e); } } private String getRealPath(final ServletContext servCon, String ePath) { String realPath = servCon.getRealPath(ePath); if (realPath == null) throw new RuntimeException("problem: real path for " + ePath + " is null."); return realPath; } }; }
/** Answer a new instance of the given class. */ public static <T> T newInstanceOf( Class<T> c ) { try { return c.newInstance(); } catch (Exception e) { throw new WrappedException( e ); } }
/** Flush an OutputStream. Rethrow IO exceptions, wrapped. */ public static void flush(OutputStream os) { try { os.flush(); } catch (IOException e) { throw new WrappedException( e ); } }
private static DocumentBuilder getBuilder() { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new WrappedException(e); } }
public static void register(String name, Object bean) { try { MBeanServer ms = ManagementFactory.getPlatformMBeanServer(); ObjectName on = new ObjectName( name ); ms.registerMBean( bean, on ); } catch (Exception e) { throw new WrappedException( e ); } }
private static Properties readProperties(FileManager fm, String fileName) { String wholeFile = fm.readWholeFileAsUTF8( fileName ); Properties p = new Properties(); try { p.load(new StringReader(wholeFile)); } catch (IOException e) { throw new WrappedException(e); } return p; }
/** Write the content UTF-8 encoded down the given output stream. */ public static void writeAsUTF8(final String content, OutputStream os) { OutputStreamWriter u = StreamUtils.asUTF8(os); try { u.write(content); u.flush(); u.close(); } catch (IOException e) { throw new WrappedException(e); } }
public static String renderNodeToString(Times times, Node d, PrefixMapping pm) { Transformer t = setPropertiesAndParams(times, new Bindings(), pm, null); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); try { t.transform(new DOMSource(d), sr); } catch (TransformerException e) { throw new WrappedException(e); } return sw.toString(); }
@Override public URL asResourceURL(String ePath) { try { if (ePath == null) throw new RuntimeException("problem: resource path in asResourceURL is null."); URL url = ePath.matches(STARTS_WITH_SCHEME) ? new URL(ePath) : ePath.startsWith("/") ? pathToURL(ePath) : pathToURL(getRealPath(servCon, "/" + ePath)) ; if (log.isDebugEnabled()) log.debug("mapped ePath '{}' to '{}'", ePath, url); return url; } catch (MalformedURLException e) { throw new WrappedException(e); } }
private static Transformer getTransformer(Times times, Bindings rc, String transformFilePath) { try { TransformerFactory tf = TransformerFactory.newInstance(); if (transformFilePath == null) return tf.newTransformer(); else synchronized (DOMUtils.class) { URL u = rc.pathAsURL(Bindings.expandVariables(rc, transformFilePath)); Templates t = cache.get(u); if (t == null) { long origin = System.currentTimeMillis(); t = tf.newTemplates(new StreamSource(u.toExternalForm())); long after = System.currentTimeMillis(); times.setStylesheetCompileTime(after - origin); cache.put(u, t); } return t.newTransformer(); } } catch (TransformerConfigurationException e) { throw new WrappedException(e.getMessage() + " [" + transformFilePath + "]", e); } }
u.close(); } catch (TransformerException e) { throw new WrappedException(e); } catch (IOException e) { throw new WrappedException(e);
@Override public Renderer.BytesOut render( Times t, Bindings ignored, Map<String, String> termBindings, final APIResultSet results ) { ByteArrayOutputStream os = new ByteArrayOutputStream(); StripPrefixes.Do(results.getMergedModel()).write( os, "TTL" ); try { os.flush(); } catch (IOException e) { throw new WrappedException( e ); } final String content = UTF8.toString( os ); return new BytesOutTimed() { @Override public void writeAll(OutputStream os) { StreamUtils.writeAsUTF8(content, os); } @Override protected String getFormat() { return "ttl"; } @Override public String getPoison() { return TURTLE_POISON; } }; }
@Override protected void writeAll(OutputStream os) { try { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream s = (checkRoundTrip ? bytesOut : os); Writer w = new OutputStreamWriter(s, "UTF-8"); JSONWriterFacade jw = new JSONWriterWrapper(w, true); JSONLDComposer c = new JSONLDComposer(model, root, context, termBindings, allStructured, jw); c.renderItems(results.getResultList()); w.flush(); if (checkRoundTrip) { byte[] bytes = bytesOut.toByteArray(); os.write(bytes); log.info("checking that JSON LD result round-trips"); boolean ok = new JSONLDRoundtrip().check(model, objectModel, bytes); log.info(" -- {}", ok); } } catch (Throwable e) { throw new WrappedException(e); } }
/** Return the dynamic reload interval for this (contextPath). If refreshIntervalTestingMillis is positive, that is the reload interval. Otherwise if there's a value in /etc/elda/conf.d/{contextPath}/delay.int then that is the value. Otherwise it is the value of TimestampedRouter.DEFAULT_INTERVAL. */ private static long getRefreshInterval(String contextPath) { long delay = refreshIntervalTestingMillis; if (delay == 0) { delay = TimestampedRouter.DEFAULT_INTERVAL; String intervalFileName = "/etc/elda/conf.d/" + contextPath + "/delay.int"; InputStream is = EldaFileManager.get().open( intervalFileName ); if (is != null) { String t = EldaFileManager.get().readWholeFileAsUTF8(is); try { is.close(); } catch (IOException e) { throw new WrappedException( e ); } long n = t.startsWith("FOREVER") ? TimestampedRouter.forever : Long.parseLong(t.replace("\n", "" )) ; if (n > 0) delay = n; } } log.info("reload check interval for '{}' is '{}'", contextPath, delay); return delay; }
throw new WrappedException( e );
public static String pullString( BytesOut rbo ) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); rbo.writeAll( new Times(), bos ); try { return bos.toString( "UTF-8" ); } catch (UnsupportedEncodingException e) { throw new WrappedException( e ); } }
throw new WrappedException( e );
private void renderFeed( OutputStream os, APIResultSet results, Times t, Map<String, String> termBindings, Bindings b ) { final PrefixMapping pm = results.getModelPrefixes(); Document d = DOMUtils.newDocument(); FeedResults feedResults = new FeedResults ( results.getRoot(), results.getResultList(), results.getModels() ); renderFeedIntoDocument( d, termBindings, feedResults ); // Transformer tr = DOMUtils.setPropertiesAndParams( t, b, pm, null ); OutputStreamWriter u = StreamUtils.asUTF8( os ); StreamResult sr = new StreamResult( u ); try { tr.transform( new DOMSource( d ), sr ); } catch (TransformerException e) { throw new WrappedException( e ); } }