Hi All!
You may came to a point when you needed an to fetch the content from a web hosted location.
The Scripting environments a load(...) that can handle a http(s): to download the externally hosted script files (not local to a PC)
From the design the load method triggers JS evaluation of the external URL content => meaningful only for a script file containing valid JS syntax.
On top if your server is behind authorization (Basic, Bearer, SSO) things will also not help.
Note: The Basic authorization seems to be working. but the drawback is that your URL will contant the user and passwoerd on the URL request. ex https://user:password@host/path
The answer is to use your custom function that utilizing java.net.URL and URLConnection a similar topics have been discussed in Downloading remote files into a project (providing a medini utility function. Does not support custom authorizations or other modifications of the URL setup )
A basic example to download a medini Freshdesk forum image:
function testFetch(){
var url=java.net.URL("https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/1018976097/logo/IKV_Cube_Only_White_(RGB).png");
var conn = null;
var res = null;
try {
conn = url.openConnection();
} catch (e) {
console.log(e["javaException"]);
return res;
}
if (!(conn instanceof java.net.HttpURLConnection))
return res;
var targetFile = new java.io.File("fetchedLogo.png");
var os = java.nio.file.Files.newOutputStream(targetFile.toPath());
var res = null;
try {
conn.connect();
console.log("URL content type {0}, response Code {1}, response Message {2}", conn.getContentType(), conn.getResponseCode(), conn.getResponseMessage());
res = conn.getInputStream();
} catch (efetch) {
console.log(efetch["javaException"]);
throw efetch["javaException"];
}
if (res instanceof java.io.InputStream)
os.write(res.readAllBytes())
else
console.log("No InputStream returned from URL connection");
os.close()
}
There if you try this snippet you might encounter a java exception raised during attempt to connect resp getting the Content form the URL.
The Error will read :
Wrapped java.lang.IllegalAccessException: class org.mozilla.javascript.MemberBox cannot access class sun.net.www.protocol.https.HttpsURLConnectionImpl (in module java.base) because
module java.base does not export sun.net.www.protocol.https to unnamed module
@4c643c8
Similar issue is discussed in many places, e.g. BIRT JS with a final solution GitHub dicsussion
Basically the solution is to update mediniAnalyze.ini file with :
--add-opens=java.base/sun.net.www.protocol.https=ALL-UNNAMED
--add-opens=java.base/sun.net.www.http=ALL-UNNAMED
Attaching an example js skeleton that creates a custom URL connection with authorization.
Marian