Difference between revisions of "Scripts/Site Sync Apex"

From Observer GigaFlow Support | VIAVI Solutions Inc.
Jump to: navigation, search
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
var serverURL = "https://apex.usa.viavi.solutions";
+
var serverURL = "https://apex.usa.viavi.solutions";
var user="api";
+
var user="api";
var password="duijfhdlifhlziuhflsuhfgulh";
+
var password="duijfhdlifhlziuhflsuhfgulh";
 
+
//-------Only change whats above this line----------------
 
+
var loginURL =  serverURL+'/oa/api/session';
//-------Only change whats above this line----------------
+
var sitesURL = serverURL+'/oa/api/v1/sites';
var loginURL =  serverURL+'/oa/api/session';
+
var toremove=[];
var sitesURL = serverURL+'/oa/api/v1/sites';
+
var now = new Date().getTime();
var toremove=[];
+
var utils = Java.type('ros.CROSUtils');
var now = new Date().getTime();
+
var sessionid="";
var utils = Java.type('ros.CROSUtils');
+
var siteData={};
var sessionid="";
+
var siteret;
var siteData={};
+
login()
var siteret;
+
function hasSubnet(group,subnet){
login()
+
return group.indexOf(subnet)>-1
 
+
}
function hasSubnet(group,subnet){
+
function cleanUP(){
return group.indexOf(subnet)>-1
+
  log.debug("cleanUP");
}
+
  var groups = actions.ros.getCTrafficGroups()
function cleanUP(){
+
  for (var tg in groups.allTrafficGroups){
log.debug("cleanUP");
+
  //log.debug(groups.allTrafficGroups.get(tg));
var groups = actions.ros.getCTrafficGroups()
+
  var thisGroup=groups.allTrafficGroups.get(tg);
for (var tg in groups.allTrafficGroups){
+
  if (typeof siteData[thisGroup.name]==='undefined'){
//log.debug(groups.allTrafficGroups.get(tg));
+
    log.debug("removing:"+thisGroup.name+" "+thisGroup.getId());
var thisGroup=groups.allTrafficGroups.get(tg);
+
    toremove.push(thisGroup.getId())
if (typeof siteData[thisGroup.name]==='undefined'){
+
  }else{
log.debug("removing:"+thisGroup.name+" "+thisGroup.getId());
+
    log.debug("not removing:"+thisGroup.name);
toremove.push(thisGroup.getId())
+
    var groupSubnets=thisGroup.getSubnets();
}else{
+
    var it = groupSubnets.iterator();
log.debug("not removing:"+thisGroup.name);
+
    while (it.hasNext()){
var groupSubnets=thisGroup.getSubnets();
+
    var s =it.next();
var it = groupSubnets.iterator();
+
    //log.debug(s)
while (it.hasNext()){
+
    var key=utils.inet_btoa(s.getStartEntry())+"-"+utils.inet_btoa(s.getEndEntry());
var s =it.next();
+
    if (!hasSubnet(siteData[thisGroup.name],key)){
//log.debug(s)
+
    it.remove();
var key=utils.inet_btoa(s.getStartEntry())+"-"+utils.inet_btoa(s.getEndEntry());
+
    actions.getDatabaseManager().executePrepared("delete from trafficgroupsubnets where id=? and startip=? and endip=?",[[thisGroup.getId(),s.getStartEntry(),s.getEndEntry()]])
if (!hasSubnet(siteData[thisGroup.name],key)){
+
    log.debug("removing subnet from "+thisGroup.name +" "+key+" "+hasSubnet(siteData[thisGroup.name],key));
it.remove();
+
      }
actions.getDatabaseManager().executePrepared("delete from trafficgroupsubnets where id=? and startip=? and endip=?",[[thisGroup.getId(),s.getStartEntry(),s.getEndEntry()]])
+
    }
log.debug("removing subnet from "+thisGroup.name +" "+key+" "+hasSubnet(siteData[thisGroup.name],key));
+
  }
  }
+
  }
}
+
  for (var i in toremove){
}
+
  log.debug("Removing full site:"+toremove[i]);
}
+
    actions.ros.removeTrafficGroup(toremove[i]);
  for (var i in toremove){
+
    }
log.debug("Removing full site:"+toremove[i]);
+
}
  actions.ros.removeTrafficGroup(toremove[i]);
+
function findInterfacesForSites(trafficgroup,ip){
  }
+
  //log.debug("findInterfacesForSites:"+trafficgroup+" "+ip);
}
+
  for (var deviceid in actions.getDevices()){
function findInterfacesForSites(trafficgroup,ip){
+
  var device =actions.getDevice(deviceid);  
  //log.debug("findInterfacesForSites:"+trafficgroup+" "+ip);
+
  for (var d in device.interfaces()){
  for (var deviceid in actions.getDevices()){
+
    var thisIn = device.interfaces().get(d);
var device =actions.getDevice(deviceid);  
+
    var interfaceIPS = thisIn.getInterfaceIPs()
for (var d in device.interfaces()){
+
    if (!interfaceIPS.isEmpty()){
var thisIn = device.interfaces().get(d);
+
    //log.debug(d+" "+thisIn+" "+interfaceIPS);
var interfaceIPS = thisIn.getInterfaceIPs()
+
    for (var ips in interfaceIPS){
if (!interfaceIPS.isEmpty()){
+
      var thisip = interfaceIPS.get(ips)
//log.debug(d+" "+thisIn+" "+interfaceIPS);
+
      if (utils.isIPInSubnet(ip,utils.inet_btoa(thisip.getIp()),utils.inet_btoa(thisip.getMask()))){
for (var ips in interfaceIPS){
+
      log.debug("Adding device/interface to group "+ trafficgroup+" device:"+device.getIP()+" ifindex:"+thisIn.getIfindex());
var thisip = interfaceIPS.get(ips)
+
      trafficgroup.addDevice(device.getIP(),thisIn.getIfindex(),actions.ros);
if (utils.isIPInSubnet(ip,utils.inet_btoa(thisip.getIp()),utils.inet_btoa(thisip.getMask()))){
+
      }
log.debug("Adding device/interface to group "+ trafficgroup+" device:"+device.getIP()+" ifindex:"+thisIn.getIfindex());
+
    }
trafficgroup.addDevice(device.getIP(),thisIn.getIfindex(),actions.ros);
+
    }
}
+
  }
}
+
  }
}
+
}
}
+
function processSites(){
}
+
  if (typeof siteret!=='undefined'){
}
+
  //log.debug(typeof siteret);
function processSites(){
+
  //log.debug("processSites:"+siteret.length());
if (typeof siteret!=='undefined'){
+
  for (var i=0;i<siteret.length();i++){
//log.debug(typeof siteret);
+
    var thisone = siteret.getJSONObject(i);
//log.debug("processSites:"+siteret.length());
+
    //log.debug(i+" "+thisone);
for (var i=0;i<siteret.length();i++){
+
    var location = thisone.getJSONObject("location")
var thisone = siteret.getJSONObject(i);
+
    var siteName = thisone.getString("name");
//log.debug(i+" "+thisone);
+
    if (typeof siteData[siteName]==='undefined'){
var location = thisone.getJSONObject("location")
+
    siteData[siteName]=[];
var siteName = thisone.getString("name");
+
    }
if (typeof siteData[siteName]==='undefined'){
+
    var siteDescription = location.getString("name");
siteData[siteName]=[];
+
    var subnets = thisone.getJSONArray("subnets");
}
+
    for (var s=0;s<subnets.length();s++){
var siteDescription = location.getString("name");
+
      var thisSubnet=subnets.get(s).getString("value").replace(/\s/g, "");
var subnets = thisone.getJSONArray("subnets");
+
    //log.debug(i+" "+siteName+" -- "+siteDescription+" "+thisSubnet);
for (var s=0;s<subnets.length();s++){
+
      if (thisSubnet.indexOf("-")>-1){
  var thisSubnet=subnets.get(s).getString("value").replace(/\s/g, "");
+
      var trafficgroup=   actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(thisSubnet.split("-")[0]),utils.inet_atob(thisSubnet.split("-")[1]),false,null,false,-1);
//log.debug(i+" "+siteName+" -- "+siteDescription+" "+thisSubnet);
+
      siteData[siteName].push(thisSubnet.split("-")[0]+"-"+thisSubnet.split("-")[1])
  if (thisSubnet.indexOf("-")>-1){
+
      findInterfacesForSites(trafficgroup,thisSubnet.split("-")[0]);
  var trafficgroup=   actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(thisSubnet.split("-")[0]),utils.inet_atob(thisSubnet.split("-")[1]),false,null,false,-1);
+
      }else if (thisSubnet.indexOf("/")>-1){
siteData[siteName].push(thisSubnet.split("-")[0]+"-"+thisSubnet.split("-")[1])
+
      //log.debug(thisSubnet);
findInterfacesForSites(trafficgroup,thisSubnet.split("-")[0]);
+
      var d=utils.getIPRange(thisSubnet.split("/"));
  }else if (thisSubnet.indexOf("/")>-1){
+
      var trafficgroup= actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,d[0],d[1],false,null,false,-1);
//log.debug(thisSubnet);
+
    siteData[siteName].push(utils.inet_btoa(d[0])+"-"+utils.inet_btoa(d[1]))
var d=utils.getIPRange(thisSubnet.split("/"));
+
    findInterfacesForSites(trafficgroup,utils.inet_btoa(d[0]));
  var trafficgroup= actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,d[0],d[1],false,null,false,-1);
+
    //log.debug(d);
siteData[siteName].push(utils.inet_btoa(d[0])+"-"+utils.inet_btoa(d[1]))
+
      //log.debug(d[0]+" - "+d[1]);
findInterfacesForSites(trafficgroup,utils.inet_btoa(d[0]));
+
      }else{
//log.debug(d);
+
      log.debug("Unknown subent type:"+thisSubnet);
//log.debug(d[0]+" - "+d[1]);
+
      }
  }else{
+
    //actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(
  log.debug("Unknown subent type:"+thisSubnet);
+
    }
  }
+
  }
+
  }else{
//actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(
+
  log.debug(typeof siteret);
}
+
  log.debug("cant processSites:"+siteret);
}
+
  }
}else{
+
    cleanUP()
log.debug(typeof siteret);
+
    log.debug(JSON.stringify(siteData["Denver"]));
log.debug("cant processSites:"+siteret);
+
}
}
+
function logOut(){
+
  log.debug("logOut with sessionid:"+sessionid);
  cleanUP()
+
  var data = {"session" :sessionid}
  log.debug(JSON.stringify(siteData["Denver"]));
+
  var con = new java.net.URL(loginURL).openConnection();
}
+
  con.requestMethod = "DELETE";
 
