加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:08:15 所属栏目:Java 来源:网络整理
导读:我们使用 Abdera与IBM Connections API进行交互,但我们的问题主要与Abdera本身有关. 我认为Abdera中存在一个错误,它不允许您在单个请求中发送包含内容和附件的条目.作为workaround,您可能会发送两个单独的请求,首先创建内容,然后使用附件进行更新.遗憾的是,C
我们使用 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的源代码后,根据规范,这似乎不是必需的元素.这看起来像阿布德拉代码中的一个错误,不是吗?

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读