Skip to content

Commit

Permalink
Start adding files such as the fonts to the server as local files
Browse files Browse the repository at this point in the history
  • Loading branch information
AngledLuffa committed Nov 7, 2024
1 parent 461db91 commit f55cdf8
Show file tree
Hide file tree
Showing 9 changed files with 3,175 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/edu/stanford/nlp/pipeline/StanfordCoreNLPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,35 @@ public void handle(HttpExchange httpExchange) throws IOException {
}
} // end static class FileHandler

/**
* Serve a content file (image, font, etc) from the filesystem or classpath
*/
public static class BytesFileHandler implements HttpHandler {
private final byte[] content;
private final String contentType;
public BytesFileHandler(String fileOrClasspath, String contentType) throws IOException {
try (InputStream is = IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(fileOrClasspath)) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int available = is.available();
while (available > 0) {
byte next[] = new byte[available];
is.read(next);
bos.write(next);
available = is.available();
}
this.content = bos.toByteArray();
}
this.contentType = contentType + "; charset=utf-8"; // always encode in utf-8
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
httpExchange.getResponseHeaders().set("Content-type", this.contentType);
httpExchange.sendResponseHeaders(HTTP_OK, content.length);
httpExchange.getResponseBody().write(content);
httpExchange.close();
}
} // end static class FileHandler

private int maybeAlterStanfordTimeout(HttpExchange httpExchange, int timeoutMilliseconds) {
if ( ! stanford) {
return timeoutMilliseconds;
Expand Down Expand Up @@ -1759,6 +1788,11 @@ public void run(Optional<Pair<String,String>> basicAuth,
withAuth(server.createContext(uriContext+"/corenlp-brat.js", new FileHandler("edu/stanford/nlp/pipeline/demo/corenlp-brat.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/corenlp-brat.cs", new FileHandler("edu/stanford/nlp/pipeline/demo/corenlp-brat.css", "text/css")), basicAuth);
withAuth(server.createContext(uriContext+"/corenlp-parseviewer.js", new FileHandler("edu/stanford/nlp/pipeline/demo/corenlp-parseviewer.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/static/fonts/Astloch-Bold.ttf", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/Astloch-Bold.ttf", "font/ttfx")), basicAuth);
withAuth(server.createContext(uriContext+"/static/fonts/Liberation_Sans-Regular.ttf", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/LiberationSans-Regular.ttf", "font/ttf")), basicAuth);
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);
withAuth(server.createContext(uriContext+"/visualizer.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/visualizer.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/img/corenlp-title.png", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/corenlp-title.png", "image/png")), basicAuth);
withAuth(server.createContext(uriContext+"/ping", new PingHandler()), Optional.empty());
withAuth(server.createContext(uriContext+"/shutdown", new ShutdownHandler()), basicAuth);
if (this.serverPort == this.statusPort) {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/pipeline/demo/corenlp-brat.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="container">
<div id="logo">
<a href="https://stanfordnlp.github.io/CoreNLP/">
<img id="logo-image" src="https://nlp.stanford.edu/img/corenlp-title.png">
<img id="logo-image" src="img/corenlp-title.png">
</a>
</div>
<div id="version-num">version 4.5.8<br><br><br></div>
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/pipeline/demo/corenlp-brat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ head.js(
// brat modules
bratLocation + '/client/src/dispatcher.js',
bratLocation + '/client/src/url_monitor.js',
bratLocation + '/client/src/visualizer.js',
'./visualizer.js',

// parse viewer
'./corenlp-parseviewer.js'
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/edu/stanford/nlp/pipeline/demo/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f55cdf8

Please sign in to comment.