This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.coffee
339 lines (305 loc) · 10.9 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# logging
log = require('./lib/logging').defaultLogger
pubres = require './lib/pubres'
NS = require './lib/namespaces'
RdfUtil = require('./lib/rdfUtil')
Yaml = require 'js-yaml'
N3 = require 'n3'
jsonld = require 'jsonld'
_extend = require('util')._extend
async = require 'async'
csv = require 'csv'
fs = require 'fs'
url = require 'url'
jade = require 'jade'
{ MongoClient } = require 'mongodb'
doi2urnMapping = {}
readCsvLinks = ->
return
file_links = null
file_links = fs.readFileSync("infolis-links.csv")
links = csv.parse(delimiter: "|")
cur = 0
links = null
links.on "readable", ->
while record = links.read()
cur += 1
id_a = record[1]
id_b = record[5]
[[id_a, id_b], [id_b, id_a]].forEach (pair) ->
if pair[0] of doi2urnMapping
doi2urnMapping[pair[0]].push pair[1]
else
doi2urnMapping[pair[0]] = [pair[1]]
# log.info("Loading link #"+cur) if cur % 5000 is 0
links.write file_links
links.end()
_matches_to_objects = (matches) ->
ret = []
for id in matches
ret.push {
"@type": "MatchResult",
"accuracy": Math.random(),
"id": id
}
ret
_content_type_from_headers = (headers) ->
acc = headers.accept
if acc.indexOf(',') > -1
acc = acc.substring 0, acc.indexOf(',')
ct_default = "application/json"
if acc is null
return ct_default
if acc is "*/*"
return "text/html"
else
return acc
_findPdfLink = (doc) ->
if doc.zotero
for att in doc.zotero[0].attachments
if att.mimeType is 'application/pdf'
return att.url
return ""
handle_ids_for_id = (req, res) ->
needle = req.query.id
if not needle
return next(message : "Missing 'id' parameter.")
matches_raw = doi2urnMapping[needle] or []
res.status = if matches_raw.length > 0 then 200 else 404
ret = {
"@context": {
"needle": {
"@id": "http://onto.dm2e.eu/omnom/parameterValue",
"@type": "@id"
},
"accuracy": {
"@id": "http://rs.tdwg.org/dwc/terms/measurementAccuracy",
"@type": "http://www.w3.org/2001/XMLSchema#float",
},
"id": {
"@id": "http://foo.bar/id",
"@type": "@id"
}
},
"@id": req.url,
"@type": "MatchResultList",
"needle": needle,
"skos:broadMatch": _matches_to_objects matches_raw
}
res.json ret
handle_context = (req, res, next) ->
# namespaces
context = NS.toJSONLD()
return json.send context
handle_vocab_gesis = (req, res, next) ->
try
gesisContext = Yaml.safeLoad(fs.readFileSync('./views/gesis-context.ld.yaml', 'utf8'))
catch e
return next {status: 500, message: "Couldn't load ontology", body: e}
context = NS.toJSONLD()
joinedContext = _extend(context, gesisContext)
res.status 200
res.set "Content-Type": "application/ld+json"
res.json joinedContext
handle_vocab = (req, res, next) ->
# the vocabulary
# rawVocab = CSON.load './views/infolis-vocabulary.csonld'
try
rawVocab = Yaml.safeLoad(fs.readFileSync('./views/infolis-vocabulary.ld.yaml', 'utf8'))
catch e
return next {status: 500, message: "Couldn't load ontology", body: e}
# namespaces
context = NS.toJSONLD()
# JSON-LD presentation mode
profile = req.query.jsonld_profile || 'compact'
if req.params.term
[ filteredVocab ] = rawVocab['@graph'].filter (doc) ->
doc['@id'] is 'infolis:' + req.params.term or
doc['@id'] is NS.infolis + req.params.term
if not filteredVocab
return next {message: "No such term '#{req.params.term}'", status: 404}
else
filteredVocab['@context'] = rawVocab['@context']
rawVocab = filteredVocab
sendRDF = (errJsonLD, data, contentType) ->
return next {message: "JSON-LD Error", body: errJsonLD} if errJsonLD
if contentType is "application/nquads"
console.info "Nothing to do, data is in the right format"
res.status 200
res.set 'Content-Type': contentType
return res.send data
else
RdfUtil.convertN3Sync data, {format: contentType, prefixes: NS.toJSON()}, (errN3, result) ->
if errN3
return next {status: 500, message: "N3 error", body: errN3}
res.status 200
res.set 'Content-Type': contentType
return res.send result
sendJSONLD = (err, data) ->
return next {message: "JSON-LD Error", body: err} if err
res.status 200
res.set 'Content-Type': 'application/ld+json'
res.json data
sendHTML = (err, data) ->
return next {message: "JSON-LD Error", body: err} if err
res.status 200
res.set 'Content-Type': 'text/html'
res.render 'debug-output', data: data
res.header "Link": "<#{NS.infolis}>" + '; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"'
res.format
json: () ->
# Send a Header like
# Accept: application/ld+json; q=1, profile="http://www.w3.org/ns/json-ld#flattened"
if req.header('Accept').indexOf('profile') > -1
requestedProfile = req.header('Accept').match /profile=\"([^"]+)\"/
if not requestedProfile or not requestedProfile[1]
return next { status: 400, message: "Unparseable 'profile' accept-param" }
switch requestedProfile[1]
when 'http://www.w3.org/ns/json-ld#flattened' then profile = 'flatten'
when 'http://www.w3.org/ns/json-ld#compacted' then profile = 'compact'
when 'http://www.w3.org/ns/json-ld#expanded' then profile = 'expand'
else return next { status: 406, message: "'profile' accept-param must be from the list" }
if profile is "expand" then jsonld.expand rawVocab, {expandContext: context}, sendJSONLD
else jsonld[profile] rawVocab, context, sendJSONLD
html: () ->
if profile is "expand" then jsonld.expand rawVocab, {expandContext: context}, sendHTML
else jsonld[profile] rawVocab, context, sendHTML
"text/n3": () ->
return jsonld.toRDF rawVocab, {expandContext: context, format: "application/nquads"}, (err,data) ->
sendRDF err, data, "text/n3"
"application/nquads": () ->
return jsonld.toRDF rawVocab, {expandContext: context, format: "application/nquads"}, (err, data) ->
sendRDF err, data, "application/nquads"
"application/trig": () ->
return jsonld.toRDF rawVocab, {expandContext: context, format: "application/nquads"}, (err, data) ->
sendRDF err, data, "application/trig"
"text/turtle": () ->
return jsonld.toRDF rawVocab, {expandContext: context, format: "application/nquads"}, (err, data) ->
sendRDF err, data, "text/turtle"
default: () ->
res.status(406)
return res.end()
handle_default = (req, res, next) ->
res.render 'index'
handle_doi_info = (req, res, next) ->
db = req.mongoDB
needle = req.query.doi
if not needle
return next(message : "Missing 'doi' parameter.")
# A trailing slash is most probably an error
needle = needle.replace(/\/$/, '')
# Use the DOI as _id in Mongo, replace all non-alnum chars with _
mongoId = needle.replace(/[^a-zA-Z0-9]/g, "_")
# collection to write to
collection = db.collection('doi_info')
# Send the result to the client using conneg
sendResults = (doc) ->
res.status 200
res.format
default: () ->
res.json doc
html: () ->
res.status 303
res.render 'debug-output', data: doc
"application/pdf": () ->
res.location(_findPdfLink(doc))
res.end()
# log.info "Searching for #{needle}, stored as #{mongoId} in db.doi_info"
collection.findOne {_id: mongoId}, (mongoErr, mongoResult) ->
if mongoErr
return next(mongoErr)
# TODO don't cache for testing
if mongoResult and not req.query.force
return sendResults(mongoResult)
crossRef = pubres.CrossRef()
zotero = pubres.Zotero()
googleSearch = pubres.GoogleSearch({ resultsPerPage: 1 })
async.waterfall [
(callback) ->
# log.info "STEP 1"
async.parallel {
# # Ask for the agency
# crossRefAgency: (callback) -> crossRef.getAgencyForDOI needle, callback
# # Ask for metadata
crossRefWorks: (callback) -> crossRef.getBibliographicMetadataForDOI needle, callback
# # Resolve the DOI and query Zotero web service for more information on this
zotero: (callback) -> zotero.scrapeDOI needle, callback
}, (err, thisStepResults) ->
# callback err, thisStepResults
callback null, thisStepResults
# (previousStepResults, callback) ->
# log.info "STEP 2"
# call
# if previousStepResults.crossRefWorks and
# not previousStepResults.crossRefWorks.attachments and
# previousStepResults.crossRefWorks.title
# googleSearch.searchPDF previousStepResults.crossRefWorks.title, (err, googleResult) ->
# callback null, _extend(previousStepResults, {googleSearch:googleResult})
], (err, results) ->
# if err
# return sendError(res, "" + JSON.stringify(err)) if err
boilerplate =
_id: mongoId
doi: needle
updated: new Date().toISOString()
results = _extend(boilerplate, results)
collection.save results, (mongoErr, mongoResult) ->
return sendError(res, ""+ mongoErr) if mongoErr
res.json(results)
startServer = (db) ->
express = require 'express'
app = express()
# Inject reference to MongoDB middleware
mongoMiddleware = (req, res, next) ->
req.mongoDB = db
next()
# CORS middleware (required for AJAX requests from Primo)
corsMiddleware = (req, res, next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, exlrequesttype'
next()
# Error handler
errorHandler = (err, req, res, next) ->
log.error err
res.status err.status || 400
res.format
json: () ->
res.set "Content-Type": "application/problem+json"
res.json err
html: () ->
res.set "Content-Type": "text/html"
res.render 'error', message: err.message, body: err.body
default: () ->
res.set "Content-Type": "text/plain"
res.set "X-Error-Message": err.message
res.send JSON.stringify err
# Templates
app.set 'views', './views'
app.set 'view engine', 'jade'
app.use mongoMiddleware
app.use corsMiddleware
###
# set up routes
# NOTE: Must be defined before error handling middleware but after data-injection middleware
###
app.get '/ids-for-id', handle_ids_for_id
app.get '/doi-info', handle_doi_info
app.get '/context/gesis', handle_vocab_gesis
app.get '/context', handle_vocab
app.get '/vocab', handle_vocab
app.get '/vocab/:term', handle_vocab
app.get '/', handle_default
app.use errorHandler
# Run the server
http = require 'http'
server = http.createServer app
server.listen 3000
MongoClient.connect 'mongodb://localhost:27017/infolis', (mongoErr, db) ->
log.info "Loading links"
readCsvLinks()
log.info "Starting server"
if (mongoErr)
log.info "Couldn't connect to MongoDB"
log.info mongoErr
return
startServer(db)