How to send content and attachments using Abdera atom client

We use Abdera to interact with IBM connections API, but our problems are mainly related to Abdera itself

I think there is an error in Abdera, which does not allow you to send entries containing content and attachments in a single request As a workaround, you may send two separate requests, first creating content and then updating it with attachments Unfortunately, the connections API requires you to include all data in a single request, otherwise the old data will not be retained

The following code shows the Abdera entry 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");

Nullpointer thrown when creating multipartrelatedrequestentity:

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

This is because it expects a content "SRC" element, but after in-depth study of Abdera's source code, according to the specification, this does not seem to be a necessary element It looks like an error in abdela's code, doesn't it?

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

I've put a reference application connection into IBM green connections to show this, but there are also two unit tests that can test nullpointer without connections This can be found in GitHub

Solution

It is possible to make it work with Abdera for future reference. This is an example of publishing an item containing text content and one (or more) attachments You need to use the part of the basic 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 allows us to create an IBM connections activity entry containing content and attachments in a request because the API needs to do so

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>