Skip to main content

Nashorn - Network Access via URLs

2 replies [Last post]
willsf1862
Offline
Joined: 2013-10-18
Points: 0

Is it currently possible to access the network using java.net.URLs with Nashorn?

Very standard code (e.g. new URL(url).getContent() that works fine in the normal JVM does not seem to have a content handler, gives a mime-type missing error, and otherwise does not work.

Existing application code libraries which currently work fine in the JVM also seem to fail in the same way within Nashorn.

However, loading files via getClassLoader().getResource() does work so the problem seems to be when trying to access the network to any host, including localhost on a non-privileged port.

I've tried adjusting the security policy to be very lenient (permit all) both in java.policy and programatically via Policy.setPolicy() with a custom policy, but no apparent change.

Is it supposed to work right now? I have an critical path project which depends on this, so any insider help would most appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
willsf1862
Offline
Joined: 2013-10-18
Points: 0

MORE INFO:

[will@localhost ext]$ java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b108)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b50, mixed mode)

I'm using a duck typing strategy with the angular $http object and need a server-side implementation. One version of the url code looks simply like:

$http.success = function(callback) {
console.log("URL: "+this.url);
var url = new java.net.URL(this.url);
var conn = url.openConnection();
console.log("CONTENT: "+conn.getContent());
callback.call(this,conn);
return this;
};

The URL used looks like:

Creating loader
Loading execution: 27701236-3540-4c24-b37e-8665d57058d7
URL: http://localhost:9040/api/v1/model/execution/27701236-3540-4c24-b37e-866...

The root cause for this code is:

aused by: java.net.UnknownServiceException: no content-type
at java.net.URLConnection.getContentHandler(URLConnection.java:1241)
at java.net.URLConnection.getContent(URLConnection.java:740)
at jdk.nashorn.internal.scripts.Script$\^eval\_._L17(:24)
at jdk.nashorn.internal.scripts.Script$\^eval\_._L1$_L64(:73)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(:6)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:527)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:204)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:367)

Adding any of...

conn.setRequestProperty("Content-Type","text/plain");
conn.setRequestProperty("Content-type","text/plain");
conn.setRequestProperty("content-type","text/plain");

...does not help.

Reading the stream does not return any content.

willsf1862
Offline
Joined: 2013-10-18
Points: 0

WORKAROUND:

The apache commons library works fine, so it's only an issue with java.net.URL defaults. Just wrap the code in a nashorn visible package and solve any separate authentication issue.

$http.success = function(callback) {
console.log("URL: "+this.url);
var content = com.example.app.util.NetworkUtils.getContent(this.url);
console.log("CONTENT: "+content);
callback.call(this,JSON.parse(content));
return this;
};

...
log.info("PROXY: "+url);
HttpGet get = new HttpGet(url);
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
BufferedReader in = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
StringBuilder content = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
...

...
10/18/2013 08:22:17 INFO [040-exec-6]: HttpUtils( 59): PROXY: http://localhost:9040/api/v1/model/execution/27701236-3540-4c24-b37e-866...
10/18/2013 08:22:17 INFO [040-exec-7]: TokenAuthenticationFilter( 82): Token authenticated: 559612ad386cda0dabebba61e4e3c845 system (172.16.10.46)
10/18/2013 08:22:17 INFO [040-exec-4]: TokenAuthenticationFilter( 82): Token authenticated: 559612ad386cda0dabebba61e4e3c845 system (172.16.10.46)
10/18/2013 08:22:17 INFO [040-exec-6]: HttpUtils( 73): RESPONSE: 880 (200,OK)
CONTENT: {
"id" : "27701236-3540-4c24-b37e-8665d57058d7",
...

...feel free to either answer this one more fully or close.