在基于服务器的代理中使用REST服务 [英] Consume REST service in server-based agent

查看:142
本文介绍了在基于服务器的代理中使用REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

使用R8.5.3在Domino中创建RESTful服务似乎很简单:Internet上有一些关于Domino Data Service的非常有趣的文章.研究此页面当然可以帮我建立连接的一端.

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.

现在是代理中的消耗部分.我们之前,一段时间之前都这样做过,然后我们使用普通的HTTP URL和简单的GetDocumentByURL.它不是完美的,但可以.

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.

但这是在Domino代理中使用Web服务的最佳方法吗?这是一个Linux环境,所以我不能使用MS对象.是否有一些我可以调用的标准库,最好是在LotusScript中?还是有办法在代理中使用某些XPages控件?

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?

感谢您的建议!

推荐答案

来自破门

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

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;
   }
}

并在Lotusscript中使用它:

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)

我使用此服务通过以下服务在Lotus中填充了一个国家/地区: http://ws.geonames.org/countryInfo ?

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

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

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

以下代码是从技术说明中复制的.如果请求是较大脚本的一部分,那么jou可以将HTTP请求包装在

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();
        }
    }
}

这篇关于在基于服务器的代理中使用REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