@@ -852,6 +852,35 @@ public void handle(HttpExchange httpExchange) throws IOException {
852
852
}
853
853
} // end static class FileHandler
854
854
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
+
855
884
private int maybeAlterStanfordTimeout (HttpExchange httpExchange , int timeoutMilliseconds ) {
856
885
if ( ! stanford ) {
857
886
return timeoutMilliseconds ;
@@ -1759,6 +1788,11 @@ public void run(Optional<Pair<String,String>> basicAuth,
1759
1788
withAuth (server .createContext (uriContext +"/corenlp-brat.js" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-brat.js" , "application/javascript" )), basicAuth );
1760
1789
withAuth (server .createContext (uriContext +"/corenlp-brat.cs" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-brat.css" , "text/css" )), basicAuth );
1761
1790
withAuth (server .createContext (uriContext +"/corenlp-parseviewer.js" , new FileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-parseviewer.js" , "application/javascript" )), basicAuth );
1791
+ withAuth (server .createContext (uriContext +"/static/fonts/Astloch-Bold.ttf" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/Astloch-Bold.ttf" , "font/ttfx" )), basicAuth );
1792
+ withAuth (server .createContext (uriContext +"/static/fonts/Liberation_Sans-Regular.ttf" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/LiberationSans-Regular.ttf" , "font/ttf" )), basicAuth );
1793
+ 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 );
1794
+ withAuth (server .createContext (uriContext +"/visualizer.js" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/visualizer.js" , "application/javascript" )), basicAuth );
1795
+ withAuth (server .createContext (uriContext +"/img/corenlp-title.png" , new BytesFileHandler ("edu/stanford/nlp/pipeline/demo/corenlp-title.png" , "image/png" )), basicAuth );
1762
1796
withAuth (server .createContext (uriContext +"/ping" , new PingHandler ()), Optional .empty ());
1763
1797
withAuth (server .createContext (uriContext +"/shutdown" , new ShutdownHandler ()), basicAuth );
1764
1798
if (this .serverPort == this .statusPort ) {
0 commit comments