I'm trying to use the Mozilla/Rhino js engine to test some SOAP requests in the mand line. However, none of the normal objects for making requests (XMLHttpRequest, HttpRequest) seem to be available. Why is this? Can I import libraries?
I'm trying to use the Mozilla/Rhino js engine to test some SOAP requests in the mand line. However, none of the normal objects for making requests (XMLHttpRequest, HttpRequest) seem to be available. Why is this? Can I import libraries?
Share Improve this question asked Mar 3, 2014 at 21:33 etsaueretsauer 3672 gold badges6 silver badges12 bronze badges 2-
none of the normal objects for making requests (XMLHttpRequest, HttpRequest)
normal where ? what do you think rhino is ? JAVASCRIPT IS NOT THE DOM ! XMLHttpRequest is a DOM API, it has nothing to do with javascript,it's not part of the Javascript spec, A javascript engine doesnt have to implement the DOM, Rhino does not. – mpm Commented Mar 3, 2014 at 22:03 - 7 In fairness to the original poster, of the let's say few billion installed implementations of javascript, including every modern web browser, of which most developers would have four or five installed, and most puters will have at least one, 90% or more of them have a DOM. So it would be fair to say that the objects the OP mention are 'normally' available when you are in Javascript. It is not an unmon point of confusion, which can be gently corrected. – barry-johnson Commented Mar 3, 2014 at 22:13
3 Answers
Reset to default 4I was able to get it to work using just Rhino with the following code.
var post = new org.apache.mons.httpclient.methods.PostMethod("https://someurl/and/path/");
var client = new org.apache.mons.httpclient.HttpClient();
// ---- Authentication ---- //
var creds = new org.apache.mons.httpclient.UsernamePasswordCredentials("username", "password");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(org.apache.mons.httpclient.auth.AuthScope.ANY, creds);
// -------------------------- //
post.setRequestHeader("Content-type", "application/xml");
post.setRequestEntity(new org.apache.mons.httpclient.methods.StringRequestEntity(buildXML(), "text/plain", "ASCII" ));
var status = client.executeMethod(post);
var br = new java.io.BufferedReader(new java.io.InputStreamReader(post.getResponseBodyAsStream()));
var response = "";
var line = br.readLine();
while(line != null){
response = response + line;
line = br.readLine();
}
post.releaseConnection();
You might possibly find a library to import, you could also write your own in Java and make them available to your rhino instance, depending on how your are using it. Keep in mind Rhino is just a Javascript language engine. It doesn't have a DOM, and is not inherently 'web aware' so to speak.
However, since it sounds like you are doing this for testing/experimentation purposes, and you will probably be more productive not having to reinvent the wheel to do so, I will strongly, strongly suggest that you just download Node.js and look into the request module (for making HTTP requests) or any of the various SOAP modules.
You can do a ton more with Node.js, but you can also use it as a very simple runner for Javascript files as well. Regardless you should move away from Rhino though. It is really old and not really supported anymore, especially now that with JDK8 even the javax.script support will switch to the Nashorn engine.
UPDATE: If you really want to give it a go (and if you are prepared to monkey around with Java), you might look at this SO question and its answers. But unless you are something of a masochist, I think you'll be happier taking a different path.
I was actually able to do this using Orchestrator 5.1 with the 'Scriptable task' object to interface with the Zabbix API:
var urlObject = new URL(url);
var jsonString = JSON.stringify({ jsonrpc: '2.0', method: 'user.login', params: { user: 'username', password: 'password' }, id: 1 });
urlObject.contentType = "application/json";
result = urlObject.postContent(jsonString);
System.log(result);
var authenticationToken = JSON.parse(result).result;