Over a million developers have joined DZone.

Building a simple AtomPub client with NetBeans IDE 7, Maven, Java and Apache Abdera

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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.

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.

From http://www.giantflyingsaucer.com/blog/?p=2961

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}