+
  con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
function logOut(){
+
  //con.setRequestProperty("SessionApexId",sessionid);
log.debug("logOut with sessionid:"+sessionid);
+
  var ret = asResponse(con);
var data = {"session" :sessionid}
+
  log.warn(ret)
var con = new java.net.URL(loginURL).openConnection();
+
  log.warn(ret.data.toString())
con.requestMethod = "DELETE";
+
  processSites()
con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
+
}
//con.setRequestProperty("SessionApexId",sessionid);
+
function login(){
var ret = asResponse(con);
+
  log.debug("Doing apex login");
log.warn(ret)
+
  var data = {"session" : {"username" : user,"password" : password}}
log.warn(ret.data.toString())
+
  var ret = httpRequest("POST",loginURL,JSON.stringify(data),"application/json","application/json");
processSites()
+
  if (parseInt(ret.statusCode)===200&&sessionid!==""){
}
+
  log.warn("Valid login:"+sessionid);
 
+
  getSites()
function login(){
+
  }else{
log.debug("Doing apex login");
+
  log.warn("Invalid login:"+JSON.parse(ret.statusCode));
var data = {"session" : {"username" : user,"password" : password}}
+
  }
var ret = httpRequest("POST",loginURL,JSON.stringify(data),"application/json","application/json");
+
  //log.warn(ret.statusCode)
if (parseInt(ret.statusCode)===200&&sessionid!==""){
+
  //log.warn(ret)
log.warn("Valid login:"+sessionid);
+
}
getSites()
+
function getSites(){
}else{
+
  log.debug("getSites with sessionid:"+sessionid);
log.warn("Invalid login:"+JSON.parse(ret.statusCode));
+
  var data = {}
}
+
  var con = new java.net.URL(sitesURL+"?SessionApexId="+sessionid).openConnection();
//log.warn(ret.statusCode)
+
  con.requestMethod = "GET";
//log.warn(ret)
+
  con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
}
+
  con.setRequestProperty("Content-Type", "application/json");
 
