4.2 Using JavaScript and XHTML

Using the REST protocol it is possible to access Operations Center data using JavaScript. The data returns as an XML document, which can be easily manipulated by an XML parser. The sample code provided in this section is intended to convey the simplicity of using the REST API and not intended as a foundation for building a solution.

4.2.1 Creating a Session

To access data from Operations Center, a session must be initiated first with a user name and password. The user name must be a valid account in Operations Center.

Use the following JavaScript code sample, which illustrates session creation using JavaScript and XML HTTP Request, to create a session:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Web 2.0 Connect</title>
  </head>
  <body>
    <script language="javascript">
     // Web 2.0 Connect host
    var NETIQ_OPERATIONS_CTR_DASHBOARD_HOST = "manor-as08-v";
    var NETIQ_OPERATIONS_CTR_DASHBOARD_PORT = 8080;
    function getWeb2ConnectRootUrl()
    {
      return "http://" + NETIQ_OPERATIONS_CTR_DASHBOARD_HOST  + ":" + NETIQ_OPERATIONS_CTR_DASHBOARD_PORT + "/moweb2cn/";
    }
     function createWeb2ConnectSession(username, password)
    {
      var url = getWeb2ConnectRootUrl() + "rest/session/?username=" + username + "&password=" + password;
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.open("GET", url, true);
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          if (xhr.responseText)
          {
            alert(xhr.responseText);
          }
        }
      };
      xhr.send();
    }
    createWeb2ConnectSession("admin", "formula");
     </script>
  </body>
</html>

NOTE:If running in Firefox 3.0, supply a null parameter to xhr.send(), as follows:

xhr.send(null)

When the Web page is loaded, the createWeb2ConnectSession() function is invoked. If the session creation is successful a message box displays the XML responses and the <value/> element within the XML response contains the string OK:

After a session is created, subsequent attempts to create a session before the session expires, result in an error.

4.2.2 Reading an Element

After a session is successfully created, other REST APIs can be used. An element can be read using the element/value mount point.

To read an element:

  1. Use the following JavaScript code sample, which illustrates retrieving element information using JavaScript, to read an element:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Web 2.0 Connect</title>
      </head>
      <body>
        <script language="javascript">
        // Web 2.0 Connect host
        var NETIQ_OPERATIONS_CTR_DASHBOARD_HOST = "manor-as08-v";
        var NETIQ_OPERATIONS_CTR_DASHBOARD_PORT = 8080;
        var SESSION_ACTIVE = false;
        function getWeb2ConnectRootUrl()
        {
          return "http://" + NETIQ_OPERATIONS_CTR_DASHBOARD_HOST  + ":" + NETIQ_OPERATIONS_CTR_DASHBOARD_PORT + "/moweb2cn/";
        }
        function createWeb2ConnectSession(username, password)
        {
          var url = getWeb2ConnectRootUrl() + "rest/session/?username=" + username + "&password=" + password;
          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
          xhr.onreadystatechange = function()
          {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
              SESSION_ACTIVE = true;
              debug.innerText = xhr.responseText;
            }
            else
            {
              debug.innerText = xhr.responseText;
            }
          };
          xhr.open("GET", url, true);
          xhr.send();
        }
         function getWeb2ConnectElementValue(identity)
        {
          var url = getWeb2ConnectRootUrl() + "rest/element/value/" + identity;
          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
          xhr.onreadystatechange = function()
          {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
              SESSION_ACTIVE = true;
              debug.innerText = xhr.responseText;
            }
            else
            {
              debug.innerText = xhr.responseText;
            }
          };
          xhr.open("GET", url, true);
          xhr.send();
        }
         createWeb2ConnectSession("admin", "formula");
        </script>
        <textarea cols="100" rows="15" id="debug"></textarea>
        </br>
        <input type="button" onclick="getWeb2ConnectElementValue('root=Administration')" value="Element Value"/>
      </body>
    </html>
    

    When the Web page is loaded a Web 2.0 Connect session is created by the createWeb2ConnectSession() function and the text area display the result of the session creation operation:

  2. After a session is successfully created, click Element Value to invoke the getWeb2ConnectElementValue() function that performs a REST element/value query for the element having the DName root=Administration.

    The text area in the HTML page populates with the XML response for the element value query:

4.2.3 Encoding an Element DName

When using Web 2.0 Connect, it is often necessary to encode the element’s dName in base64. The following example shows how to encode the dname using JavaScript; which is basically a JavaScript translation of the encode utility that is provided in the Operations Center mocommon.jar library:

