function LEJSONRPCError() {
this.inheritFrom = Error
this.domain = null;
this.code = null;
this.message = null;
}
LEJSONRPCError.prototype.toString = function() {
return "LEJSONRPCError ("+this.code+"::"+this.domain+") \""+this.message+"\"";
}
function LEJSONRPCProxy() {
this.format = new LEJSONFormat();
this.requestIdCounter = 0;
this.synchronousResponse = null;
this.synchronousError = null;
this.namedEndpoints = null;
this.wosid = null;
this.sessionTimeoutSeconds = -1;
this.baseEndpointName = null;
this.assumeInBrowser = true;
this.entryPoint = null;
}
LEJSONRPCProxy.prototype.processGlobalIDExchangeResult = function(mapping,tgid) {
for(i=0;i<mapping.length;i+=2)
{
if(mapping[i].token==tgid.token)
return mapping[i+1];
}
return null;
}
LEJSONRPCProxy.prototype.processAuthenticationResult = function(result) {
if(typeof result != 'object')
throw new Error('the result of an authenticate invocation should be object');
if(null!=result['wosid'])
{
this.namedEndpoints = { };
this.wosid = result['wosid'];
this.sessionTimeoutSeconds = result['sessionTimeoutSeconds'];
this.entryPoint = result['entryPoint'];
for(var key in result) {
if(key.length-8 == key.lastIndexOf('Endpoint')) {
this.namedEndpoints[key]=result[key];
}
if(-1!=key.indexOf('BaseEndpoint'))
this.baseEndpointName = key;
}
return true;
}
return false;
}
LEJSONRPCProxy.prototype.authenticateOnEndpoint = function(endpoint,principal,credential) {
var params = [];
params.push(principal);
params.push(credential);
return this.processAuthenticationResult(this.invokeOnEndpointWithParameters('authenticate',endpoint,params));
}
LEJSONRPCProxy.prototype.forgetSession = function() {
this.baseEndpointName = null;
this.namedEndpoints = null;
this.wosid = null;
this.sessionTimeoutSeconds = null;
this.entryPoint = null;
}
LEJSONRPCProxy.prototype.logoutSession = function() {
if(null!=this.wosid)
{
this.invokeOnNamedEndpointWithParameters('logout',this.baseEndpointName,[]);
this.forgetSession();
}
}
LEJSONRPCProxy.prototype.invokeOnNamedEndpointWithParameters = function(methodName, namedEndpoint, parameters) {
if(null==this.wosid)
throw new Error('attempt to invoke a method on a named endpoint without a session');
var endpoint = this.namedEndpoints[namedEndpoint];
if(null==endpoint)
throw new Error('attempt to invoke a method on a named endpoint '+namedEndpoint+' which does not exist');
return this.invokeOnEndpointWithParameters(methodName,endpoint,parameters);
}
LEJSONRPCProxy.prototype.invokeOnEndpointWithParameters = function(methodName, endpoint, parameters) {
this.synchronousResponse = null;
this.synchronousError = null;
if((null==methodName)||(0==methodName.length)) {
throw new Error("the method name has not been supplied for a JSON-RPC invocation");
}
if(null==parameters) {
parameters = [ ];
}
var outboundMap = {
"method" : methodName,
"version" : "1.0",
"params" : parameters,
"id" : this.requestIdCounter };
this.requestIdCounter++;
var xmlhttp = false;
if(this.assumeInBrowser)
{
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
xmlhttp = false;
}
} else if(window.ActiveXObject) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xmlhttp = false;
}
}
}
}
else
xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST',endpoint,false);
xmlhttp.setRequestHeader("Content-Type","application/json");
xmlhttp.send(this.format.format(outboundMap));
if(200==xmlhttp.status) {
this.synchronousResponse = this.format.parse(xmlhttp.responseText);
}
else {
this.synchronousError = this.format.parse(xmlhttp.responseText);
}
if(null!=this.synchronousResponse) {
return this.synchronousResponse['result'];
}
var err = new LEJSONRPCError();
err.domain = "LEWOStuff";
err.message = "unknown JSON-RPC invocation error communicating to the server."
err.code = 0;
if(null!=this.synchronousError)
{
var errMap = this.synchronousError.error;
if(null!=errMap.code) { err.code = errMap.code; }
if(null!=errMap.message) { err.message = errMap.message; }
if(null!=errMap.domain) { err.domain = errMap.domain; }
}
throw err;
}