+
  con.setRequestProperty("Accept", "application/json");
function getSites(){
+
  siteret = utils.JSONObject(asResponse(con).data.toString()).getJSONObject("data").getJSONArray("sites");
log.debug("getSites with sessionid:"+sessionid);
+
  //log.warn(siteret)
var data = {}
+
  //log.warn(siteret.data.toString())
var con = new java.net.URL(sitesURL+"?SessionApexId="+sessionid).openConnection();
+
  logOut(sessionid)  
con.requestMethod = "GET";
+
}
con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
+
//dont touch anything below here
con.setRequestProperty("Content-Type", "application/json");
+
function httpRequest(method,theUrl, data, contentType,contentAccept){
con.setRequestProperty("Accept", "application/json");
+
    method = method || "POST";
siteret = utils.JSONObject(asResponse(con).data.toString()).getJSONObject("data").getJSONArray("sites");
+
  contentAccept = contentAccept || "application/json";
//log.warn(siteret)
+
    contentType = contentType || "application/json";
//log.warn(siteret.data.toString())
+
    var con = new java.net.URL(theUrl).openConnection();
logOut(sessionid)
+
    con.requestMethod = method;
}
+
    con.setRequestProperty("Content-Type", contentType);
 
+
    con.setRequestProperty("Accept", contentAccept);