var Base64 
    // private property
    _keyStr :    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./",
           tob64 : function ( buffer) {
           var notleading = false;
           var len = buffer.length;
           var pos = len % 3;
           var c;
           var b0 = 0;
           var b1 = 0;
           var b2 = 0;
           var sb = "";
           if ( pos == 1)
           {
               b2 = buffer.charCodeAt(0);
           }
           else if (pos == 2)
           {
               b1 = buffer.charCodeAt(0);
               b2 = buffer.charCodeAt(1);     
           }
           while ( true )
           {
               c = (b0 & 0xfc) >>> 2;
               if(notleading || c != 0) {
                   sb = sb + this._keyStr.charAt(c);
                   notleading = true;
               }
               c = ((b0 & 3) << 4) | ((b1 & 0xf0) >>> 4);
               if(notleading || c != 0) {
                   sb = sb + this._keyStr.charAt(c);
                   notleading = true;
               }
               c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >>> 6);

               if(notleading || c != 0) {
                   sb = sb + this._keyStr.charAt(c);
                   notleading = true;
               }
               c = b2 & 0x3f;
               if(notleading || c != 0) {
                   sb = sb + this._keyStr.charAt(c);
                   notleading = true;
               }
               if(pos >= len)
                   break;
               else
                   try {
                       b0 = buffer.charCodeAt(pos++);
                       b1 = buffer.charCodeAt(pos++);
                       b2 = buffer.charCodeAt(pos++);
                   } catch(Exception) { break; }
           }
           if(notleading)
               return sb;
           else
               return "0";
           ;
       }
       }
alert(Base64.tob64(’identity:dname:’ + 'org=%2F+root/desktop=Under+System/org=Another+Example/root=Organizations')
 );

For more information about base64 encoding of element DNames, see Section 4.1.2, Reading an Element.

4.2.4 Configuring Session Retention

The Web 2.0 Connect session remains active for the duration specified by the Tomcat configuration. After session expiration, subsequent calls to REST APIs fails. Web 2.0 Connect provides a simple ping capability for maintaining a session.

To configure session retention:

  1. Use the following JavaScript code sample, which illustrates performing a REST ping using JavaScript, to configure session retention:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Web 2.0 Connect</title>
      </head>
      <body>
        <script language="javascript">
        // Web 2.0 Connect host
        var NETIQ_OPERATIONS_CTR_DASHBOARD_HOST = "manor-as08-v";
        var NETIQ_OPERATIONS_CTR_DASHBOARD_PORT = 8080;
        function getWeb2ConnectRootUrl()
        {
          return "http://" + NETIQ_OPERATIONS_CTR_DASHBOARD_HOST  + ":" + NETIQ_OPERATIONS_CTR_DASHBOARD_PORT + "/moweb2cn/";
        }
        function createWeb2ConnectSession(username, password)
        {
          var url = getWeb2ConnectRootUrl() + "rest/session/?username=" + username + "&password=" + password;
          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
          xhr.onreadystatechange = function()
          {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
              debug.innerText = xhr.responseText;
            }
            else
            {
              debug.innerText = "Session creation failed";
            }
          };
          xhr.open("GET", url, true);
          xhr.send();
        }
        function getWeb2ConnectElementValue(identity)
        {
          var url = getWeb2ConnectRootUrl() + "rest/element/value/" + identity;
          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
          xhr.onreadystatechange = function()
          {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
              debug.innerText = xhr.responseText;
            }
            else
            {
              debug.innerText = "Element/Value failed";
            }
          };
          xhr.open("GET", url, true);
          xhr.send();
        }
         function doWeb2ConnectPing()
        {
          var url = getWeb2ConnectRootUrl() + "rest/ping";
          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
          xhr.onreadystatechange = function()
          {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
              debug.innerText = xhr.responseText;
            }
            else
            {
              debug.innerText = "Ping failed";
            }
          };
          xhr.open("GET", url, true);
          xhr.send();
        }
         createWeb2ConnectSession("admin", "formula");
        </script>
        <textarea cols="100" rows="15" id="debug"></textarea>
        </br>
        <input type="button" onclick="getWeb2ConnectElementValue('root=Administration')" value="Element Value"/>
      </body>
    </html>
    
  2. After a session is successfully created, click Ping to invoke the doWeb2ConnectPing() function that performs a REST ping.

    The text area in the HTML page populates with the XML response for the ping query:

4.2.5 Performing an Operation

Invoking an operation requires a remote call rest/element/perform method. This method requires an identity to be passed in the URL, as well as the name of the operation to invoke:

http://OperationsCenterServer:port/moweb2cn/rest/element/perform/identity?operationName=operation_name

