4

Can anyone point me to a article, tutorial, or walk-through of getting started with consuming REST services in XPages? I have seen a few that use the Domino Data Service or a Domino REST service, but I would like to see one consuming external REST services such as PayPal's.

Please don't direct me to Social Business Toolkit, I have looked at it and even downloaded it but don't feel I should have to install J2EE and Eclipse to see a demo of 12 lines of JavaScript.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
John
  • 157
  • 4
  • 15
  • 6
    At the risk of self-promotion, I'd recommend getting a copy of the XPages Extension Library book; chapter 11 is dedicated entirely to REST services. Several of the examples reference the demo database to which Lothar refers, but it's a fairly thorough walkthrough of the many options available. One other tangential point: if you're running Domino, you've installed J2EE; if you're running Designer, you've installed Eclipse... just couldn't resist pointing that out. – Tim Tripcony Jan 17 '14 at 23:07
  • Yes, understood, but my understanding of the SBT was that you needed to setup that environment again in order to use it. I downloaded it and tried to look at a sample db, but I got about 40 errors in Designer which I could not resolve. No idea where to go from there and my point was, should I have to? I have the book, but I didn't see any help about using external REST services. I will look again. – John Jan 19 '14 at 09:15
  • Admittedly, the downside of the thoroughness of the book is that if you're looking for one specific topic, you have to find it within everything else that is covered. If you already have a specific service in mind that you want to consume, the more details about it that you can provide, the more easily we can provide a laser-focused answer. Let's start with this: is it XML-based, JSON-based, or both? And, if both, which would you prefer to use? – Tim Tripcony Jan 19 '14 at 16:59
  • I am attempting to use the PayPal REST API in Domino. I was never able to get any of their Java samples to compile in Domino, so I wanted to try their REST API. The REST Playground here https://devtools-paypal.com/hateoas/index.html?interactive=ON&env=sandbox makes everything look very simple, but I am not sure how to connect it up in Domino. – John Jan 19 '14 at 19:00
  • Hi Tim, I am trying to use the one sample that I think might be relevant in your book, Consuming Service Data from External Applications - OpenSocial Gadgets. I tried copying the code there into an XPage, but it does not compile for me, I get the error "The tag at the root of the page cannot be from an unknown namespace." Am I missing something? – John Jan 23 '14 at 13:08

2 Answers2

8

I know this is a bit after the fact, but for mere consumption of a RESTful endpoint for use in XPages, I blogged recently about doing so on the server-side. My implementation makes use of a Java Class used to generate the output via URLConnection and, ultimately, a StringBuffer to read in the contents, then parse it into a JsonObject for return. I did two folow ups on the topic and you can find them accordingly:

Series page / TOC

  1. REST consumption, server-side with Java
  2. REST consumption with authentication
  3. Generating Custom JSON data from Java

My examples make use of the Google GSON library, but as pointed out by Paul T. Calhoun, there is the com.ibm.commons.util.io.json package which has come with Domino for a while and probably the better option for Domino devs (no external dependencies and no potential java.policy edit).

The basic structure of the method is:

/* 
 * @param String of the url
 * @return JsonObject containing the data from the REST response.
 * @throws IOException
 * @throws MalformedURLException
 * @throws ParseException 
 */
public static JsonObject GetMyRestData( String myUrlStr ) throws IOException, MalformedURLException {
    JsonObject myRestData = new JsonObject();
    try{

        URL myUrl = new URL(myUrlStr);
        URLConnection urlCon = myUrl.openConnection();
        urlCon.setConnectTimeout(5000);
        InputStream is = urlCon.getInputStream();
        InputStreamReader isR = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isR);
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while( (line = reader.readLine()) != null ){
            buffer.append(line);
        }
        reader.close();
        JsonParser parser = new JsonParser();
        myRestData = (JsonObject) parser.parse(buffer.toString());

        return myRestData;

    }catch( MalformedURLException e ){
        e.printStackTrace();
        myRestData.addProperty("error", e.toString());
        return myRestData;
    }catch( IOException e ){
        e.printStackTrace();
        myRestData.addProperty("error", e.toString());
        return myRestData;
    }
}
Eric McCormick
  • 2,716
  • 2
  • 19
  • 37
  • Since my method creates a com.google.gson.JsonObject, if you don't want to enforce the class type in SSJS, just change the method to output a String, then perform a toJson/JSON.parse of the String in the SSJS. – Eric McCormick Nov 04 '14 at 20:05
3

There are numerous articles etc. out there. I mostly stick with the Xpages ExtLib demo database, available from openNTF. In case you don't have it: just d/l the extlib installation, the demo db is part of the package. Talking about SBT: depending on your version of Domino Designer you might already it the toolkit available (e.g. Version 9). There's also another demo db available with SBT examples. All in all I wonder what you mean with installing J2EE / Eclipse

Lothar Mueller
  • 2,528
  • 1
  • 16
  • 29
  • I am running Domino 9, downloaded the entire 150 MB SBT sdk in order to get the demo db, and I cannot get it to compile in Domino. Is there another demo db? – John Jan 19 '14 at 09:17
  • OK, I have downloaded and looked at the demo db from the ExtLib. It looks to me like it is about using the REST control to publish a REST service. This is not what I am looking for, I would like to consume a public REST service. Is this not available or am I looking n the wrong place? – John Jan 22 '14 at 07:16
  • I don't have the demoes here right now, but from what I remember both are containing examples for consuming REST data. Other than that I remember reading up on using REST from within Domino / Xpages. I think it must have been in some kind of Wiki available from IBM's developerWorks site – Lothar Mueller Jan 22 '14 at 07:55
  • 2
    OK, I have looked through the entire demo db, and cannot find any examples of consuming a non-Domino REST service. See this discussion for more: http://www-10.lotus.com/ldd/xpagesforum.nsf/xpTopicThread.xsp?documentId=8BCB11252D985E5885257AAF0016BC5A – John Jan 23 '14 at 12:39
  • Interesting discussion, thanks for adding the url. Have you tried Panu's workaround (2nd to last answer)? Panu also brought up another idea in mentioning the web services consumer. Apart from that it appears as if it wasn't working out of the box, I'm afraid – Lothar Mueller Jan 24 '14 at 15:38
  • I really had already given up using web services in Domino, it seems to be a hack at best. I was hoping to use REST via HATEOS as it seemed much simpler and less reliant on the framework. I'm now considering abandoning Domino for any of this kind of work as I can't find any examples so I think I must be barking up the wrong tree altogether. – John Jan 25 '14 at 20:11