//dont touch anything below here
+
    // Send post request
function httpRequest(method,theUrl, data, contentType,contentAccept){
+
    con.doOutput=true;
  method = method || "POST";
+
    write(con.outputStream, data);
  contentAccept = contentAccept || "application/json";
+
  var headers = con.getHeaderFields();
  contentType = contentType || "application/json";
+
  log.debug(con.getHeaderFields())
  var con = new java.net.URL(theUrl).openConnection();
+
  if (headers.containsKey("Set-Cookie")){
  con.requestMethod = method;
+
  var d = headers.get("Set-Cookie")
  con.setRequestProperty("Content-Type", contentType);
+
  log.debug(d);
  con.setRequestProperty("Accept", contentAccept);
+
  for (var e in d){
  // Send post request
+
    log.debug(e+" "+d[e]);
  con.doOutput=true;
+
    if (d[e].indexOf("SessionApexId")>-1){
  write(con.outputStream, data);
+
    // 0 SessionApexId=721ea868799a2112dbb81c; Path=/; secure; HttpOnly
var headers = con.getHeaderFields();
+
    var f = d[e].split("; ");
log.debug(con.getHeaderFields())
+
    for (var g in f){
if (headers.containsKey("Set-Cookie")){
+
      if (f[g].indexOf("SessionApexId")>-1){
var d = headers.get("Set-Cookie")
+
        sessionid=f[g].split("=")[1];
log.debug(d);
+
        log.debug("Setting sessionid:"+sessionid);
for (var e in d){
+
      }
log.debug(e+" "+d[e]);
+
    }
if (d[e].indexOf("SessionApexId")>-1){
+
    }
// 0 SessionApexId=721ea868799a2112dbb81c; Path=/; secure; HttpOnly
+
  }
var f = d[e].split("; ");
+
  }
for (var g in f){
+
    return asResponse(con);
if (f[g].indexOf("SessionApexId")>-1){
+
}
  sessionid=f[g].split("=")[1];
+
function asResponse(con){
  log.debug("Setting sessionid:"+sessionid);
+
    var d = read(con.inputStream);
}
+
    return {data : d, statusCode : con.responseCode};
}
+
}
}
+
function write(outputStream, data){
}
+
    var wr = new java.io.DataOutputStream(outputStream);
 
+
    wr.writeBytes(data);
}
+
    wr.flush();
  return asResponse(con);
+
    wr.close();
}
+
}
function asResponse(con){
+
function read(inputStream){
  var d = read(con.inputStream);
+
    var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
  return {data : d, statusCode : con.responseCode};
+
    var inputLine;
}
+
    var response = new java.lang.StringBuffer();
function write(outputStream, data){
+
    while ((inputLine = inReader.readLine()) != null) {
  var wr = new java.io.DataOutputStream(outputStream);
+
    response.append(inputLine);
  wr.writeBytes(data);
+
    }
  wr.flush();
+
    inReader.close();
  wr.close();
+
    return response.toString();
}
+
}
function read(inputStream){
+
  var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
+
  var inputLine;
+
  var response = new java.lang.StringBuffer();
+
  while ((inputLine = inReader.readLine()) != null) {
+
  response.append(inputLine);
+
  }
+
  inReader.close();
+
  return response.toString();
+
}
+

