Skip to content

Commit 5047ac1

Browse files
committed
Add files such as the fonts and brat js libraries to the server as local files
1 parent 461db91 commit 5047ac1

20 files changed

+4844
-14
lines changed

src/edu/stanford/nlp/pipeline/StanfordCoreNLPServer.java

+52
Original file line numberDiff line numberDiff line change
@@ -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) {
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
2+
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
3+
4+
var AnnotationLog = (function(window, undefined) {
5+
var AnnotationLog = function(dispatcher) {
6+
var annotationLoggingOn = false;
7+
var currentCollection = null;
8+
var currentDocument = null;
9+
var currentArguments = null;
10+
11+
var rememberLoggingState = function(response) {
12+
annotationLoggingOn = response.annotation_logging;
13+
};
14+
15+
var rememberCurrent = function(_collection, _document, _arguments) {
16+
currentCollection = _collection;
17+
currentDocument = _document;
18+
currentArguments = _arguments;
19+
};
20+
21+
var logAction = function(_action) {
22+
if (!annotationLoggingOn) {
23+
// logging not requested for current collection
24+
return false;
25+
} else {
26+
dispatcher.post('ajax', [ {
27+
action: 'logAnnotatorAction',
28+
collection: currentCollection,
29+
'document': currentDocument,
30+
log: _action,
31+
}, null]);
32+
}
33+
}
34+
35+
dispatcher.
36+
on('collectionLoaded', rememberLoggingState).
37+
on('current', rememberCurrent).
38+
on('logAction', logAction);
39+
}
40+
41+
return AnnotationLog;
42+
})(window);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
2+
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
3+
var Configuration = (function(window, undefined) {
4+
var abbrevsOn = true;
5+
var textBackgrounds = "striped";
6+
var svgWidth = '100%';
7+
var rapidModeOn = false;
8+
var confirmModeOn = true;
9+
var autorefreshOn = false;
10+
11+
var visual = {
12+
margin: { x: 2, y: 1 },
13+
arcTextMargin: 1,
14+
boxSpacing: 1,
15+
curlyHeight: 4,
16+
arcSpacing: 9, //10;
17+
arcStartHeight: 19, //23; //25;
18+
}
19+
20+
return {
21+
abbrevsOn: abbrevsOn,
22+
textBackgrounds: textBackgrounds,
23+
visual: visual,
24+
svgWidth: svgWidth,
25+
rapidModeOn: rapidModeOn,
26+
confirmModeOn: confirmModeOn,
27+
autorefreshOn: autorefreshOn,
28+
};
29+
})(window);

src/edu/stanford/nlp/pipeline/demo/corenlp-brat.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css"/>
1010
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
1111
<!-- Brat -->
12-
<link rel="stylesheet" type="text/css" href="https://nlp.stanford.edu/js/brat/style-vis.css"/>
13-
<script type="text/javascript" src="https://nlp.stanford.edu/js/brat/client/lib/head.load.min.js"></script>
12+
<link rel="stylesheet" type="text/css" href="style-vis.css"/>
13+
<script type="text/javascript" src="head.load.min.js"></script>
1414
<!-- d3 -->
1515
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
1616
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dagre-d3/0.4.17/dagre-d3.min.js"></script>
@@ -29,7 +29,7 @@
2929
<div class="container">
3030
<div id="logo">
3131
<a href="https://stanfordnlp.github.io/CoreNLP/">
32-
<img id="logo-image" src="https://nlp.stanford.edu/img/corenlp-title.png">
32+
<img id="logo-image" src="img/corenlp-title.png">
3333
</a>
3434
</div>
3535
<div id="version-num">version 4.5.8<br><br><br></div>
@@ -95,7 +95,7 @@
9595

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

101101
<!-- Annotation population area -->

src/edu/stanford/nlp/pipeline/demo/corenlp-brat.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ var serverAddress = '';
88
var bratLocation = 'https://nlp.stanford.edu/js/brat/';
99
head.js(
1010
// External libraries
11-
bratLocation + '/client/lib/jquery.svg.min.js',
12-
bratLocation + '/client/lib/jquery.svgdom.min.js',
11+
'./jquery.svg.min.js',
12+
'./jquery.svgdom.min.js',
1313

1414
// brat helper modules
15-
bratLocation + '/client/src/configuration.js',
16-
bratLocation + '/client/src/util.js',
17-
bratLocation + '/client/src/annotation_log.js',
18-
bratLocation + '/client/lib/webfont.js',
15+
'./configuration.js',
16+
'./util.js',
17+
'./annotation_log.js',
18+
'./webfont.js',
1919

2020
// brat modules
21-
bratLocation + '/client/src/dispatcher.js',
22-
bratLocation + '/client/src/url_monitor.js',
23-
bratLocation + '/client/src/visualizer.js',
21+
'./dispatcher.js',
22+
'./url_monitor.js',
23+
'./visualizer.js',
2424

2525
// parse viewer
2626
'./corenlp-parseviewer.js'
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
2+
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
3+
// TODO: does 'arguments.callee.caller' work?
4+
5+
var Dispatcher = (function($, window, undefined) {
6+
var Dispatcher = function() {
7+
var that = this;
8+
9+
var table = {};
10+
11+
var on = function(message, host, handler) {
12+
if (handler === undefined) {
13+
handler = host;
14+
host = arguments.callee.caller;
15+
}
16+
if (table[message] === undefined) {
17+
table[message] = [];
18+
}
19+
table[message].push([host, handler]);
20+
return this;
21+
};
22+
23+
// Notify listeners that we encountered an error in an asynch call
24+
var inAsynchError = false; // To avoid error avalanches
25+
var handleAsynchError = function(e) {
26+
if (!inAsynchError) {
27+
inAsynchError = true;
28+
// TODO: Hook printout into dispatch elsewhere?
29+
console.warn('Handled async error:', e);
30+
that.post('dispatchAsynchError', [e]);
31+
inAsynchError = false;
32+
} else {
33+
console.warn('Dropped asynch error:', e);
34+
}
35+
};
36+
37+
var post = function(asynch, message, args, returnType) {
38+
if (typeof(asynch) !== 'number') {
39+
// no asynch parameter
40+
returnType = args;
41+
args = message;
42+
message = asynch;
43+
asynch = null;
44+
}
45+
if (args === undefined) {
46+
args = [];
47+
}
48+
var results = [];
49+
// DEBUG: if (typeof(message) != "string" || !(message.match(/mouse/) || message == "hideComment")) console.log(message, args);
50+
51+
if (typeof(message) === 'function') {
52+
// someone was lazy and sent a simple function
53+
var host = arguments.callee.caller;
54+
if (asynch !== null) {
55+
result = setTimeout(function() {
56+
try {
57+
message.apply(host, args);
58+
} catch(e) {
59+
that.handleAsynchError(e);
60+
}
61+
}, asynch);
62+
} else {
63+
result = message.apply(host, args);
64+
}
65+
results.push(result);
66+
} else {
67+
// a proper message, propagate to all interested parties
68+
var todo = table[message];
69+
if (todo !== undefined) {
70+
$.each(todo, function(itemNo, item) {
71+
var result;
72+
if (asynch !== null) {
73+
result = setTimeout(function() {
74+
try {
75+
item[1].apply(item[0], args);
76+
} catch (e) {
77+
that.handleAsynchError(e);
78+
}
79+
}, asynch);
80+
} else {
81+
result = item[1].apply(item[0], args);
82+
}
83+
results.push(result);
84+
});
85+
/* DEBUG
86+
} else {
87+
console.warn('Message ' + message + ' has no subscribers.'); // DEBUG
88+
*/
89+
}
90+
}
91+
if (returnType == 'any') {
92+
var i = results.length;
93+
while (i--) {
94+
if (results[i] !== false) return results[i];
95+
}
96+
return false;
97+
}
98+
if (returnType == 'all') {
99+
var i = results.length;
100+
while (i--) {
101+
if (results[i] === false) return results[i];
102+
}
103+
}
104+
return results;
105+
};
106+
107+
var proxy = function(destination, message) {
108+
this.on(message, function() {
109+
destination.post(message, Array.prototype.slice.call(arguments));
110+
});
111+
};
112+
113+
var dispatcher = {
114+
on: on,
115+
post: post,
116+
proxy: proxy,
117+
};
118+
Dispatcher.dispatchers.push(dispatcher);
119+
return dispatcher;
120+
};
121+
122+
Dispatcher.dispatchers = [];
123+
Dispatcher.post = function(asynch, message, args, returnType) {
124+
$.each(Dispatcher.dispatchers, function(dispatcherNo, dispatcher) {
125+
dispatcher.post(asynch, message, args, returnType);
126+
});
127+
};
128+
129+
return Dispatcher;
130+
})(jQuery, window);

src/edu/stanford/nlp/pipeline/demo/head.load.min.js

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/edu/stanford/nlp/pipeline/demo/jquery.svg.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)