在基于服务器的代理中使用 REST 服务

D.B*_*ger 2 rest lotus-domino

我们被要求构建一个基于 Domino 服务器的数据库,用于与远程非 Domino 服务器交换数据。可以使用 Web 服务连接到远程服务器。

使用 R8.5.3 在 Domino 中创建 RESTful 服务似乎很简单:Internet 上有一些关于 Domino 数据服务的非常有趣的文章。研究这一页肯定会帮助我建立连接的一端。

现在介绍代理中的消耗部分。我们之前做过一次,不久前,然后我们使用纯 HTTP URL 和一个简单的 GetDocumentByURL。它并不完美,但它有效。

但这是在 Domino 代理中使用 Web 服务的最佳方式吗?这是一个 Linux 环境,所以我不能使用 MS-objects 等。是否有一些我可以调用的标准库,最好是在 LotusScript 中?或者有没有办法在代理中使用某些 XPage 控件?

感谢您的建议!

Jas*_*tra 5

[编辑] 来自BreakingPar的示例

要放置在名为 GetHTML 的 Java 库中的 java 代码:

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;
   }
}
Run Code Online (Sandbox Code Playgroud)

并在 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)
Run Code Online (Sandbox Code Playgroud)

我用它通过此服务填充 Lotus 中的国家/地区下拉菜单:http://ws.geonames.org/countryInfo

您可以使用 Java 代理来使用其余服务: 是否有使用 LotusScript GetDocumentByURL 方法的替代方法

下面的代码是从技术说明中复制的。如果请求是更大脚本的一部分,则可以将 HTTP 请求包装在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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)