如何使用Abdera atom客户端发送内容和附件
How to use Abdera atom client to send content and attachment
我们正在使用 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 来展示这一点,但也包括了两个单元测试,其中可以在不需要连接的情况下测试空指针。这可以在 GitHub
上找到
它可以与 Abdera 一起使用,为了将来参考这里有一个示例 post 一个包含文本内容和一个(或多个)附件的条目。您需要使用底层 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 需要它。
我们正在使用 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 来展示这一点,但也包括了两个单元测试,其中可以在不需要连接的情况下测试空指针。这可以在 GitHub
上找到它可以与 Abdera 一起使用,为了将来参考这里有一个示例 post 一个包含文本内容和一个(或多个)附件的条目。您需要使用底层 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 需要它。