Consume REST service in server-based agent

佐手、 提交于 2021-02-08 05:45:22

问题


We're asked to build a Domino server-based database that exchanges data with a remote non-Domino server. The remote server can be connected to by using webservices.

Creating a RESTful service in Domino seems simple, using R8.5.3: there are some very interesting articles on Domino Data Service on the Internet. Studying this page will certainly help me to create one end of the connection.

Now for the consuming part in the agent. We did this once before, some time ago, and then we used plain HTTP URLs and a simple GetDocumentByURL. It isn't perfect, but it works.

But is that the best way to consume a web service in a Domino agent? It's a Linux environment so I cannot use MS-objects or so. Is there some standard library that I can call, preferably in LotusScript? Or is there a way to use some XPages control in an agent?

Thanks for your suggestions!


回答1:


[EDIT] The example from breakingpar

The java code to be placed in a Java Library called GetHTML:

import java.io.*;
import java.net.*;

public class GetHTML {

   public String getHTML(String urlToRead) {
      URL url; // The URL to read
      HttpURLConnection conn; // The actual connection to the web page
      BufferedReader rd; // Used to read results from the web page
      String line; // An individual line of the web page HTML
      String result = ""; // A long string containing all the HTML
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
            result += line;
         }
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
}

And to use it in Lotusscript:

Uselsx "*javacon"
Use "GetHTML" ' Java library
Const myURL = "http://www.breakingpar.com"
Dim js As JAVASESSION
Dim getHTMLClass As JAVACLASS
Dim getHTMLObject As JavaObject
Dim html As String

Set js = New JAVASESSION
Set getHTMLClass = js.GetClass("GetHTML")
Set getHTMLObject = getHTMLClass.CreateObject
html = getHTMLObject.getHTML(myURL)

I used this to populate a country drop down in Lotus via this service: http://ws.geonames.org/countryInfo?

You can use a Java Agent to consume the rest service: Is there an alternative to using the LotusScript GetDocumentByURL method

The code below is copied from the technote. If the request is part of a bigger script jou can wrap the HTTP request in LS2J

import lotus.domino.*;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;

public class JavaAgent extends AgentBase {
    public void NotesMain() {
        try {
            Session session = getSession();
            AgentContext agentContext = session.getAgentContext();

            Database db = agentContext.getCurrentDatabase();
            URL ibmURL = new URL(" http://finance.yahoo.com/q?s=IBM&d=t");
            BufferedReader bin = new BufferedReader(new InputStreamReader(ibmURL.openStream()));
            String line;
            StringBuffer sb = new StringBuffer();

            while ((line = bin.readLine()) != null) {
                sb.append(line);
            }
            String ibmString = sb.toString();

            Document newNotesDoc = db.createDocument();
            newNotesDoc.replaceItemValue("Form", "IBMForm");
            newNotesDoc.replaceItemValue("WebPageUS", ibmString);
            newNotesDoc.computeWithForm(true, false);
            newNotesDoc.save(true, true);

            String ibms = newNotesDoc.getItemValueString("QuoteUS");
            System.out.println("IBM Raw String is " + ibms);
            newNotesDoc.recycle();

            NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
            BigDecimal d = new BigDecimal(ibms);
            double ibmd = d.doubleValue();
            String ibm = n.format(ibmd);
            System.out.println("IBM Currency is " + ibm);

            SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm:ss a");
            Date currentTime_1 = new Date();
            String dateString = formatter.format(currentTime_1);
            System.out.println("Formatted date is " + dateString);
            String displayText = "IBM stock price as of " + dateString + " NYSE US " + ibm;
            System.out.println("Display text is " + displayText);
            db.recycle();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


来源:https://stackoverflow.com/questions/20703996/consume-rest-service-in-server-based-agent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!