Building a simple AtomPub client with NetBeans IDE 7, Maven, Java and Apache Abdera
Join the DZone community and get the full member experience.
Join For FreeIn 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.
At this point I assume you already have Java and NetBeans IDE 7 running, if not you can go here to get them both. I also assume you have some NetBeans IDE and Java knowledge.
Start NetBeans IDE 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.Document; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.protocol.Response.ResponseType; import org.apache.abdera.protocol.client.AbderaClient; import org.apache.abdera.protocol.client.ClientResponse; 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()); Document<Entry> doc = abderaClient.post("http://localhost:9002/employee", entry).getDocument(); report("The Created Entry", doc.getRoot().toString()); ClientResponse resp = abderaClient.get("http://localhost:9002/employee"); if (resp.getType() == ResponseType.SUCCESS) { Document<Feed> docFeed = resp.getDocument(); report("The Returned Feed", docFeed.getRoot().toString()); } else { // Error } } private static void report(String title, String message) { System.out.println("== " + title + " =="); if (message != null) System.out.println(message); System.out.println(); } }
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.
Opinions expressed by DZone contributors are their own.
Comments