@@ -852,6 +852,35 @@ public void handle(HttpExchange httpExchange) throws IOException {
852852 }
853853 } // end static class FileHandler
854854
855+ /**
856+ * Serve a content file (image, font, etc) from the filesystem or classpath
857+ */
858+ public static class BytesFileHandler implements HttpHandler {
859+ private final byte [] content ;
860+ private final String contentType ;
861+ public BytesFileHandler (String fileOrClasspath , String contentType ) throws IOException {
862+ try (InputStream is = IOUtils .getInputStreamFromURLOrClasspathOrFileSystem (fileOrClasspath )) {
863+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
864+ int available = is .available ();
865+ while (available > 0 ) {
866+ byte next [] = new byte [available ];
867+ is .read (next );
868+ bos .write (next );
869+ available = is .available ();
870+ }
871+ this .content = bos .toByteArray ();
872+ }
873+ this .contentType = contentType + "; charset=utf-8" ; // always encode in utf-8
874+ }
875+ @ Override
876+ public void handle (HttpExchange httpExchange ) throws IOException {
877+ httpExchange .getResponseHeaders ().set ("Content-type" , this .contentType );
878+ httpExchange .sendResponseHeaders (HTTP_OK , content .length );
879+ httpExchange .getResponseBody ().write (content );
880+ httpExchange .close ();
881+ }
882+ } // end static class FileHandler
883+
855884 private int maybeAlterStanfordTimeout (HttpExchange httpExchange , int timeoutMilliseconds ) {
856885 if ( ! stanford ) {
857886 return timeoutMilliseconds ;
@@ -966,6 +995,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
966995 setHttpExchangeResponseHeaders (httpExchange );
967996
968997 if (!this .contextRoot .equals (httpExchange .getRequestURI ().getRawPath ())) {
998+ System .out .println ("Can't find " + httpExchange .getRequestURI ().getRawPath ());
969999 String response = "URI " + httpExchange .getRequestURI ().getRawPath () + " not handled" ;
9701000 httpExchange .getResponseHeaders ().add ("Content-type" , "text/plain" );
9711001 httpExchange .sendResponseHeaders (HTTP_NOT_FOUND , response .length ());
@@ -1756,9 +1786,31 @@ public void run(Optional<Pair<String,String>> basicAuth,
17561786 withAuth (server .createContext (uriContext +"/semgrex" , new SemgrexHandler (authenticator , callback )), basicAuth );
17571787 withAuth (server .createContext (uriContext +"/tregex" , new TregexHandler (authenticator , callback )), basicAuth );
17581788 withAuth (server .createContext (uriContext +"/scenegraph" , new SceneGraphHandler (authenticator )), basicAuth );
1789+
17591790 withAuth (server .createContext (uriContext +"/corenlp-brat.js" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-brat.js" , "application/javascript" )), basicAuth );
17601791 withAuth (server .createContext (uriContext +"/corenlp-brat.cs" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-brat.css" , "text/css" )), basicAuth );
17611792 withAuth (server .createContext (uriContext +"/corenlp-parseviewer.js" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-parseviewer.js" , "application/javascript" )), basicAuth );
1793+
1794+ withAuth (server .createContext (uriContext +"/style-vis.css" , new FileHandler ("edu/stanford/nlp/pipeline/demo/style-vis.css" , "text/css" )), basicAuth );
1795+
1796+ withAuth (server .createContext (uriContext +"/static/fonts/Astloch-Bold.ttf" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/Astloch-Bold.ttf" , "font/ttfx" )), basicAuth );
1797+ withAuth (server .createContext (uriContext +"/static/fonts/Liberation_Sans-Regular.ttf" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/LiberationSans-Regular.ttf" , "font/ttf" )), basicAuth );
1798+ withAuth (server .createContext (uriContext +"/static/fonts/PT_Sans-Caption-Web-Regular.ttf" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/PTSansCaption-Regular.ttf" , "font/ttf" )), basicAuth );
1799+
1800+ withAuth (server .createContext (uriContext +"/annotation_log.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/annotation_log.js" , "application/javascript" )), basicAuth );
1801+ withAuth (server .createContext (uriContext +"/configuration.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/configuration.js" , "application/javascript" )), basicAuth );
1802+ withAuth (server .createContext (uriContext +"/dispatcher.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/dispatcher.js" , "application/javascript" )), basicAuth );
1803+ withAuth (server .createContext (uriContext +"/head.load.min.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/head.load.min.js" , "application/javascript" )), basicAuth );
1804+ withAuth (server .createContext (uriContext +"/jquery.svg.min.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/jquery.svg.min.js" , "application/javascript" )), basicAuth );
1805+ withAuth (server .createContext (uriContext +"/jquery.svgdom.min.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/jquery.svg.min.js" , "application/javascript" )), basicAuth );
1806+ withAuth (server .createContext (uriContext +"/url_monitor.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/url_monitor.js" , "application/javascript" )), basicAuth );
1807+ withAuth (server .createContext (uriContext +"/util.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/util.js" , "application/javascript" )), basicAuth );
1808+ withAuth (server .createContext (uriContext +"/visualizer.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/visualizer.js" , "application/javascript" )), basicAuth );
1809+ withAuth (server .createContext (uriContext +"/webfont.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/webfont.js" , "application/javascript" )), basicAuth );
1810+
1811+ withAuth (server .createContext (uriContext +"/img/corenlp-title.png" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-title.png" , "image/png" )), basicAuth );
1812+ withAuth (server .createContext (uriContext +"/img/loading.gif" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/loading.gif" , "image/gif" )), basicAuth );
1813+
17621814 withAuth (server .createContext (uriContext +"/ping" , new PingHandler ()), Optional .empty ());
17631815 withAuth (server .createContext (uriContext +"/shutdown" , new ShutdownHandler ()), basicAuth );
17641816 if (this .serverPort == this .statusPort ) {
0 commit comments