Skip to content

Commit

Permalink
Add files such as the fonts and brat js libraries to the server as lo…
Browse files Browse the repository at this point in the history
…cal files
  • Loading branch information
AngledLuffa committed Nov 7, 2024
1 parent 461db91 commit 5047ac1
Show file tree
Hide file tree
Showing 20 changed files with 4,844 additions and 14 deletions.
52 changes: 52 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 @@ -966,6 +995,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
setHttpExchangeResponseHeaders(httpExchange);

if (!this.contextRoot.equals(httpExchange.getRequestURI().getRawPath())) {
System.out.println("Can't find " + httpExchange.getRequestURI().getRawPath());
String response = "URI " + httpExchange.getRequestURI().getRawPath() + " not handled";
httpExchange.getResponseHeaders().add("Content-type", "text/plain");
httpExchange.sendResponseHeaders(HTTP_NOT_FOUND, response.length());
Expand Down Expand Up @@ -1756,9 +1786,31 @@ public void run(Optional<Pair<String,String>> basicAuth,
withAuth(server.createContext(uriContext+"/semgrex", new SemgrexHandler(authenticator, callback)), basicAuth);
withAuth(server.createContext(uriContext+"/tregex", new TregexHandler(authenticator, callback)), basicAuth);
withAuth(server.createContext(uriContext+"/scenegraph", new SceneGraphHandler(authenticator)), 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+"/style-vis.css", new FileHandler("edu/stanford/nlp/pipeline/demo/style-vis.css", "text/css")), 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+"/annotation_log.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/annotation_log.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/configuration.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/configuration.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/dispatcher.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/dispatcher.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/head.load.min.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/head.load.min.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/jquery.svg.min.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/jquery.svg.min.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/jquery.svgdom.min.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/jquery.svg.min.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/url_monitor.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/url_monitor.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/util.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/util.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/visualizer.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/visualizer.js", "application/javascript")), basicAuth);
withAuth(server.createContext(uriContext+"/webfont.js", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/webfont.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+"/img/loading.gif", new BytesFileHandler("edu/stanford/nlp/pipeline/demo/loading.gif", "image/gif")), 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.
42 changes: 42 additions & 0 deletions src/edu/stanford/nlp/pipeline/demo/annotation_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:

var AnnotationLog = (function(window, undefined) {
var AnnotationLog = function(dispatcher) {
var annotationLoggingOn = false;
var currentCollection = null;
var currentDocument = null;
var currentArguments = null;

var rememberLoggingState = function(response) {
annotationLoggingOn = response.annotation_logging;
};

var rememberCurrent = function(_collection, _document, _arguments) {
currentCollection = _collection;
currentDocument = _document;
currentArguments = _arguments;
};

var logAction = function(_action) {
if (!annotationLoggingOn) {
// logging not requested for current collection
return false;
} else {
dispatcher.post('ajax', [ {
action: 'logAnnotatorAction',
collection: currentCollection,
'document': currentDocument,
log: _action,
}, null]);
}
}

dispatcher.
on('collectionLoaded', rememberLoggingState).
on('current', rememberCurrent).
on('logAction', logAction);
}

return AnnotationLog;
})(window);
29 changes: 29 additions & 0 deletions src/edu/stanford/nlp/pipeline/demo/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
var Configuration = (function(window, undefined) {
var abbrevsOn = true;
var textBackgrounds = "striped";
var svgWidth = '100%';
var rapidModeOn = false;
var confirmModeOn = true;
var autorefreshOn = false;

var visual = {
margin: { x: 2, y: 1 },
arcTextMargin: 1,
boxSpacing: 1,
curlyHeight: 4,
arcSpacing: 9, //10;
arcStartHeight: 19, //23; //25;
}

return {
abbrevsOn: abbrevsOn,
textBackgrounds: textBackgrounds,
visual: visual,
svgWidth: svgWidth,
rapidModeOn: rapidModeOn,
confirmModeOn: confirmModeOn,
autorefreshOn: autorefreshOn,
};
})(window);
8 changes: 4 additions & 4 deletions src/edu/stanford/nlp/pipeline/demo/corenlp-brat.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<!-- Brat -->
<link rel="stylesheet" type="text/css" href="https://nlp.stanford.edu/js/brat/style-vis.css"/>
<script type="text/javascript" src="https://nlp.stanford.edu/js/brat/client/lib/head.load.min.js"></script>
<link rel="stylesheet" type="text/css" href="style-vis.css"/>
<script type="text/javascript" src="head.load.min.js"></script>
<!-- d3 -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dagre-d3/0.4.17/dagre-d3.min.js"></script>
Expand All @@ -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 Expand Up @@ -95,7 +95,7 @@

<!-- Loading gif -->
<div id="loading" class="row" style="display:none">
<img src="https://nlp.stanford.edu/img/loading.gif" height="100px" style="margin-left: 100px"/>
<img src="img/loading.gif" height="100px" style="margin-left: 100px"/>
</div>

<!-- Annotation population area -->
Expand Down
18 changes: 9 additions & 9 deletions src/edu/stanford/nlp/pipeline/demo/corenlp-brat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ var serverAddress = '';
var bratLocation = 'https://nlp.stanford.edu/js/brat/';
head.js(
// External libraries
bratLocation + '/client/lib/jquery.svg.min.js',
bratLocation + '/client/lib/jquery.svgdom.min.js',
'./jquery.svg.min.js',
'./jquery.svgdom.min.js',

// brat helper modules
bratLocation + '/client/src/configuration.js',
bratLocation + '/client/src/util.js',
bratLocation + '/client/src/annotation_log.js',
bratLocation + '/client/lib/webfont.js',
'./configuration.js',
'./util.js',
'./annotation_log.js',
'./webfont.js',

// brat modules
bratLocation + '/client/src/dispatcher.js',
bratLocation + '/client/src/url_monitor.js',
bratLocation + '/client/src/visualizer.js',
'./dispatcher.js',
'./url_monitor.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.
130 changes: 130 additions & 0 deletions src/edu/stanford/nlp/pipeline/demo/dispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
// TODO: does 'arguments.callee.caller' work?

var Dispatcher = (function($, window, undefined) {
var Dispatcher = function() {
var that = this;

var table = {};

var on = function(message, host, handler) {
if (handler === undefined) {
handler = host;
host = arguments.callee.caller;
}
if (table[message] === undefined) {
table[message] = [];
}
table[message].push([host, handler]);
return this;
};

// Notify listeners that we encountered an error in an asynch call
var inAsynchError = false; // To avoid error avalanches
var handleAsynchError = function(e) {
if (!inAsynchError) {
inAsynchError = true;
// TODO: Hook printout into dispatch elsewhere?
console.warn('Handled async error:', e);
that.post('dispatchAsynchError', [e]);
inAsynchError = false;
} else {
console.warn('Dropped asynch error:', e);
}
};

var post = function(asynch, message, args, returnType) {
if (typeof(asynch) !== 'number') {
// no asynch parameter
returnType = args;
args = message;
message = asynch;
asynch = null;
}
if (args === undefined) {
args = [];
}
var results = [];
// DEBUG: if (typeof(message) != "string" || !(message.match(/mouse/) || message == "hideComment")) console.log(message, args);

if (typeof(message) === 'function') {
// someone was lazy and sent a simple function
var host = arguments.callee.caller;
if (asynch !== null) {
result = setTimeout(function() {
try {
message.apply(host, args);
} catch(e) {
that.handleAsynchError(e);
}
}, asynch);
} else {
result = message.apply(host, args);
}
results.push(result);
} else {
// a proper message, propagate to all interested parties
var todo = table[message];
if (todo !== undefined) {
$.each(todo, function(itemNo, item) {
var result;
if (asynch !== null) {
result = setTimeout(function() {
try {
item[1].apply(item[0], args);
} catch (e) {
that.handleAsynchError(e);
}
}, asynch);
} else {
result = item[1].apply(item[0], args);
}
results.push(result);
});
/* DEBUG
} else {
console.warn('Message ' + message + ' has no subscribers.'); // DEBUG
*/
}
}
if (returnType == 'any') {
var i = results.length;
while (i--) {
if (results[i] !== false) return results[i];
}
return false;
}
if (returnType == 'all') {
var i = results.length;
while (i--) {
if (results[i] === false) return results[i];
}
}
return results;
};

var proxy = function(destination, message) {
this.on(message, function() {
destination.post(message, Array.prototype.slice.call(arguments));
});
};

var dispatcher = {
on: on,
post: post,
proxy: proxy,
};
Dispatcher.dispatchers.push(dispatcher);
return dispatcher;
};

Dispatcher.dispatchers = [];
Dispatcher.post = function(asynch, message, args, returnType) {
$.each(Dispatcher.dispatchers, function(dispatcherNo, dispatcher) {
dispatcher.post(asynch, message, args, returnType);
});
};

return Dispatcher;
})(jQuery, window);
8 changes: 8 additions & 0 deletions src/edu/stanford/nlp/pipeline/demo/head.load.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/edu/stanford/nlp/pipeline/demo/jquery.svg.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5047ac1

Please sign in to comment.