如何使用Abdera Atom客户端发送内容和附件 [英] How to use Abdera atom client to send content and attachment

查看:115
本文介绍了如何使用Abdera Atom客户端发送内容和附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are using Abdera to interact with the IBM Connections API, but our problem is mostly related to Abdera itself.

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

I think there is a bug in Abdera that does not allow you send an Entry that contains content and attachments in a single request. As a workaround you would probably be able to send two separate requests to create first with content and update afterwards with attachment. Sadly the Connections API required you to have all data in a single request or your old data is not preserved.

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

The following code shows an Abdera Entry that is created:

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:

When the MultipartRelatedRequestEntity is created a NullPointer is thrown:

java.lang.NullPointerException
    at
org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)

发生这种情况是因为它期望包含内容"src"元素,但是在深入研究Abdera的源代码之后,根据规范,似乎这不是必需的元素.这看起来像是Abdera代码中的错误,不是吗?

This happens because it is expecting a content "src" element, but after digging into the source code of Abdera it seems like this is not a required element according to the specification. This looks like a bug in the Abdera code, no?

/**
 * <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

I have put a reference application connection to IBM Greenhouse Connections to show this, but have also included two unit tests in which the nullpointer can be tested without needing Connections. This can be found on GitHub

推荐答案

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

It is possible to get it working with Abdera, for future reference here is an example to post an entry that contains text content and also one (or more) attachmment(s). You need to use Part of the underlying HttpClient framework:

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

这使我们能够创建

This allowed us to create an IBM Connections Activity Entry with content and attachments in one request, since this is needed by the API.

这篇关于如何使用Abdera Atom客户端发送内容和附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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