In my last article I showed you how to create and run a simple AtomPub server using Apache Abdera. Today we will create a client (using modified code provided in this guide as well as the Apache Abdera examples) to put some data into the AtomPub server.
Updated Oct. 31, 2011: Code updated to return the actual status upon creation of a new ATOM entry
At this point I assume you already have Java and Netbeans 7 running, if not you can go here to get them both. I also assume you have some Netbeans and Java knowledge.
Start Netbeans 7 and create a new Maven Java project named: ATOMPubClient
Right-click on the Dependencies and select: Add Dependency… When the dialog opens up put in the Query textbox the following: abdera-client
In the App.java class (already created) modify it so the code looks like this:
/*
* Original code created by the Apache Abdera team
* http://abdera.apache.org/
*/
package com.giantflyingsaucer.atompubclient;
import java.util.Date;
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Entry;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.abdera.protocol.client.RequestOptions;
public class App {
public static void main(String[] args) throws Exception {
Abdera abdera = new Abdera();
AbderaClient abderaClient = new AbderaClient(abdera);
Factory factory = abdera.getFactory();
Entry entry = factory.newEntry();
entry.setId("tag:example.org,2011:foo");
entry.setTitle("This is the title");
entry.setUpdated(new Date());
entry.addAuthor("Chad");
entry.setContent("Hello World");
report("The Entry to Post", entry.toString());
RequestOptions opts = new RequestOptions();
opts.setContentType("application/atom+xml;type=entry");
ClientResponse resp = abderaClient.post("http://localhost:9002/employee", entry, opts);
report("HTTP STATUS CODE", resp.getStatusText());
}
private static void report(String title, String message) {
System.out.println("== " + title + " ==");
if (message != null)
System.out.println(message);
System.out.println();
}
}
Results:
== The Entry to Post == <entry xmlns="http://www.w3.org/2005/Atom"><id>tag:example.org,2011:foo</id><title type="text">This is the title</title><updated>2011-10-31T19:46:26.891Z</updated><author><name>Chad</name></author><content type="text">Hello World</content></entry> == HTTP STATUS CODE == Created
This is very simple, an entry is created and the feed is returned – nothing more. I’ve ignored the capability of editing and deleting. This code can be improved in several ways but my purpose was just to make it a bit easier for developers to get started with Apache Abdera and to supplement the existing documents.
The full source code is available here.







Pingback: Building a simple AtomPub server using Netbeans 7, Jetty 7, Maven, Java and Apache Abdera | Giant Flying Saucer
Pingback: Build a simple AtomPub client to convert ATOM XML to JSON | Giant Flying Saucer