The following code extends the previous JavaScript and HTML examples to demonstrate invoking an operation:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "htstp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Web 2.0 Connect</title>
    <script language="javascript">
    // Web 2.0 Connect host
    var NETIQ_OPERATIONS_CTR_DASHBOARD_HOST = "manor-as08-v";
    var NETIQ_OPERATIONS_CTR_DASHBOARD_PORT = 8080;
    function getWeb2ConnectRootUrl()
    {
      return "http://" + NETIQ_OPERATIONS_CTR_DASHBOARD_HOST  + ":" + NETIQ_OPERATIONS_CTR_DASHBOARD_PORT + "/moweb2cn/";
    }
    function createWeb2ConnectSession(username, password)
    {
      var url = getWeb2ConnectRootUrl() + "rest/session?username=" + username + "&password=" + password;
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = "Session created\n" + xhr.responseText;
        }

        else
        {
          debug.innerText = "Session creation failed";
        }
      };
      xhr.open("GET", url, true);
      xhr.send();
    }
    function closeWeb2ConnectSession()
    {
      var url = getWeb2ConnectRootUrl() + "rest/session";
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = "Session terminated\n" + xhr.responseText;
        }
        else
        {
          debug.innerText = "Session termination failed";
        }
      };
      xhr.open("DELETE", url, true);
      xhr.send();
    }
    function getWeb2ConnectElementValue(identity)
    {
      var url = getWeb2ConnectRootUrl() + "rest/element/value/" + identity;
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = xhr.responseText;
        }
        else
        {
          debug.innerText = "Element/Value failed";
        }
      };
      xhr.open("GET", url, true);
      xhr.send();
    }
    function doWeb2ConnectPing()
    {
      var url = getWeb2ConnectRootUrl() + "rest/ping";
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = "Ping successful\n" + xhr.responseText;
        }
        else
        {
          debug.innerText = "Ping failed";
        }
      };
      xhr.open("GET", url, true);
      xhr.send();
    }
     function doWeb2ConnectPerformOperation()
    {
      var url = getWeb2ConnectRootUrl() + "rest/element/perform/root=Administration?operationName=Debug";
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = "Operation successful\n" + xhr.responseText;
        }
        else
        {
          debug.innerText = "Operation failed";
        }
      };
      xhr.open("GET", url, true);
      xhr.send();
    }
     function pageLoad()
    {
      createWeb2ConnectSession("admin", "formula");
    }
    </script>
  </head>
  <body onload="pageLoad()">
    <textarea cols="100" rows="15" id="debug"></textarea>
    </br>
    <input type="button" onclick="getWeb2ConnectElementValue('root=Administration')" value="Element Value"/>
    <input type="button" onclick="doWeb2ConnectPing()" value="Ping"/>
    <input type="button" onclick="doWeb2ConnectPerformOperation()" value="Operation"/>
    <input type="button" onclick="closeWeb2ConnectSession()" value="End Session"/>
  </body>
</html>

This example requires an operation named Debug to have been defined in Operations Center, as follows:

The operation is server side with element context. The operation simply writes the name of the element it was invoked against to the Operations Center trace log using the following script:

formula.log.info(element.Name + " ‑ " + java.lang.System.currentTimeMillis());

4.2.6 Closing a Session

Closing a session using JavaScript using XML HTTP Request simply requires a remote call to the rest/session method with an HTTP DELETE method:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Web 2.0 Connect</title>
    <script language="javascript">
    // Web 2.0 Connect host
    var NETIQ_OPERATIONS_CTR_DASHBOARD_HOST = "manor-as08-v";
    var NETIQ_OPERATIONS_CTR_DASHBOARD_PORT = 8080;
    function getWeb2ConnectRootUrl()
    {
      return "http://" + NETIQ_OPERATIONS_CTR_DASHBOARD_HOST  + ":" + NETIQ_OPERATIONS_CTR_DASHBOARD_PORT + "/moweb2cn/";
    }
    function createWeb2ConnectSession(username, password)
    {
      var url = getWeb2ConnectRootUrl() + "rest/session/?username=" + username + "&password=" + password;
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
      xhr.open("GET", url, true);
      xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          if (xhr.responseText)
          {
            alert(xhr.responseText);
          }
        }
      };
      xhr.send();
    }
     function closeWeb2ConnectSession()
    {
      var url = getWeb2ConnectRootUrl() + "rest/session";
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
       xhr.onreadystatechange = function()
      {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
          debug.innerText = "Session terminated\n" + xhr.responseText;
        }
        else
        {
          debug.innerText = "Session termination failed";
        }
      };
      xhr.open("DELETE", url, true);
      xhr.send();
    }
    function pageLoad()
    {
      createWeb2ConnectSession("admin", "formula");
    }
     </script>
  </head>
   <body onload="pageLoad()">
    <textarea cols="100" rows="15" id="debug"></textarea>
    </br>
    <input type="button" onclick="closeWeb2ConnectSession()" value="End Session"/>
   </body>
</html>