Search code examples
xpagesssjs

how get JSON from URL


I need to read with the function SSJS fromJson() a URL. For example the Data access API for a Notes View

http://{host}/{database}/api/data/collections/name/{name}

How can I do this ?

P.S I think (I don't know if is true) that if I use Java code (for example the class URLReader from this blogger, I lose authors/readers functionality because is my server and not the current user that execute the reading of the stream?

I'll explain why I'm trying to understand this...

I need to use this plugin JQuery Jquery Data Tables in my app. I need a complete Server-side processing because I have over 10.000 documents for any view. This jQueryPlugin send a parameters to a specif URL (I think my XAgent) so that I think to create a XAgent that read this parameter and parsing a JSON API Data for the output. This because I need a fasted response.

The solution of Oliver Busse it very slow because load all entries of my view in a JSON (I have many entries) and I wait 30/40 seconds for this operation


Solution

  • I gather from the PS that you're specifically looking to fetch JSON on the server from itself, while retaining user authentication information. Sven's post there does a bit of that, but I think that the most reliable way would be to grab the Authorization and Cookie headers from the request and then pass them along in your URL request. This answer has a good basis for doing this with Java. You could expand that to do something like this (which, granted, I haven't tested, but it's a starting point):

        HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String authorization = req.getHeader("Authorization");
        String cookie = req.getHeader("Cookie");
    
        URL myURL = new URL("http://foo.com");
        HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
        if(StringUtil.isNotEmpty(authorization)) {
            myURLConnection.setRequestProperty("Authorization", authorization);
        }
        if(StringUtil.isNotEmpty(cookie)) {
            myURLConnection.setRequestProperty("Cookie", cookie);
        }
        myURLConnection.setRequestMethod("GET");
        myURLConnection.setDoInput(true);
        myURLConnection.setDoOutput(true);
        myURLConnection.connect();
        InputStream is = null;
        try {
            is = myURLConnection.getInputStream();
            String result = StreamUtil.readString(is);
        } finally {
            StreamUtil.close(is);
            myURLConnection.disconnect();
        }
    

    Ideally, you would also fetch the server host name, protocol, and port from the request.

    Eric's comment is also wise: if this is something you can do with the normal classes, that's going to be more flexible and less problem-prone, due to how fiddly server-self HTTP calls can be.