0

I have an xPage that uses an Server side Javascript beforePageLoad call to populate some fields from a remote JSON REST service.

Get this error ..... Error calling method 'openConnection()' on java class 'java.net.URL' ECL Permission Denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.net.www.protocol.https")

This works in a web browser. Are there any client settings I need to change or is there a better way to do this so I can also use it in the Notes XPiNC environment?

Code is below ... thanks in advance

var url = "https://api.companieshouse.gov.uk/company/" + CompanyNo;

var url:java.net.URL = new java.net.URL(url);
var urlconn:java.net.URLConnection = url.openConnection();
urlconn.setRequestProperty("Authorization", "Basic xxxxxxxx==");
if (urlconn.getResponseCode() < 400) {
 var reader:java.io.BufferedReader = new java.io.BufferedReader(
                                            new java.io.InputStreamReader(
                                            urlconn.getInputStream())
                                        );
 var inputLine;
    var jsonTxt = "";
    while ((inputLine = reader.readLine()) != null){
        jsonTxt += inputLine;
    }
    reader.close();

    viewScope.Response = fromJson(jsonTxt);
} else {
       /* error from server */

    viewScope.Response = "Error " + urlconn.getResponseCode() + url;
    }
  • 1
    Short answer: Don't do this in JavaScript, long answer: see below – stwissel Jun 03 '20 at 18:32
  • I agree with @stwissel. But you could possibly append to domino\jvm\lib\java.policy "grant { permission java.security.AllPermission; }; " -- does that clear the security error? – teleman Jun 03 '20 at 18:51

1 Answers1

2

There are a few things you need to change in this approach:

  • Don't use the Java classes from SSJS. Wrap your whole logic into a Java bean. It gives you a better development experience and is easier to test
  • UrlConnection is way too low level, and is a headache for https. Use the Apache HTTP client library (which is AFAIK available on the Domino server)

Some sample code should get you started along these lines:

 HttpClient httpclient = new HttpClient();
 HttpMethod httpMethod = new GetMethod( this.targetURL );
 int statusCode = httpclient.executeMethod(httpMethod);
 // TODO Check for statusCode 200
 String result = httpMethod.getResponseBodyAsString();

Also checkout this Baeldung tutorial

stwissel
  • 20,110
  • 6
  • 54
  • 101
  • thanks for the pointers - Sorry to be slow (I'am old school Notes developer thats not moved with the times!) Can you point me to a step by step demo of creating java beans so I can learn this too :) – Tim Savigar Jun 03 '20 at 19:08
  • 2
    Sure. Here you go: https://per.lausten.dk/blog/2012/02/creating-your-first-managed-bean-for-xpages.html – stwissel Jun 03 '20 at 19:17
  • I've made a start at doing the java bean - thanks for your help - the code seems to run on a local replica but not on the server - are there some permissions that need to be changed? – Tim Savigar Nov 11 '20 at 18:23
  • Had to make changes to Server security doc - added my ID (was in a group previously) and it started working :) – Tim Savigar Nov 13 '20 at 14:46
  • I've got my java bean working (at last) - BUT - the faces-config.xml appears to be overwritten (I assume by design task) - it is not being inherited from the template though .... any ideas? – Tim Savigar Nov 25 '20 at 13:07