This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
413 lines (358 loc) · 13.5 KB
/
main.go
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2019 Carleton University Library All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/cu-library/overridefromenv"
)
const (
// EnvPrefix is the prefix for the environment variables.
EnvPrefix string = "PERMANENTDETOUR_"
// DefaultAddress is the default address to serve from.
DefaultAddress string = ":8877"
// PrimoDomain is the domain at which Primo instances are hosted.
PrimoDomain string = "primo.exlibrisgroup.com"
// MaxMappingFileLength is the maximum number of lines in a mapping file.
MaxMappingFileLength uint64 = 1000000
// RecordPrefix is the prefix of the path of requests to III catalogues for the permalink of a record.
RecordPrefix string = "/record=b"
// PatronInfoPrefix is the prefix of the path of requests to III catalogues for the patron login form.
PatronInfoPrefix string = "/patroninfo"
// SearchAuthorIndexPrefix is the prefix of the path of requests to III catalogues for the author search.
SearchAuthorIndexPrefix string = "/search/a"
// SearchCallNumberIndexPrefix is the prefix of the path of requests to III catalogues for the call number search.
SearchCallNumberIndexPrefix string = "/search/c"
// SearchTitleIndexPrefix is the prefix of the path of requests to III catalogues for the title search.
SearchTitleIndexPrefix string = "/search/c"
// AdvancedSearchPrefix is the prefix of the path of requests to III catalogues for advanced searches.
AdvancedSearchPrefix string = "/search/X"
// SearchPrefix is the prefix of the path of requests to III catalogues for search results.
SearchPrefix string = "/search"
)
// A version flag, which should be overwritten when building using ldflags.
var version = "devel"
// Detourer is a struct which stores the data needed to perform redirects.
type Detourer struct {
idMap map[uint32]uint64 // The map of III BibIDs to ExL IDs.
primo string // The domain name (host) for the target Primo instance.
vid string // The vid parameter to use when building Primo URLs.
lp *url.URL // The landing page for redirects.
}
// The Detourer serves HTTP redirects based on the request.
func (d Detourer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// In the default case, redirect to the Primo search form.
redirectTo := &url.URL{
Scheme: "https",
Host: d.primo,
Path: "/discovery/search",
}
// Depending on the prefix...
switch {
case strings.HasPrefix(r.URL.Path, RecordPrefix):
d.buildRecordRedirect(redirectTo, r)
case strings.HasPrefix(r.URL.Path, PatronInfoPrefix):
redirectTo.Path = "/discovery/login"
case strings.HasPrefix(r.URL.Path, SearchAuthorIndexPrefix):
buildAuthorSearchRedirect(redirectTo, r)
case strings.HasPrefix(r.URL.Path, SearchCallNumberIndexPrefix):
buildCallNumberSearchRedirect(redirectTo, r)
case strings.HasPrefix(r.URL.Path, SearchTitleIndexPrefix):
buildTitleSearchRedirect(redirectTo, r)
case strings.HasPrefix(r.URL.Path, AdvancedSearchPrefix):
setParamInURL(redirectTo, "mode", "advanced")
buildSearchRedirect(redirectTo, r)
case strings.HasPrefix(r.URL.Path, SearchPrefix):
buildSearchRedirect(redirectTo, r)
}
// Set the vid parameter on all redirects.
setParamInURL(redirectTo, "vid", d.vid)
// If the landing page has been set, set the dest param to the final URL.
if d.lp != nil {
dest := redirectTo.String()
redirectTo = d.lp
setParamInURL(redirectTo, "dest", dest)
}
// Send the redirect to the client.
http.Redirect(w, r, redirectTo.String(), http.StatusMovedPermanently)
}
// buildRecordRedirect updates redirectTo to the correct Primo record URL for the requested bibID.
func (d Detourer) buildRecordRedirect(redirectTo *url.URL, r *http.Request) {
// Convert everything after the RecordPrefix to a integer.
bibID64, err := strconv.ParseUint(r.URL.Path[len(RecordPrefix):], 10, 32)
if err == nil {
bibID := uint32(bibID64)
exlID, present := d.idMap[bibID]
if present {
redirectTo.Path = "/discovery/fulldisplay"
setParamInURL(redirectTo, "docid", fmt.Sprintf("alma%v", exlID))
}
}
}
// buildAuthorSearchRedirect updates redirectTo to the correct Primo URL for the requested author search.
func buildAuthorSearchRedirect(redirectTo *url.URL, r *http.Request) {
redirectTo.Path = "/discovery/browse"
setParamInURL(redirectTo, "browseScope", "author")
q := r.URL.Query()
searchParam := q.Get("SEARCH")
if searchParam != "" {
setParamInURL(redirectTo, "browseQuery", searchParam)
}
}
// buildCallNumberSearchRedirect updates redirectTo to the correct Primo URL for the requested call number search.
func buildCallNumberSearchRedirect(redirectTo *url.URL, r *http.Request) {
redirectTo.Path = "/discovery/browse"
setParamInURL(redirectTo, "browseScope", "callnumber.0")
q := r.URL.Query()
searchParam := q.Get("SEARCH")
if searchParam != "" {
setParamInURL(redirectTo, "browseQuery", searchParam)
}
}
// buildTitleSearchRedirect updates redirectTo to the correct Primo URL for the requested title search.
func buildTitleSearchRedirect(redirectTo *url.URL, r *http.Request) {
redirectTo.Path = "/discovery/browse"
setParamInURL(redirectTo, "browseScope", "title")
q := r.URL.Query()
searchParam := q.Get("SEARCH")
if searchParam != "" {
setParamInURL(redirectTo, "browseQuery", searchParam)
}
}
// buildSearchRedirect updates redirectTo to an approximate Primo URL for the requested search.
func buildSearchRedirect(redirectTo *url.URL, r *http.Request) {
q := r.URL.Query()
// Sort
switch q.Get("sortdropdown") {
case "t":
setParamInURL(redirectTo, "sortby", "title")
case "a":
setParamInURL(redirectTo, "sortby", "author")
case "c":
setParamInURL(redirectTo, "sortby", "date_a")
case "r":
setParamInURL(redirectTo, "sortby", "date_d")
}
// Filter
switch q.Get("searchscope") {
case "1":
setParamInURL(redirectTo, "mfacet", "rtype,include,books,1")
case "2":
setParamInURL(redirectTo, "mfacet", "rtype,include,journals,1")
case "3":
setParamInURL(redirectTo, "mfacet", "rtype,include,books,1")
addParamInURL(redirectTo, "mfacet", "rtype,include,online_resources,2")
case "4":
setParamInURL(redirectTo, "mfacet", "rtype,include,journals,1")
addParamInURL(redirectTo, "mfacet", "rtype,include,online_resources,2")
case "5":
setParamInURL(redirectTo, "mfacet", "rtype,include,online_resources,1")
case "6":
setParamInURL(redirectTo, "mfacet", "rtype,include,government_documents,1")
case "7":
setParamInURL(redirectTo, "mfacet", "rtype,include,audios,1")
case "8":
setParamInURL(redirectTo, "mfacet", "rype,include,videos,1")
}
setParamInURL(redirectTo, "tab", "Everything")
setParamInURL(redirectTo, "search_scope", "MyInst_and_CI")
if q.Get("searcharg") != "" {
switch q.Get("searchtype") {
case "t":
setParamInURL(redirectTo, "query", fmt.Sprintf("title,contains,%v", q.Get("searcharg")))
case "a":
setParamInURL(redirectTo, "query", fmt.Sprintf("creator,contains,%v", q.Get("searcharg")))
case "d":
setParamInURL(redirectTo, "query", fmt.Sprintf("sub,contains,%v", q.Get("searcharg")))
case "c":
redirectTo.Path = "/discovery/browse"
setParamInURL(redirectTo, "browseScope", "callnumber.0")
setParamInURL(redirectTo, "browseQuery", q.Get("searcharg"))
case "X":
setParamInURL(redirectTo, "mode", "advanced")
setParamInURL(redirectTo, "query", fmt.Sprintf("any,contains,%v", q.Get("searcharg")))
default:
setParamInURL(redirectTo, "query", fmt.Sprintf("any,contains,%v", q.Get("searcharg")))
}
} else if q.Get("SEARCH") != "" {
setParamInURL(redirectTo, "mode", "advanced")
setParamInURL(redirectTo, "query", fmt.Sprintf("any,contains,%v", q.Get("SEARCH")))
}
}
func main() {
// Define the command line flags.
addr := flag.String("address", DefaultAddress, "Address to bind on.")
subdomain := flag.String("primo", "", "The subdomain of the target Primo instance, ?????.primo.exlibrisgroup.com. Required.")
vid := flag.String("vid", "", "VID parameter for Primo. Required.")
landingpage := flag.String("landingpage", "", "Landing page for redirects. Optional.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Permanent Detour: A tiny web service which redirects Sierra Web OPAC requests to Primo URLs.\n")
fmt.Fprintf(os.Stderr, "Version %v\n", version)
fmt.Fprintf(os.Stderr, "Usage: permanentdetour [flag...] [file...]\n")
fmt.Fprintf(os.Stderr, "If 'landingpage' flag is supplied, redirects will go to page?dest=finaldest\n\n")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "\n Environment variables read when flag is unset:")
flag.VisitAll(func(f *flag.Flag) {
uppercaseName := strings.ToUpper(f.Name)
fmt.Fprintf(os.Stderr, " %v%v\n", EnvPrefix, uppercaseName)
})
}
// Process the flags.
flag.Parse()
err := overridefromenv.Override(flag.CommandLine, EnvPrefix)
if err != nil {
log.Fatalln(err)
}
if *subdomain == "" {
log.Fatalln("A primo subdomain is required.")
}
if *vid == "" {
log.Fatalln("A vid is required.")
}
var lp *url.URL
// Check the page flag
if *landingpage != "" {
var err error // avoid shadowing
lp, err = url.Parse(*landingpage)
if err != nil {
log.Fatalf("Error parsing landing page '%v' as URL: %v", *landingpage, err)
}
log.Printf("Landing Page: %v\n", lp.String())
}
// The Detourer has all the data needed to build redirects.
d := Detourer{
primo: fmt.Sprintf("%v.%v", *subdomain, PrimoDomain),
vid: *vid,
lp: lp,
}
log.Printf("Primo Instance: %v\n", d.primo)
log.Printf("VID: %v\n", d.vid)
// Map of III BibIDs to ExL IDs
// The initial size is an estimate based on the number of arguments.
size := uint64(len(flag.Args())) * MaxMappingFileLength
d.idMap = make(map[uint32]uint64, size)
// Process each file in the arguments list.
for _, mappingFilePath := range flag.Args() {
// Add the mappings from this file to the idMap.
err := processFile(d.idMap, mappingFilePath)
if err != nil {
log.Fatalf("Error processing mapping file: %v.\n", err)
}
}
log.Printf("%v III BibID to Ex Libris ID mappings processed.\n", len(d.idMap))
// Use an explicit request multiplexer.
mux := http.NewServeMux()
mux.Handle("/", d)
server := http.Server{
Addr: *addr,
Handler: mux,
}
shutdown := make(chan struct{})
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Wait to receive a message on the channel.
<-sigs
err := server.Shutdown(context.Background())
if err != nil {
log.Printf("Error shutting down server, %v.\n", err)
}
close(shutdown)
}()
log.Println("Starting server.")
err = server.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatalf("Fatal server error, %v.\n", err)
}
<-shutdown
log.Println("Server stopped.")
}
// processFile takes a file path, opens the file, and reads it line by line to extract id mappings.
func processFile(m map[uint32]uint64, mappingFilePath string) error {
// Get the absolute path of the file. Not strictly necessary, but creates clearer error messages.
absFilePath, err := filepath.Abs(mappingFilePath)
if err != nil {
return fmt.Errorf("could not get absolute path of %v, %v", mappingFilePath, err)
}
// Open the file for reading. Close the file automatically when done.
file, err := os.Open(absFilePath)
if err != nil {
return fmt.Errorf("could not open %v for reading, %v", absFilePath, err)
}
defer file.Close()
// Read the file line by line.
scanner := bufio.NewScanner(file)
lnum := 0
for scanner.Scan() {
lnum++
bibID, exlID, err := processLine(scanner.Text())
if err != nil {
return fmt.Errorf("unable to process line %v '%v', %v", lnum, scanner.Text(), err)
}
_, present := m[bibID]
if present {
return fmt.Errorf("previously seen Bib ID %v was encountered", bibID)
}
m[bibID] = exlID
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("scanner error when processing %v, %v", absFilePath, err)
}
return nil
}
// processLine takes a line of input, and finds the III bib ID and the exL ID.
func processLine(line string) (bibID uint32, exlID uint64, _ error) {
// Split the input line into fields on commas.
splitLine := strings.Split(line, ",")
if len(splitLine) < 2 {
return bibID, exlID, fmt.Errorf("line has incorrect number of fields, 2 expected, %v found", len(splitLine))
}
// The bibIDs look like this: a1234x-instid
// We need to strip off the first character and anything after the dash.
dashIndex := strings.Index(splitLine[1], "-")
if (dashIndex == 0) || (dashIndex == 1) {
return bibID, exlID, fmt.Errorf("no bibID number was found before dash between bibID and institution id")
}
bibIDString := "invalid"
// If the dash isn't found, use the whole bibID field except the first character.
if dashIndex == -1 {
bibIDString = splitLine[1][1:len(splitLine[1])-1]
} else {
bibIDString = splitLine[1][1:dashIndex-1]
}
bibID64, err := strconv.ParseUint(bibIDString, 10, 32)
if err != nil {
return bibID, exlID, err
}
bibID = uint32(bibID64)
exlID, err = strconv.ParseUint(splitLine[0], 10, 64)
if err != nil {
return bibID, exlID, err
}
return bibID, exlID, nil
}
// setParamInURL is a helper function which sets a parameter in the query of a url.
func setParamInURL(redirectTo *url.URL, param, value string) {
q := redirectTo.Query()
q.Set(param, value)
redirectTo.RawQuery = q.Encode()
}
// addParamInURL is a helper function which adds a parameter in the query of a url.
func addParamInURL(redirectTo *url.URL, param, value string) {
q := redirectTo.Query()
q.Add(param, value)
redirectTo.RawQuery = q.Encode()
}