
var defaultXmlBlasterSecurityPlugin="htpasswd";org.xmlBlaster.client.XbAccess=function(urlBase,loginName){if(!org.xmlBlaster.util.isDefined(urlBase))
throw"IllegalArgument: Can't create org.xmlBlaster.client.XbAccess without urlBase";this.urlBase=urlBase;this.loginName=(loginName)?loginName:"anonymous";this.isConnected=false;this.serverPollerTimeoutHandle=null;this.log=org.xmlBlaster.util.log;this.errorHandlerObject=null;this.errorHandlerFunc=null;this.httpMockCb=null;this.setHttpMockCb=function(httpMockCb){this.httpMockCb=httpMockCb||null;}
this.getLoginName=function(){return this.loginName;}
this.getUrlBase=function(callType){if(callType==undefined)return this.urlBase;if(callType=="POST")
return this.urlBase;return this.getUrlBaseRandom();}
this.getUrlBaseRandom=function(){return this.getUrlBase()+"?sid="+Math.random();}
this.postXmlScript=function(script_,responseFp_,responseThisArg,async_,forceLoad_,returnObj_,_additionalParams){var script=script_||"";var responseFp=responseFp_||null;var returnObj=returnObj_||null;var async=(async_==undefined||async_==null)?true:async_;var forceLoad=(forceLoad_==undefined||forceLoad_==null)?false:forceLoad_;var myXmlHttp=org.xmlBlaster.util.createXmlHttpRequest(this.httpMockCb);var callType="POST";var url=this.getUrlBase(callType);var hasQuest=false;if(forceLoad==true){hasQuest=true;url+="?forceLoad=true";}
if(org.xmlBlaster.util.isFilled(_additionalParams)){url+=(hasQuest)?("&"+_additionalParams):("?"+_additionalParams);hasQuest=true;}
var enc=org.xmlBlaster.util.Base64.encode(script);var xbAccessRef=this;var log=org.xmlBlaster.util.log;log.debug("XbAccess.js-postXmlScript() calling async="+async+" async_="+async_+" responseFp_="+(responseFp_!=undefined&&responseFp_!=null)+" script="+script);if(async==true){myXmlHttp.onreadystatechange=function(){if(myXmlHttp.readyState==xbAccessRef.READYSTATE_DONE||myXmlHttp.readyState=="complete"){var dataReceived=myXmlHttp.responseText;log.debug("XbAccess.js-Status:"+myXmlHttp.status+" script: "+script+" returned"+dataReceived);if(responseFp!=null){responseFp.call(responseThisArg,dataReceived,myXmlHttp,returnObj);}}}}
myXmlHttp.open(callType,url,async);myXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");var params="ActionType=xmlScript&xmlScriptBase64="+enc;myXmlHttp.setRequestHeader("Content-length",params.length);log.debug("XbAccess.js-callType="+callType+" + url="+url+"  "+params);try{myXmlHttp.send(params);}
catch(ex){throw new org.xmlBlaster.util.XmlBlasterException("communication.noConnection","XbAccess.js#postXmlScript","No Web-Server found for: "+url,false);}
if(async==true){return null;}
else{return myXmlHttp.responseText;}}
this.connectRaw=function(forceLoad,qos,returnObj,additionalParams){var async=false;this.log.info("XbAccess.js-connect(): "+qos);var xmlString=this.postXmlScript(qos,null,null,async,forceLoad,returnObj,additionalParams);this.isConnected=true;this.log.info("XbAccess.js-connect() returned: "+xmlString);var domDocument=org.xmlBlaster.util.getDOMDocument(xmlString);var msgUnits=org.xmlBlaster.util.parseXmlBlasterResponse(domDocument);if(msgUnits.length==0)
return null;var msgUnit=msgUnits[0];if(msgUnit.isException())
throw msgUnit.getXmlBlasterException();return msgUnit.getQosData();}
this.connect=function(forceLoad,securityLoginName,passwd,securityPlugin_,maxSessions,returnObj_,additionalParams){var securityPlugin=(securityPlugin_==undefined||securityPlugin_==null)?defaultXmlBlasterSecurityPlugin:securityPlugin_;var maxSessions=maxSessions||10;var timeout=0;var qos="";if(org.xmlBlaster.util.isFilled(securityLoginName)){qos="<xmlBlaster>"+"<connect>"+"<qos>"+" <securityService type='"+securityPlugin+"' version='1.0'>"+"   <user>"+securityLoginName+"</user>"+"   <passwd>"+passwd+"</passwd>"+" </securityService>"+" <session name='"+this.getLoginName()+"' timeout='"+timeout+"' maxSessions='"+maxSessions+"'/>"+" <queue relating='connection'>"+"   <address type='SOCKET' retries='0'/>"+" </queue>"+" <queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>"+"   <callback type='SOCKET' sessionId='browser' "+"      pingInterval='600000' retries='1' delay='60000' dispatcherActive='true'>"+"   </callback>"+" </queue>"+" <clientProperty name='locale'>"+org.xmlBlaster.util.getLocaleIdJava()+"</clientProperty>"+" <clientProperty name='timezoneOffsetMinutes'>"+org.xmlBlaster.util.getTimezoneOffsetMinutes()+"</clientProperty>"+"</qos>"+"</connect>"+"</xmlBlaster>";}
else{qos="<xmlBlaster>"+"<connect>"+"<qos>"+" <session name='"+this.getLoginName()+"' timeout='"+timeout+"' maxSessions='"+maxSessions+"'/>"+" <clientProperty name='locale'>"+org.xmlBlaster.util.getLocaleIdJava()+"</clientProperty>"+" <clientProperty name='timezoneOffsetMinutes'>"+org.xmlBlaster.util.getTimezoneOffsetMinutes()+"</clientProperty>"+"</qos>"+"</connect>"+"</xmlBlaster>";}
return this.connectRaw(forceLoad,qos,returnObj_,additionalParams);}
this.disconnect=function(forceLoad,securityLoginName,passwd){var qos="<xmlBlaster>"+"<disconnect>"+"<qos>"+"</qos>"+"</disconnect>"+"</xmlBlaster>";var async=false;this.log.info("XbAccess.js-Disconnect: "+qos);var xmlString=this.postXmlScript(qos,null,null,async);this.isConnected=false;this.log.info("XbAccess.js-Disconnect returned: "+xmlString);if(xmlString==null||xmlString.length==0){this.log.error("XbAccess.js-Disconnect returned empty string, we ignore it");return null;}
var domDocument=org.xmlBlaster.util.getDOMDocument(xmlString);var msgUnits=org.xmlBlaster.util.parseXmlBlasterResponse(domDocument);if(msgUnits.length==0)
return null;var msgUnit=msgUnits[0];return msgUnit.getQosData();}
this.shutdown=function shutdown(){this.stopAjaxPolling();}
this.subscribe=function(key_,qos_,responseFp_,responseThisArg,returnObj_){var key=key_||null;var qos=(qos_==undefined||qos_==null)?"<qos><multiSubscribe>false</multiSubscribe><local>false</local></qos>":qos_;if(key==null)return false;var keyXml="";if(key.indexOf("exact:")==0){keyXml="  <key oid='"+key.substr("exact:".length)+"'/>";}
else if(key.indexOf("xpath:")==0){keyXml="  <key oid='' queryType='XPATH'>"+key.substr("xpath:".length)+"</key>";}
else if(key.indexOf("<")==0){keyXml=key;}
else{keyXml="<key oid='"+key+"'/>";}
var script="<xmlBlaster>"+" <subscribe>"+"  "+keyXml+"  "+qos+" </subscribe>"+"</xmlBlaster>";this.log.info("XbAccess.js-Subscribe: "+script);var async=true;this.postXmlScript(script,responseFp_,responseThisArg,async,false,returnObj_);}
this.unSubscribe=function(key,qos,responseFp_,responseThisArg,returnObj_){if(!org.xmlBlaster.util.isDefined(key))
throw"IllegalArgument: Can't unSubscribe without key";var qos=(org.xmlBlaster.util.isDefined(qos))?qos:"<qos/>";var keyXml="";if(key.indexOf("exact:")==0){keyXml="  <key oid='"+key.substr("exact:".length)+"'/>";}
else if(key.indexOf("xpath:")==0){keyXml="  <key oid='' queryType='XPATH'>"+key.substr("xpath:".length)+"</key>";}
else if(key.indexOf("<")==0){keyXml=key;}
else{keyXml="<key oid='"+key+"'/>";}
var script="<xmlBlaster>"+" <unSubscribe>"+"  "+keyXml+"  "+qos+" </unSubscribe>"+"</xmlBlaster>";this.log.info("XbAccess.js-UnSubscribe: "+script);var async=true;this.postXmlScript(script,responseFp_,responseThisArg,async,false,returnObj_);}
this.publish=function(msgUnit,responseFp_,responseThisArg,returnObj_){var xml=msgUnit.toXml();var script="<xmlBlaster>"+xml+"</xmlBlaster>";this.log.info("XbAccess.js-publish: "+script);this.setStartTimestamp("XbAccess.publish("+msgUnit.getKeyData().getOid()+")");var async_=true;var forceLoad_=false;this.postXmlScript(script,responseFp_,responseThisArg,true,false,returnObj_);}
this.publishServiceMessage=function(topicId,serviceListTO,responseFp_,responseThisArg,returnObj_){var content=serviceListTO.toXml();var script="<xmlBlaster>"+"  <publish>"+"    <key oid='"+topicId+"'/>"+"    <content>"+org.xmlBlaster.util.escapeXml(content)+"</content>"+"    <qos><clientProperty name='replyTo'>"+org.xmlBlaster.util.escapeXml(this.loginName)+"</clientProperty></qos>"+"  </publish>"+"</xmlBlaster>";this.log.info("XbAccess.js-publishServiceMessage "+topicId+": "+script);this.setStartTimestamp("XbAccess.publishServiceMessage("+serviceListTO.getTasks()+")");var async_=true;var forceLoad_=false;this.postXmlScript(script,responseFp_,responseThisArg,true,false,returnObj_);}
this.setStartTimestamp=function(info){this.startTimestamp=(new Date).valueOf();this.startTimestampInfo=info||"";}
this.resetStartTimestamp=function(){this.startTimestamp=null;this.startTimestampInfo="";}
this.logTimeElapsed=function(info,reset){if(!org.xmlBlaster.util.isDefined(this.startTimestamp))
return;var reset=reset||false;var info=info||"";var elapsed=(new Date-this.startTimestamp);this.log.info(elapsed+"ms "+this.startTimestampInfo+"-"+info);if(reset)
this.resetStartTimestamp();}
this.__ajaxPollingSwitchedOn=false;this.defaultPollingType="updatePollBlocking";this.ajaxPollingMillis=8000;this.__doAjaxPolling=function(onAjaxUpdateFp,pollingType){if(pollingType==undefined||pollingType==null)
pollingType=this.defaultPollingType;this.log.debug("XbAccess.js-__doAjaxPolling() Called __doAjaxPolling("+pollingType+") "+this.ajaxPollingMillis);var ref=this.pollAjaxOnce(onAjaxUpdateFp,pollingType);if(typeof ref=="function")
ref();else
alert("ref is "+ref);if("updatePollBlocking"==pollingType){this.log.debug("XbAccess.js-__doAjaxPolling() updatePollBlocking -> we don't need a timer");return;}
if(this.ajaxPollingMillis>0){this.log.debug("XbAccess.js-__doAjaxPolling() updatePoll -> starting timer");var functRef=this.pollAjaxOnce(onAjaxUpdateFp,pollingType);this.serverPollerTimeoutHandle=setInterval(functRef,this.ajaxPollingMillis);}}
this.startAjaxPolling=function(_onAjaxUpdateFp,_pollingType){this.__ajaxPollingSwitchedOn=true;this.__doAjaxPolling(_onAjaxUpdateFp,_pollingType);}
this.stopAjaxPolling=function(){var ret=this.__ajaxPollingSwitchedOn;this.__ajaxPollingSwitchedOn=false;if(this.serverPollerTimeoutHandle!=null){clearTimeout(this.serverPollerTimeoutHandle);this.serverPollerTimeoutHandle=null;}
var handle=this.currentAjaxXmlHttp;if(handle!=null){handle.abort();this.currentAjaxXmlHttp=null;}
return true;}
this.READYSTATE_UNSENT=0;this.READYSTATE_OPENED=1;this.READYSTATE_HEADERS_RECEIVED=2;this.READYSTATE_LOADING=3;this.READYSTATE_DONE=4;this.HTTP_OK=200;this.HTTP_MOVED_PERMANENTLY=301;this.HTTP_MOVED_TEMPORARY=302;this,HTTP_BAD_REQUEST=400;this.HTTP_NOT_FOUND=404;this.HTTP_SERVER_ERROR=500;this.currentAjaxXmlHttp=null;this.pollAjaxOnce=function(onAjaxUpdateFp,pollingType){if(!this.isConnected){this.log.debug("XbAccess.js-Ignoring pollAjaxOnce(isConnected="+this.isConnected+")");return null;}
if(!this.__ajaxPollingSwitchedOn){this.log.debug("XbAccess.js-Ignoring pollAjaxOnce(__ajaxPollingSwitchedOn="+this.__ajaxPollingSwitchedOn+")");return null;}
if(pollingType==undefined||pollingType==null)
pollingType=this.defaultPollingType;var xbAccessRef=this;return(closureDummy=function(){if(!xbAccessRef.__ajaxPollingSwitchedOn){return null;}
var myXmlHttp=org.xmlBlaster.util.createXmlHttpRequest(this.httpMockCb);xbAccessRef.currentAjaxXmlHttp=myXmlHttp;var callType="GET";var url=xbAccessRef.getUrlBase(callType);url+="&ActionType="+pollingType;xbAccessRef.log.debug("XbAccess.js-pollAjaxOnce("+url+") inside closure ...");myXmlHttp.onreadystatechange=function(){var statusOk=false;{if((myXmlHttp.readyState==xbAccessRef.READYSTATE_DONE||myXmlHttp.readyState=="complete")){try{if(org.xmlBlaster.util.isDefined(myXmlHttp.status))
statusOk=(myXmlHttp.status==xbAccessRef.HTTP_OK);}
catch(ex){}}}
if(myXmlHttp.readyState==xbAccessRef.READYSTATE_DONE&&statusOk==false){xbAccessRef.log.debug("XbAccess.js-Tomcat shutdown, giving up");var wasStopped=xbAccessRef.stopAjaxPolling();if(!wasStopped)return;if(onAjaxUpdateFp){var msgUnit=new org.xmlBlaster.util.MsgUnit("exception");msgUnit.xmlBlasterException=new org.xmlBlaster.util.XmlBlasterException("resource.lostWebServer","XbAccess.js","The webserver is down, lost connection",false);var msgUnits=new Array();msgUnits.push(msgUnit);onAjaxUpdateFp(msgUnits);}
return;}
if((myXmlHttp.readyState==xbAccessRef.READYSTATE_DONE||myXmlHttp.readyState=="complete")&&statusOk){xbAccessRef.currentAjaxXmlHttp=null;var dataReceived=myXmlHttp.responseText;if(!org.xmlBlaster.util.isFilled(dataReceived)){if("updatePollBlocking"==pollingType){xbAccessRef.log.debug("XbAccess.js-Ignoring empty dataReceived (updatePollBlocking), no new data is available");closureDummy();}
else{xbAccessRef.log.debug("XbAccess.js-Ignoring empty dataReceived (updatePolling), no new data is available");}
return;}
xbAccessRef.logTimeElapsed("XbAccess.js finished ajax request myXmlHttp.readyState="+myXmlHttp.readyState+" myXmlHttp.status="+myXmlHttp.status+" data length="+dataReceived.length);var dom=myXmlHttp.responseXML;if(org.xmlBlaster.util.isDefined(dom)&&dom.documentElement!=null&&"void"!=""+dom.documentElement.nodeName){xbAccessRef.logTimeElapsed("XbAccess.pollAjaxOnce: Parsing now <xmlBlasterResponse>");var msgUnits=org.xmlBlaster.util.parseXmlBlasterResponse(dom);if(onAjaxUpdateFp){xbAccessRef.logTimeElapsed("XbAccess.pollAjaxOnce: Calling onAjaxUpdateFp(msgUnits)");onAjaxUpdateFp(msgUnits);xbAccessRef.logTimeElapsed("XbAccess.pollAjaxOnce: Calling onAjaxUpdateFp(msgUnits) done",true);}
else
xbAccessRef.log.error("XbAccess.js-No onAjaxUpdateFp given, ignoring "+dom);}
else{}
if("updatePollBlocking"==pollingType){xbAccessRef.log.debug("XbAccess.js-updatePollBlocking -> immediate invoke again");closureDummy();}}}
var async=true;myXmlHttp.open(callType,url,async);myXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");myXmlHttp.send(null);xbAccessRef.logTimeElapsed("XmlHttp.send");});}}
org.xmlBlaster.util.createXmlHttpRequest=function(httpMockCb)
{var objXMLHttp=null
if(window.XMLHttpRequest){objXMLHttp=new XMLHttpRequest();}
else if(window.ActiveXObject){objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");}
if(objXMLHttp==null)
alert("Your browser does not support Ajax");if(httpMockCb!=null){httpMockCb(objXMLHttp);}
return objXMLHttp}