谁能给我举个例子,说明如何使用xPages Social Enabler创建新的IBM连接活动?

谁能给我举个例子,说明如何使用xPages Social Enabler创建新的IBM连接活动?,xpages,Xpages,谁能给我举个例子,说明如何使用xPages Social Enabler创建新的IBM连接活动?我在文档中找不到任何有用的信息,因此我改编了Niklas Heidloff关于如何在Connections中创建新书签的示例。我有以下创建新活动的代码: try { var svc = new sbt.ConnectionsService("/activities/service/atom2/activities"); var sb = new java.lang.StringBui

谁能给我举个例子,说明如何使用xPages Social Enabler创建新的IBM连接活动?我在文档中找不到任何有用的信息,因此我改编了Niklas Heidloff关于如何在Connections中创建新书签的示例。我有以下创建新活动的代码:

try { 
   var svc = new sbt.ConnectionsService("/activities/service/atom2/activities"); 

   var sb = new java.lang.StringBuilder(); 
   sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
   sb.append("<entry xmlns:snx=\"http://www.ibm.com/xmlns/prod/sn\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:thr=\"http://purl.org/syndication/thread/1.0\" xmlns=\"http://www.w3.org/2005/Atom\">"); 
   sb.append("<title type=\"text\">"); 
   sb.append("test activity from xpages"); 
   sb.append("</title>"); 
   sb.append("<content type=\"html\">"); 
   sb.append("</content>"); 
   sb.append("</entry>");                 

   var msg = svc.post(null, sb.toString(), "xml"); 
} catch(e) { 
    print(e) 
} 
<?xml version="1.0" encoding="utf-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom"> 
   <category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="activity" label="Activity"/> 
   <title type="text">Posted activity</title> 
   <content type="html"> 
  This is an activity that has been automatically uploaded from the cURL command line
   </content> 
</entry>

谁能给我一个如何正确使用它的提示,或者给我一些有用的文档吗?

不要使用StringBuilder来创建XML。至少使用或更好地创建XML()。这确保了XML是有效的,在Abdera的情况下,ATOM也是有效的

使用这种方法很重要,因为您得到了一个
节点
对象,它会自动触发所需的内容类型

然后检查如何在中创建活动(是-混淆)。在本文中,您可以找到检索活动的代码——实际上,我建议使用它来获取有效的格式作为示例。有些URL是。最接近完整示例的是Luis的演示

要浏览连接,我使用以下批处理文件:

set server=[server] 
set HOME=c:\work
curl %server%%1 –-netrc -G --basic -k -v -L -o %2 %3 %4 %5 %6 %7
使用.netrc文件(请参阅)

这是活动所需的XML格式:

try { 
   var svc = new sbt.ConnectionsService("/activities/service/atom2/activities"); 

   var sb = new java.lang.StringBuilder(); 
   sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
   sb.append("<entry xmlns:snx=\"http://www.ibm.com/xmlns/prod/sn\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:thr=\"http://purl.org/syndication/thread/1.0\" xmlns=\"http://www.w3.org/2005/Atom\">"); 
   sb.append("<title type=\"text\">"); 
   sb.append("test activity from xpages"); 
   sb.append("</title>"); 
   sb.append("<content type=\"html\">"); 
   sb.append("</content>"); 
   sb.append("</entry>");                 

   var msg = svc.post(null, sb.toString(), "xml"); 
} catch(e) { 
    print(e) 
} 
<?xml version="1.0" encoding="utf-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom"> 
   <category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="activity" label="Activity"/> 
   <title type="text">Posted activity</title> 
   <content type="html"> 
  This is an activity that has been automatically uploaded from the cURL command line
   </content> 
</entry>
打开activityresult.xml并找到app:collection元素的href属性-您需要它来添加操作。使用以下XML:

<?xml version="1.0" encoding="utf-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:snx="http://www.ibm.com/xmlns/prod/sn"> 
   <category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="todo"/> 
   <category term="Connection4.0"/> 
   <category term="Test"/> 
   <title type="text">Some things that need to be done</title> 
   <content type="html"> 
This is an &lt;b&gt;action&lt;/b&gt; in an activity that has been automatically uploaded from the cURL command line.
   </content> 
   <snx:assignedto>noreply@ibm.com</snx:assignedto> 
</entry>

一旦CURL版本生效,您就可以使用Abdera代码进行尝试。

以下是Firefox中REST客户端的工作示例:

标题:内容类型应用程序/atom+xml

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:snx="http://www.ibm.com/xmlns/prod/sn" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="activity" label="Activity" />
<content type="html"/>
<title type="text">
test
</title>
</entry> 

测试

上面代码的问题是您将字符串传递给post方法。但是,这并没有设置正确的内容类型。请使用Stephan建议的API使用XML创建org.w3c.dom.Node,并将其传入。这将自动在标题中设置正确的内容类型。

已解决!!!我看了看来源,问题很明显。这是一个bug,或者至少是一个误解,但很容易解决。根据文档和我的测试证明,这种连接在请求中需要以下标题:Content Type=application/atom+xml。。。。但在Social Enabler的源代码中,我发现了以下两种相关方法:

受保护的void prepareRequest(HttpClient HttpClient、HttpRequestBase HttpRequestBase、选项)引发ClientServicesException{ //TODO:添加对gzip内容的支持 //addRequestHeader(“接受编码”、“gzip”)

正如您所看到的,没有这样的头(application/atom+xml)对于任何情况。但如果您提供XML内容作为字符串,代码将使用“findRequestTextContentType”方法返回默认内容类型,即“text/plain”,这对于我们的情况都是不正确的。它是硬编码的,因此无法设置默认编码。但至少,“findRequestTextContentType”是类型保护的,因此它是c无法重写。因此,我创建了自己的ConnectionsService类,该类扩展了前一个类,并重写findRequestTextContentType方法,以为我的案例返回正确的内容类型。这很好,解决了问题

import sbt.ConnectionsService;

public class ConnectionsServiceCustom extends ConnectionsService {

    public ConnectionsServiceTcl(String serviceUrl) {
        super(serviceUrl);
        // TODO Auto-generated constructor stub
    }

     @Override
     protected String findRequestTextContentType(Options options) {
            return "application/atom+xml";
        }

}

正如Niklas和Stephen指出的,您需要使用Dom对象(节点、文档等)…如果在创建此类对象时出现错误,则很可能是因为文档/节点的内容格式不正确或不正确。。 有一个内置的XPages util类,允许您从字符串创建文档

com.ibm.commons.xml.DOMUtil

退房

com.ibm.commons.xml.DOMUtil.createDocument(String, String)
e、 g

第一个参数是XML文档的内容,第二个参数是格式


这个类提供了几种解析和构造DOM文档的实用方法。

我查看了Luis Benites的代码,他使用client.addCredentials(“”,null,null,new UsernamePasswordCredentials(username,password));…必须知道用户密码才能对连接进行身份验证?但我们不知道存储在Domino person文档中的用户密码。我们对Domino和Connections server使用SSO。然后您需要使用LTPA令牌。Abdera在后台使用Apache HTTP客户端,您可以在其中设置cookie。您需要从浏览器中读取LTPATokener sessionDid手动发布活动工作的步骤1?将问题分开很重要:-)我尝试使用cUrl,cUrl正确身份验证,但从Connections server返回此错误:
com.ibm.openactivities.atom.exception.ParsingException:org.xml.sax.SAXParseException:pro中不允许包含内容log.com.ibm.openactivities.exception.OpenActivitiesException:com.ibm.openactivities.atom.exception.parsingeexception:org.xml.sax.SAXParseException:prolog中不允许包含内容。我将发布到此URL:…因为http重定向到此SSL版本。David,您是否按照我的建议创建一个xml节点,然后条件
内容安装nceof节点
被触发,您表现良好。虽然创建“XML字符串”看起来很容易,但它迟早会让您上瘾,所以“戒掉这个习惯”并使用SAX或DOM和节点;-)
        if(options.getHeaders()!=null) {
            addHeaders(httpClient, httpRequestBase, options);
        }
        if (options.content != null) {
            String contentType = null;
            HttpEntity entity = null;
            Object content = options.content;
            try {
                //If a subclass overrides com.ibm.xsp.extlib.services.client.Service.processRequestContent(HttpRequestBase, Object, Options)
                //the the subclass must set the content type of the request, and also set the request's entity!
                if(processRequestContent(httpClient, httpRequestBase, options)){
                    if (content instanceof IValue) {
                        JsonFactory jsFactory = new JsonJavaScriptFactory(DesignerRuntime.getJSContext());
                        entity = new StringEntity(JsonGenerator.toJson(jsFactory, content, true));
                        contentType = "application/json";
                    }
                    else if (content instanceof JsonObject) {
                        JsonFactory jsFactory = JsonJavaFactory.instanceEx;
                        entity = new StringEntity(JsonGenerator.toJson(jsFactory, content, true));
                        contentType = "application/json";
                    }
                    else if (content instanceof Node) {
                        entity = new StringEntity(DOMUtil.getXMLString((Node) content, true));
                        contentType = "application/xml";
                    }
                    else {
                        entity = new StringEntity(content.toString());
                        contentType = findRequestTextContentType(options);
                    }
                }
            } catch (Exception ex) {
                if(ex instanceof ClientServicesException) {
                    throw (ClientServicesException)ex;
                }
                throw new ClientServicesException(ex, "Error while parsing request content");
            }
            if (entity != null && (httpRequestBase instanceof HttpEntityEnclosingRequestBase)) {
                httpRequestBase.setHeader("Content-type", contentType);
                ((HttpEntityEnclosingRequestBase) httpRequestBase).setEntity(entity);
            }
        }
    }

protected String findRequestTextContentType(Options options) {
        return "text/plain";
    }
import sbt.ConnectionsService;

public class ConnectionsServiceCustom extends ConnectionsService {

    public ConnectionsServiceTcl(String serviceUrl) {
        super(serviceUrl);
        // TODO Auto-generated constructor stub
    }

     @Override
     protected String findRequestTextContentType(Options options) {
            return "application/atom+xml";
        }

}
com.ibm.commons.xml.DOMUtil.createDocument(String, String)
com.ibm.commons.xml.DOMUtil.createDocument("my xml string", null);