Skip to content

Instantly share code, notes, and snippets.

@MarkBluemel
Last active December 7, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkBluemel/cdae45a28b17add40a1f7a93beb896ea to your computer and use it in GitHub Desktop.
Save MarkBluemel/cdae45a28b17add40a1f7a93beb896ea to your computer and use it in GitHub Desktop.
HTML/Javascript samples of using the MQ 9.0.1 REST API
The samples included illustrate how HTML with embedded JavaScript may be used to access MQ Installation and MQ Queue Manager data using the new REST API at MQ 9.0.1
Disclaimer: The author is a Java programmer, not a HTML or JavaScript developer. The samples are functional, but are neither elegant nor recommended practice.
In each case the HTML files can take query parameters to control their actions :-
host=name to specify the name of a host to connect to (default is localhost)
port=port number to connect to (default is 9080)
For simplicity these examples are coded to connect using http, not https.
Copyright (c) 2016 IBM Corp.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!-- -->
<!-- Copyright (c) 2016 IBM Corp. -->
<!-- -->
<!-- Permission is hereby granted, free of charge, to any person -->
<!-- obtaining a copy of this software and associated documentation -->
<!-- files (the "Software"), to deal in the Software without -->
<!-- restriction, including without limitation the rights to use, -->
<!-- copy, modify, merge, publish, distribute, sublicense, and/or -->
<!-- sell copies of the Software, and to permit persons to whom the -->
<!-- Software is furnished to do so, subject to the following -->
<!-- conditions: -->
<!-- -->
<!-- The above copyright notice and this permission notice shall be -->
<!-- included in all copies or substantial portions of the Software. -->
<!-- -->
<!-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -->
<!-- ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -->
<!-- TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -->
<!-- PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -->
<!-- SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR -->
<!-- ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -->
<!-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -->
<!-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -->
<!-- OR OTHER DEALINGS IN THE SOFTWARE. -->
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="style.css" rel="stylesheet" type="text/css" />
<title>MQ REST API Sample - Installation Query</title>
</head>
<body>
<div class="main">
<h2>MQ REST API Sample - Installation query </h2>
<p>This sample demonstrates an MQ REST API Query retrieving the definition of the current installation.</p>
<h2>The URL being accessed is:-</h2>
<!-- The javascript will update this paragraph with the URL actually used -->
<p id="urlParagraph"></p>
<!-- The javascript will update this paragraph with the query results or details of any error encountered -->
<p id="instList"></p>
<script type="text/javascript">
// Extract parameters from the query portion of the URL
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
var host=getUrlParameter("host");
if (host == "") { // default
host="localhost";
}
var port=getUrlParameter("port");
if (port == "") { // default
port="9080";
}
// We have host and port, so can construct the URL (and tell the user what it is)
var url = "http://" + host + ":" + port + "/ibmmq/rest/v1/installation?attributes=extended";
document.getElementById("urlParagraph").innerHTML = url;
// setup a suitable HTTP request, and execute it asynchronously
try {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
pollStateChanged(this);
};
request.open("GET", url, true);
request.send();
}
catch (err) {
console.log(err)
}
// handle the asynchronous aspect of the request
function pollStateChanged(xhr) {
if (xhr.readyState == 4) /* DONE */{
var nextpolldelay = 500;
if (xhr.status == 200) {
// We have some data
var respData = JSON.parse(xhr.response);
// The returned JSON Object contains a single property named "installation"
// which contains an array of JSON Objects describing installations
var installations = respData.installation;
var instLen = installations.length;
var text = "<h2>Query Results</h2>";
text += "<table>";
text += "<tr>";
text += "<th style=\"text-align:left\">Name</th>";
text += "<th style=\"text-align:left\">Host</th>";
text += "<th style=\"text-align:left\">Platform(Operating System)</th>";
text += "<th style=\"text-align:left\">MQ Version(build level) -<br>maximum command level</th>";
text += "<th style=\"text-align:left\">Paths</th>";
text += "<th style=\"text-align:left\">Primary<br>Installation?</th>"
text += "<th style=\"text-align:left\">Description</th>";
text += "</tr>";
for (i=0; i < instLen; i++) {
text += "<tr>";
// Basic data is directly referenced
var inst = installations[i];
//extended data is in a nested JSON Object in the property "extended"
var extended = inst.extended;
text += "<td>" + inst.name + "</td>";
text += "<td>" + extended.hostName + "</td>";
text += "<td>" + inst.platform + "(" + extended.operatingSystem + ")" + "</td>";
text += "<td>" + inst.version + "(" + extended.level + ") - " + extended.maxCommandLevel + "</td>";
text +=
"<td>" +
"<table>" +
"<tr>" +
"<td>Installation :<br>Data :</td>" +
"<td>" + extended.installationPath + "<br>" + extended.dataPath + "</td>" +
"</tr>" +
"</table>" +
"</td>";
text += "<td>" + extended.primary + "</td>";
text += "<td>" + extended.description + "</td>";
text += "</tr>";
}
text += "</table>"
document.getElementById("instList").innerHTML = text;
} else {
if (xhr.status != 204) {
// If we've got some diagnostics, show them
var respData = JSON.parse(xhr.response);
if ("error" in respData) {
var error = respData["error"][0];
var text ="<h2>Error Data</h2>";
text += "<table>"
var fields = ["type", "msgId", "message", "explanation", "action"];
var flen = fields.length;
for (i = 0; i < flen; i++ ) {
var field = fields[i];
if (field in error) {
text +=
"<tr>" +
"<td>" + "<strong>" + field + ": " + "</strong>" + "</td>" +
"<td>" + error[field] + "</td>" +
"</tr>";
}
}
text += "</table>";
document.getElementById("instList").innerHTML = text;
} else {
document.getElementById("instList").innerHTML = xhr.statusText;
}
}
}
}
}
</script>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!-- -->
<!-- Copyright (c) 2016 IBM Corp. -->
<!-- -->
<!-- Permission is hereby granted, free of charge, to any person -->
<!-- obtaining a copy of this software and associated documentation -->
<!-- files (the "Software"), to deal in the Software without -->
<!-- restriction, including without limitation the rights to use, -->
<!-- copy, modify, merge, publish, distribute, sublicense, and/or -->
<!-- sell copies of the Software, and to permit persons to whom the -->
<!-- Software is furnished to do so, subject to the following -->
<!-- conditions: -->
<!-- -->
<!-- The above copyright notice and this permission notice shall be -->
<!-- included in all copies or substantial portions of the Software. -->
<!-- -->
<!-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -->
<!-- ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -->
<!-- TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -->
<!-- PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -->
<!-- SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR -->
<!-- ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -->
<!-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -->
<!-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -->
<!-- OR OTHER DEALINGS IN THE SOFTWARE. -->
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="style.css" rel="stylesheet" type="text/css" />
<title>MQ REST API Sample - Queue Manager Query</title>
</head>
<body>
<div class="main">
<h2>MQ REST API Sample - Queue Manager query </h2>
<p>This sample demonstrates an MQ REST API Query retrieving all queue managers associated with the current installation.</p>
<h2>The URL being accessed is:-</h2>
<!-- The javascript will update this paragraph with the URL actually used -->
<p id="urlParagraph"></p>
<!-- The javascript will update this paragraph with the query results or details of any error encountered -->
<p id="qmgrList"></p>
<script type="text/javascript">
// Extract parameters from the query portion of the URL
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
var host=getUrlParameter("host");
if (host == "") { // default
host="localhost";
}
var port=getUrlParameter("port");
if (port == "") { // default
port="9080";
}
// We have host and port, so can construct the URL (and tell the user what it is)
var url = "http://" + host + ":" + port + "/ibmmq/rest/v1/qmgr?attributes=extended";
document.getElementById("urlParagraph").innerHTML = url;
// setup a suitable HTTP request, and execute it asynchronously
try {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
pollStateChanged(this);
};
request.open("GET", url, true);
request.send();
}
catch (err) {
console.log(err)
}
// handle the asynchronous aspect of the request
function pollStateChanged(xhr) {
if (xhr.readyState == 4) /* DONE */{
if (xhr.status == 200) {
// We have some data
var respData = JSON.parse(xhr.response);
// The returned JSON Object contains a single property named "qmgr"
// which contains an array of JSON Objects describing queue managers
var qmgrs = respData.qmgr;
var qmLen = qmgrs.length;
var text = "<h2>Query Results</h2>";
text += "<table>";
text += "<tr>";
text += "<th>Name</th>";
text += "<th>Status</th>";
text += "<th>Installation</th>";
text += "<th>Default Queue Manager?</th>";
text += "<th>Permit Standby?</th>";
text += "</tr>";
for (i=0; i < qmLen; i++) {
text += "<tr>";
var qm = qmgrs[i];
// Basic data is directly referenced
text += "<td>" + qm.name + "</td>";
text += "<td>" + qm.status + "</td>";
//extended data is in a nested JSON Object in the property "extended"
var extended = qm.extended;
text += "<td>" + extended.installationName + "</td>";
text += "<td>" + extended.isDefaultQmgr + "</td>";
text += "<td>" + extended.permitStandby + "</td>";
text += "</tr>";
}
text += "</table>"
document.getElementById("qmgrList").innerHTML = text;
} else {
if (xhr.status != 204) {
// If we've got some diagnostics, show them
var respData = JSON.parse(xhr.response);
if ("error" in respData) {
var error = respData["error"][0];
var text ="<h2>Error Data</h2>";
text += "<table>"
var fields = ["type", "msgId", "message", "explanation", "action"];
var flen = fields.length;
for (i = 0; i < flen; i++ ) {
var field = fields[i];
if (field in error) {
text +=
"<tr>" +
"<td>" + "<strong>" + field + ": " + "</strong>" + "</td>" +
"<td>" + error[field] + "</td>" +
"</tr>";
}
}
text += "</table>";
document.getElementById("qmgrList").innerHTML = text;
} else {
document.getElementById("qmgrList").innerHTML = xhr.statusText;
}
}
}
}
}
</script>
</div>
</body>
</html>
/* Copyright (c) 2016 IBM Corp.
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software
* and associated documentation files (the "Software"),
* to deal in the Software without restriction,
* including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
html, body {
margin: 0px;
padding: 0px;
border: 0px;
}
body {
font-family: Sans-Serif;
font-size: 10pt;
}
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 5px;
vertical-align: top
text-align: left
}
div.main {
padding: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment