Using RHINO js engine to make http requests
Asked Answered
C

3

6

I'm trying to use the Mozilla/Rhino js engine to test some SOAP requests in the command line. However, none of the normal objects for making requests (XMLHttpRequest, HttpRequest) seem to be available. Why is this? Can I import libraries?

Condolent answered 3/3, 2014 at 21:33 Comment(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.Farrish
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 computers 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 uncommon point of confusion, which can be gently corrected.Johnsiejohnson
H
4

I was able to get it to work using just Rhino with the following code.

var post = new org.apache.commons.httpclient.methods.PostMethod("https://someurl/and/path/");
var client = new org.apache.commons.httpclient.HttpClient();

// ---- Authentication ---- //
var creds = new org.apache.commons.httpclient.UsernamePasswordCredentials("username", "password");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, creds);
// -------------------------- //

post.setRequestHeader("Content-type", "application/xml");
post.setRequestEntity(new org.apache.commons.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();
Hermaherman answered 27/5, 2015 at 15:59 Comment(1)
this is not a port js to java while code in js. but thanksRh
J
0

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.

Johnsiejohnson answered 3/3, 2014 at 21:50 Comment(5)
I was actually looking for this because I am trying to make a SOAP call from VMWARE's vCenter Orchestrator and the source uses a WSDL format not supported by its SOAP plugin. VCO provides a Rhino JS engine as it's scripting engine so I thought I could get around it that way, but it appears not.Condolent
I didn't fully read your comment the first time. Hmm. From looking at their plugins the unfortunately only provide 'higher level' ones. You should contact their support and see if they would just give an HTTP one instead of providing dedicated SOAP and REST plugins only.Johnsiejohnson
BTW, maybe in addition to traditional support, you could see if their 'labs' guys would respond with anything. This seems to be where they throw unsupported widgets - maybe someone could do you a favor. VMWare LabsJohnsiejohnson
So is there no way using pure javascript (no DOM) to make an http request?Condolent
That is correct. As a bit of a simplification the global objects Mozilla lists here are all you'll have access to (I say simplification, because I don't believe Rhino supports the 'lab' objects).Johnsiejohnson
N
0

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;
Nice answered 23/12, 2014 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.