-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_example.js
68 lines (61 loc) · 1.85 KB
/
node_example.js
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
/*
Node.js script to test haku-app streaming API.
Prereqs:
npm install --save cheerio bluebird request JSONStream event-stream
Create file with three lines:
my-username
my-password
https://host
Start streaming with command:
node node_example.js file_with_username_password_and_host.txt
*/
const http = require('http')
const fs = require('fs')
const request = require('request')
const cheerio = require('cheerio')
const Promise = require('bluebird')
const JSONStream = require('JSONStream')
const es = require('event-stream')
const [file_with_creds_and_host] = process.argv.slice(-1)
const [username, password, hostname] = fs.readFileSync(file_with_creds_and_host, 'utf8').split(/\r?\n/)
const postForm = (url, form) => {
return new Promise((resolve, reject) => {
request.post(url, {form:form}, (error, response, body) => {
if(error) {
reject(error)
} else {
resolve(body)
}
})
})
}
const ticketGrantingTicketFromResponseHtml = (htmlPageWithTicket) => {
$ = cheerio.load(htmlPageWithTicket)
const tgt = $('form').attr('action')
return tgt
}
postForm(hostname + '/cas/v1/tickets', {username:username, password:password})
.then(ticketGrantingTicketFromResponseHtml)
.then((tgtUrl) => postForm(tgtUrl,{service:hostname+'/haku-app/j_spring_cas_security_check'}))
.then((serviceTicket) => {
const path = '/haku-app/streaming/applications/listfull'
const url = hostname + path
console.log('Calling URL ' + url)
request({ url: url,
method: "POST",
headers: {
"Content-Type": "application/json",
"CasSecurityTicket": serviceTicket
},
json: {
"searchTerms":"",
"asIds":["1.2.246.562.29.25191045126"],
"aoOids":[],
"states":["ACTIVE","INCOMPLETE"],
}
}).pipe(JSONStream.parse('*'))
.pipe(es.mapSync(function (data) {
console.error('Got one! ' + data.oid)
return 1
}))
})