Latest revision as of 16:04, 22 March 2022

var serverURL = "https://apex.usa.viavi.solutions";
var user="api";
var password="duijfhdlifhlziuhflsuhfgulh";
//-------Only change whats above this line----------------
var loginURL =  serverURL+'/oa/api/session';
var sitesURL = serverURL+'/oa/api/v1/sites';
var toremove=[];
var now = new Date().getTime();
var utils = Java.type('ros.CROSUtils');
var sessionid="";
var siteData={};
var siteret;
login()
function hasSubnet(group,subnet){
return group.indexOf(subnet)>-1
}
function cleanUP(){
 log.debug("cleanUP");
 var groups = actions.ros.getCTrafficGroups()
 for (var tg in groups.allTrafficGroups){
  //log.debug(groups.allTrafficGroups.get(tg));
  var thisGroup=groups.allTrafficGroups.get(tg);
  if (typeof siteData[thisGroup.name]==='undefined'){
   log.debug("removing:"+thisGroup.name+" "+thisGroup.getId());
   toremove.push(thisGroup.getId())
  }else{
   log.debug("not removing:"+thisGroup.name);
   var groupSubnets=thisGroup.getSubnets();
   var it = groupSubnets.iterator();
   while (it.hasNext()){
    var s =it.next();
    //log.debug(s)
    var key=utils.inet_btoa(s.getStartEntry())+"-"+utils.inet_btoa(s.getEndEntry());
    if (!hasSubnet(siteData[thisGroup.name],key)){
    it.remove();
    actions.getDatabaseManager().executePrepared("delete from trafficgroupsubnets where id=? and startip=? and endip=?",thisGroup.getId(),s.getStartEntry(),s.getEndEntry())
    log.debug("removing subnet from "+thisGroup.name +" "+key+" "+hasSubnet(siteData[thisGroup.name],key));
     }
   }
  }
 }
  for (var i in toremove){
  log.debug("Removing full site:"+toremove[i]);
    actions.ros.removeTrafficGroup(toremove[i]);
    }
}
function findInterfacesForSites(trafficgroup,ip){
  //log.debug("findInterfacesForSites:"+trafficgroup+" "+ip);
  for (var deviceid in actions.getDevices()){
  var device =actions.getDevice(deviceid); 
  for (var d in device.interfaces()){
   var thisIn = device.interfaces().get(d);
   var interfaceIPS = thisIn.getInterfaceIPs()
   if (!interfaceIPS.isEmpty()){
    //log.debug(d+" "+thisIn+" "+interfaceIPS);
    for (var ips in interfaceIPS){
     var thisip = interfaceIPS.get(ips)
     if (utils.isIPInSubnet(ip,utils.inet_btoa(thisip.getIp()),utils.inet_btoa(thisip.getMask()))){
      log.debug("Adding device/interface to group "+ trafficgroup+" device:"+device.getIP()+" ifindex:"+thisIn.getIfindex());
      trafficgroup.addDevice(device.getIP(),thisIn.getIfindex(),actions.ros);
     }
    }
   }
  }
 }
}
function processSites(){
 if (typeof siteret!=='undefined'){
  //log.debug(typeof siteret);
  //log.debug("processSites:"+siteret.length());
  for (var i=0;i<siteret.length();i++){
   var thisone = siteret.getJSONObject(i);
   //log.debug(i+" "+thisone);
   var location = thisone.getJSONObject("location")
   var siteName = thisone.getString("name");
   if (typeof siteData[siteName]==='undefined'){
    siteData[siteName]=[];
   }
   var siteDescription = location.getString("name");
   var subnets = thisone.getJSONArray("subnets");
   for (var s=0;s<subnets.length();s++){
     var thisSubnet=subnets.get(s).getString("value").replace(/\s/g, "");
    //log.debug(i+" "+siteName+" -- "+siteDescription+" "+thisSubnet);
     if (thisSubnet.indexOf("-")>-1){
      var trafficgroup=    actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(thisSubnet.split("-")[0]),utils.inet_atob(thisSubnet.split("-")[1]),false,null,false,-1);
     siteData[siteName].push(thisSubnet.split("-")[0]+"-"+thisSubnet.split("-")[1])
     findInterfacesForSites(trafficgroup,thisSubnet.split("-")[0]);
     }else if (thisSubnet.indexOf("/")>-1){
     //log.debug(thisSubnet);
     var d=utils.getIPRange(thisSubnet.split("/"));
      var trafficgroup= actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,d[0],d[1],false,null,false,-1);
    siteData[siteName].push(utils.inet_btoa(d[0])+"-"+utils.inet_btoa(d[1]))
    findInterfacesForSites(trafficgroup,utils.inet_btoa(d[0]));
    //log.debug(d);
     //log.debug(d[0]+" - "+d[1]);
     }else{
     log.debug("Unknown subent type:"+thisSubnet);
     }
    //actions.ros.addTrafficGroup(now,"script",siteName,siteDescription,utils.inet_atob(
   }
  }
 }else{
  log.debug(typeof siteret);
  log.debug("cant processSites:"+siteret);
 }
   cleanUP()
   log.debug(JSON.stringify(siteData["Denver"]));
}
function logOut(){
 log.debug("logOut with sessionid:"+sessionid);
 var data = {"session" :sessionid}
 var con = new java.net.URL(loginURL).openConnection();
 con.requestMethod = "DELETE";
 con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
 //con.setRequestProperty("SessionApexId",sessionid);
 var ret = asResponse(con);
 log.warn(ret)
 log.warn(ret.data.toString())
 processSites()
}
function login(){
 log.debug("Doing apex login");
 var data = {"session" : {"username" : user,"password" : password}}
 var ret = httpRequest("POST",loginURL,JSON.stringify(data),"application/json","application/json");
 if (parseInt(ret.statusCode)===200&&sessionid!==""){
  log.warn("Valid login:"+sessionid);
  getSites()
 }else{
  log.warn("Invalid login:"+JSON.parse(ret.statusCode));
 }
 //log.warn(ret.statusCode)
 //log.warn(ret)
}
function getSites(){
 log.debug("getSites with sessionid:"+sessionid);
 var data = {}
 var con = new java.net.URL(sitesURL+"?SessionApexId="+sessionid).openConnection();
 con.requestMethod = "GET";
 con.setRequestProperty("Cookie","SessionApexId="+sessionid+";");
 con.setRequestProperty("Content-Type", "application/json");
 con.setRequestProperty("Accept", "application/json");
 siteret = utils.JSONObject(asResponse(con).data.toString()).getJSONObject("data").getJSONArray("sites");
 //log.warn(siteret)
 //log.warn(siteret.data.toString())
 logOut(sessionid) 
}
//dont touch anything below here
function httpRequest(method,theUrl, data, contentType,contentAccept){
   method = method || "POST";
  contentAccept = contentAccept || "application/json";
   contentType = contentType || "application/json";
   var con = new java.net.URL(theUrl).openConnection();
   con.requestMethod = method;
   con.setRequestProperty("Content-Type", contentType);
   con.setRequestProperty("Accept", contentAccept);
   // Send post request
   con.doOutput=true;
   write(con.outputStream, data);
 var headers = con.getHeaderFields();
 log.debug(con.getHeaderFields())
 if (headers.containsKey("Set-Cookie")){
  var d = headers.get("Set-Cookie")
  log.debug(d);
  for (var e in d){
   log.debug(e+" "+d[e]);
    if (d[e].indexOf("SessionApexId")>-1){
    // 0 SessionApexId=721ea868799a2112dbb81c; Path=/; secure; HttpOnly
    var f = d[e].split("; ");
    for (var g in f){
     if (f[g].indexOf("SessionApexId")>-1){
       sessionid=f[g].split("=")[1];
       log.debug("Setting sessionid:"+sessionid);
     }
    }
   }
  }
 }
   return asResponse(con);
}
function asResponse(con){
   var d = read(con.inputStream);
   return {data : d, statusCode : con.responseCode};
}
function write(outputStream, data){
   var wr = new java.io.DataOutputStream(outputStream);
   wr.writeBytes(data);
   wr.flush();
   wr.close();
}
function read(inputStream){
   var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
   var inputLine;
   var response = new java.lang.StringBuffer();
   while ((inputLine = inReader.readLine()) != null) {
    response.append(inputLine);
   }
   inReader.close();
   return response.toString();
}