任何人都可以举例说明如何使用xPages Social Enabler创建新的IBM Connections Activity吗?

时间:2012-11-20 10:25:39

标签: xpages

有人能举例说明如何使用xPages Social Enabler创建新的IBM Connections Activity吗?我无法在文档中找到任何有用的信息,所以我改编了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) 
} 

但上面的代码不会创建任何东西,但会在Domino控制台上引发错误。这是由svc.post()命令返回的:

[31726:00075-3041917840] 11/19/2012 01:03:59 PM  HTTP JVM: Client service request to: http://vhost1279.site1.compute.ihost.com:81/activities/service/atom2/activities did not return OK status. Status returned: 415, reason: Unsupported Media Type, expected:information, please consult error-l 

[31726:00075-3041917840] 11/19/2012 01:03:59 PM  HTTP JVM: g-0.xml located in /local/opt/ibm/lotus/notesdata/domino/workspace/logs 

[31726:00075-3041917840] 11/19/2012 01:03:59 PM  HTTP JVM: com.ibm.xsp.extlib.sbt.services.client.ClientServicesException: HTTP Status 415, Unsupported Media Type. HTTP error response code received in response to request to url: http://vhost1279.site1.comties/service/atom2/activities 

任何人都可以给我一个提示如何正确使用它或指向我一些有用的文档吗?

4 个答案:

答案 0 :(得分:1)

不要使用StringBuilder来创建XML。至少使用SAX或更好Apache Abdera来创建XML(Tutorial here)。这可以确保您的XML有效,并且在Abdera的情况下也是有效的ATOM。

使用这种方法非常重要,因为您获得了一个Node对象,它会自动触发所需的内容类型。

然后检查如何create Connections documentation wiki中的this article活动(是 - 令人困惑)。在CURL中,您找到了检索活动的代码 - 我实际上建议您使用here来获取有效格式作为示例。一些CURL网址为status update。最接近完整示例的是Luis的CURL documentation演示。

要探索连接,我使用以下批处理文件:

set server=[server] 
set HOME=c:\work
curl %server%%1 –-netrc -G --basic -k -v -L -o %2 %3 %4 %5 %6 %7

使用.netrc文件(请参阅{{3}})

 machine [server] login [user] password [password]

这是活动所需的XML格式:

<?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>

然后发布:

post activities/service/atom2/activities newactivity.xml activityresult.xml

打开activityresult.xml并找到ocate app:集合元素的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>

和这个命令:

post [the-url-you-found-above] newaction.xml actionresult.xml

一旦CURL版本有效,您可以尝试使用Abdera代码。

答案 1 :(得分:0)

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

https://vhost1279.site1.compute.ihost.com/activities/service/atom2/activities

标题:Content-Type application / 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> 

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

答案 2 :(得分:0)

解决!!!我看来源,问题很明显。这是一个错误或至少是误解,但可以很容易地解决。根据文档和我的测试证明这个Connections在请求中需要以下标题:Content-Type = application / atom + xml ....但是在Social Enabler的源代码中我找到了这两个相关的方法:

protected void prepareRequest(HttpClient httpClient,HttpRequestBase httpRequestBase,Options options)抛出ClientServicesException {             // TODO:添加对gzip内容的支持             //httpClient.addRequestHeader("Accept-Encoding“,”gzip“);

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

正如您所看到的,对于任何情况都没有这样的标题(application / atom + xml)。但是,如果您将XML内容作为字符串提供,则代码使用“findRequestTextContentType”方法返回默认内容类型,即对于我们的情况不正确的“text / plain”。它是硬编码的,因此无法如何设置默认编码。但是,至少,'findRequestTextContentType'是受类型保护的,因此可以覆盖它。所以我创建了自己的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";
        }

}

答案 3 :(得分:0)

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

创建文档

com.ibm.commons.xml.DOMUtil

结帐

com.ibm.commons.xml.DOMUtil.createDocument(String, String)

e.g。

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

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

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