Skip to content

Instantly share code, notes, and snippets.

@Stevoisiak
Last active June 14, 2024 20:19
Show Gist options
  • Save Stevoisiak/2cea3f318ca02d7e914fa5abc4422b61 to your computer and use it in GitHub Desktop.
Save Stevoisiak/2cea3f318ca02d7e914fa5abc4422b61 to your computer and use it in GitHub Desktop.
This file provides the basic methods and objects needed to login, logout, and send xml requests to Kronos Workforce Central v4.0 XML API gateway.
// Source: WFC v5.0 Developer's Toolkit Programmer's Guide
// Chapter 3: Advanced Programming Topics
// https://forge.cornell.edu/sf/docman/do/downloadDocument/projects.cit_is_customer_apps/docman.root.design_docs/doc5084
/*
Copyright:Kronos, Inc. 2001
Description:
This file provides the basic methods and objects needed to login, logout, and send xml requests to WFC v4.0 xml api gateway.
Methods:
logon(username,password[,url]) return true or false
logoff([url]) return true or false
execute(dom,url,action) return response dom if Status="Success", else returns null
Object:
XMLHTTP([url]) send and receive xml streams from any given url
XMLHTTP.send(xml) send the xml stream to the given url and returns the response
Object:
Request([url][,dom][,parent]) default dom is created with Kronos_WFC/Request node path
Request.getAction() returns the action attribute value
Request.setAction(str) sets the action attribute value
Request.getObject() returns the contained node
Request.setObject(obj) sets the contained node
Request.getNode() returns the current dom
Request.execute([action][,obj]) returns the response node on success otherwise null
Request.getResponse() returns the full response dom returned from server after execute()
Request.onchange set this property to a function handler, when
the Request object changes, the function handler
will be invoked
Request.getDocumentElement() returns the documentElement (i.e. top level node)
*/
// global variables
var jsessionid = ''
// constants
var SESSION_COOKIE = 'jsessionid'
var DEFAULT_URL = 'http://localhost/wfc/XmlService'
var DEFAULT_USER = 'superuser'
var DEFAULT_PSWD = '<superuser password>'
var ACTION_ATTR = 'Action'
var OBJECT_ATTR = 'Object'
var USERNAME_ATTR = 'UserName'
var PASSWORD_ATTR = 'Password'
var QUERY_NAME_ATTR = 'QueryName'
var RESPONSE_STATUS_ATTR = 'Status'
var RESPONSE_STATUS_SUCCESS = 'Success'
var DOCUMENT_ELEMENT_NAME = 'Kronos_WFC'
var REQUEST_NODE_NAME = 'Request'
var RESPONSE_NODE_NAME = 'Response'
var REQUEST_PATH = '//' + DOCUMENT_ELEMENT_NAME + '/' + REQUEST_NODE_NAME
var RESPONSE_PATH = '//' + DOCUMENT_ELEMENT_NAME + '/ ' + RESPONSE_NODE_NAME
//send xml request and return xmlDom response
var XMLHTTP_count = 0
function XMLHTTP(url) {
this.id = 'XMLHTTP' + (++XMLHTTP_count)
this.url = (url) ? url : DEFAULT_URL
this.method = 'POST'
this.contentType = 'text/xml'
this.send = XMLHTTP_send
this._init = XMLHTTP_init
this._http = new Object()
this.setSessionIdFromCookies = XMLHTTP_setSessionIdFromCookies
this.obj = this.id + 'XMLHTTPObject'
eval(this.obj + '=this')
this._init()
}
function XMLHTTP_init() {
//Msxml2.XMLHTTP.3.0 //Microsoft.XMLHTTP
this._http = new ActiveXObject("msxml2.XMLHTTP")
this._http.open("POST", this.url, false)
this._http.setRequestHeader('Content-type', this.contentType)
// normally this code is not needed, however, there is a known
// bug that causes the XMLHTTP object to fail to send cookies.
// this code seems to reduce the frequency of failure.
// since WFC 4.0 xml api gateway uses cookies to maintain a
// session id, it is impossible to get around this bug.
// when Microsoft fixes this bug, it will require clients of
// this code to upgrade their XMLHTTP. reference Microsoft
// MSDN:// http://support.microsoft.com/support/kb/articles/
// Q290/8/99.ASP if(jsessionid){ document.cookie=(SESSION_COOKIE+'='+jsessionid) }
}
function XMLHTTP_send(xml) {
//this._init()
try {
this._http.send(xml.xml)
} catch (e) {
e.number = e.number & 0xFFFF
if (e.number == 5) {
e.description = "Unable to find URL: " + this.url
}
throw e
}
// see note in init() method ...
this.setSessionIdFromCookies(this._http.getResponseHeader('SetCookie'))
return this._http.responseXML
}
function XMLHTTP_setSessionIdFromCookies(cookies) {
if (cookies) {
var startPos = cookies.indexOf('jsessionid')
if (startPos >= 0) {
startPos = cookies.indexOf(' = ') + 1
var stopPos = cookies.indexOf(';', startPos)
jsessionid = cookies.substring(startPos, stopPos)
}
}
}
// submit a given dom to a specified url
// return the response node on success
// if you need to know more about the error
// result, use the Request object
function execute(dom, url, action) {
var root = dom.documentElement
if (action) {
var node = root.selectSingleNode(REQUEST_PATH)
node.setAttribute(ACTION_ATTR, action)
}
var http = new XMLHTTP(url)
var responseDOM = http.send(root)
if (responseDOM == null) {
return null
}
var responseNode = responseDOM.selectSingleNode(RESPONSE_PATH)
if (responseNode == null) {
return null
}
var success = (responseNode.getAttribute(RESPONSE_STATUS_ATTR) == RESPONSE_STATUS_SUCCESS)
if (success) {
return responseNode
} else {
return null
}
}
// LOGON method
function logon(username, password, url) {
var request = new Request(null)
request.getNode().setAttribute(USERNAME_ATTR, username)
request.getNode().setAttribute(PASSWORD_ATTR, password)
request.getNode().setAttribute(ACTION_ATTR, 'LOGON')
request.getNode().setAttribute(OBJECT_ATTR, 'SYSTEM')
return (request.execute() != null)
}
// LOGOFF method
function logoff(logoutDOM, url) {
var request = new Request(null)
request.getNode().setAttribute(ACTION_ATTR, 'LOGOFF')
request.getNode().setAttribute(OBJECT_ATTR, 'SYSTEM')
return (request.execute() != null)
}
/*
Copyright:Kronos, Inc. 2001
This code is generated. DO NOT Edit!
Request Description:
*/
var NODE_ELEMENT = 1
function __raiseevent(e) {
eval(e)
if (this.parent) {
this.parent._raiseevent(e)
}
}
var Request_count = 0
function Request(url, dom, parent) {
this._dom = dom
this.parent = parent
this.url = (url) ? url : DEFAULT_URL
this.id = 'Request' + (++Request_count)
this.name = this.id
this.type = REQUEST_NODE_NAME
this.getNode = Request_getNode
this.getAction = Request_getAction
this.setAction = Request_setAction
this.getObject = Request_getObject
this.setObject = Request_setObject
this.getResponse = Request_getResponse
this.execute = Request_execute
this.onchange = new Function()
this.getDocumentElement = Request_getDocumentElement
this.obj = 'top.' + this.id + 'RequestObject'
eval(this.obj + ' = this')
}
Request.prototype._raiseevent = __raiseevent
function Request_getNode() {
if (this._dom == null) {
var _newdoc = new ActiveXObject('msxml2.domdocument')
this._dom = _newdoc.createNode(NODE_ELEMENT, this.type, '')
if (this.parent) {
this.parent.getNode().appendChild(this._dom)
this._documentElement = this.parent.getNode().documentElement
} else {
var doc = _newdoc.createNode(NODE_ELEMENT, DOCUMENT_ELEMENT_NAME, '')
doc.setAttribute('Version', '1.0')
doc.appendChild(this._dom)
this._documentElement = doc
}
}
return this._dom
}
function Request_execute(action, object) {
if (action) {
this.setAction(action)
}
if (object) {
this.setObject(object)
}
var http = new XMLHTTP(this.url)
try {
this._response = http.send(this.getDocumentElement())
} catch (e) {
log("Unable to send request. Possible reason:\nNumber: " + e.number + "\nDescription: " + e.description)
}
if (this._response == null) {
return null
}
var responseNode = this._response.selectSingleNode(RESPONSE_PATH)
if (responseNode == null) {
return null
}
var success = (responseNode.getAttribute(RESPONSE_STATUS_ATTR) == RESPONSE_STATUS_SUCCESS)
if (success) {
return responseNode
} else {
// use getResponse() to see the xml returned
return null
}
}
function Request_getResponse() {
return this._response
}
function Request_getDocumentElement() {
this.getNode() // insure we have a dom
return this._documentElement
}
function Request_getAction() {
return this.getNode().getAttribute(ACTION_ATTR)
}
function Request_setAction(vAction) {
this.getNode().setAttribute(ACTION_ATTR, vAction)
this._raiseevent('this.onchange("' + ACTION_ATTR + '")')
}
function Request_getObject() {
return this.getNode().childNodes
}
function Request_setObject(vObject) {
var _root = this.getNode().selectSingleNode(' //' + this.type)
if (_root) {
var _oldnode = _root.selectSingleNode('//' + this.type + '/' + vObject.type)
if (_oldnode) {
_root.removeChild(_oldnode)
}
}
this.getNode().appendChild((vObject.getNode) ? vObject.getNode() : vObject)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment