如何使用Abdera atom客户端发送内容和附件

时间:2015-07-24 12:23:40

标签: java atom-feed ibm-connections atompub apache-abdera

我们正在使用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的情况下测试nullpointer。这可以在GitHub

上找到

1 个答案:

答案 0 :(得分:0)

有可能让它与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需要这样做。

相关问题