我们正在使用Abdera与IBM Connections API进行交互,但是我们的问题主要与Abdera本身有关。

我认为Abdera中存在一个错误,该错误不允许您在单个请求中发送包含内容和附件的条目。作为workaround,您可能可以发送两个单独的请求,以首先创建包含内容的内容,然后再添加附件进行更新。遗憾的是,Connections API要求您在单个请求中拥有所有数据,否则旧数据将不被保留。

以下代码显示了创建的Abdera条目:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("google-trends.tiff");

final Abdera abdera = new Abdera();
Entry entry = abdera.getFactory().newEntry();
entry.setTitle("THIS IS THE TITLE");
entry.setContentAsHtml("<p>CONTENT AS HTML</p>");
entry.setPublished(new Date());

Category category = abdera.getFactory().newCategory();
category.setLabel("Entry");
category.setScheme("http://www.ibm.com/xmlns/prod/sn/type");
category.setTerm("entry");
entry.addCategory(category);

RequestEntity request =
    new MultipartRelatedRequestEntity(entry, is, "image/jpg",
        "asdfasdfasdf");

创建MultipartRelatedRequestEntity时,将抛出NullPointer:
java.lang.NullPointerException
    at
org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)

发生这种情况是因为它希望包含内容“src”元素,但是在深入研究Abdera的源代码之后,根据规范,似乎这不是必需的元素。这看起来像是Abdera代码中的错误,不是吗?
/**
 * <p>
 * RFC4287: atom:content MAY have a "src" attribute, whose value MUST be an IRI reference. If the "src" attribute is
 * present, atom:content MUST be empty. Atom Processors MAY use the IRI to retrieve the content and MAY choose to
 * ignore remote content or to present it in a different manner than local content.
 * </p>
 * <p>
 * If the "src" attribute is present, the "type" attribute SHOULD be provided and MUST be a MIME media type, rather
 * than "text", "html", or "xhtml".
 * </p>
 *
 * @param src The IRI to use as the src attribute value for the content
 * @throws IRISyntaxException if the src value is malformed
 */

我已经将参考应用程序连接放置到IBM Greenhouse Connections中进行显示,但是还包括两个单元测试,可以在其中测试空指针而无需使用Connections。可以在GitHub上找到

最佳答案

可以使其与Abdera一起使用,以供将来参考,此处是一个示例,该示例发布了一个包含文本内容和一个(或多个)附件的条目。您需要使用基础HttpClient框架的Part:

  final Entry entry = this.createActivityEntry();
  final RequestOptions options = this.client.getDefaultRequestOptions();
  options.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");

  StringPart entryPart = new StringPart("entry", entry.toString());
  entryPart.setContentType("application/atom+xml");

  FilePart filePart = new FilePart("file", new File(resource.getFile()));

  RequestEntity request = new MultipartRequestEntity(new Part[] { entryPart, filePart}, this.client.getHttpClientParams());
  ClientResponse response = client.post(this.url + this.activityId, request, options);

这使我们可以在一个请求中创建包含内容和附件的IBM Connections Activity Entry,因为API对此是必需的。

09-29 22:36