public class Main {
public static void main(String[] args) throws Exception {
Abdera abdera = new Abdera();
Client client = new CommonsClient(abdera);
Factory factory = abdera.getFactory();
// Perform introspection. This is an optional step. If you already
// know the URI of the APP collection to POST to, you can skip it.
Document<Service> introspection =
client.get(
args[0]).getDocument();
Service service =
introspection.getRoot();
Collection collection =
service.getCollection(
args[1],
args[2]);
report("The Collection Element", collection.toString());
// Create the entry to post to the collection
Entry entry = factory.newEntry();
entry.setId("tag:example.org,2006:foo");
entry.setTitle("This is the title");
entry.setUpdated(new Date());
entry.addAuthor("James");
entry.setContent("This is the content");
report("The Entry to Post", entry.toString());
// Post the entry. Be sure to grab the resolved HREF of the collection
Document<Entry> doc = client.post(
collection.getResolvedHref().toString(),
entry).getDocument();
// In some implementations (such as Google's GData API, the entry URI is
// distinct from it's edit URI. To be safe, we should assume it may be
// different
IRI entryUri = doc.getBaseUri();
report("The Created Entry", doc.getRoot().toString());
// Grab the Edit URI from the entry. The entry MAY have more than one
// edit link. We need to make sure we grab the right one.
IRI editUri = getEditUri(doc.getRoot());
// If there is an Edit Link, we can edit the entry
if (editUri != null) {
// Before we can edit, we need to grab an "editable" representation
doc = client.get(editUri.toString()).getDocument();
// Change whatever you want in the retrieved entry
doc.getRoot().getTitleElement().setValue("This is the changed title");
// Put it back to the server
client.put(editUri.toString(), doc.getRoot());
// This is just to show that the entry has been modified
doc = client.get(entryUri.toString()).getDocument();
report("The Modified Entry", doc.getRoot().toString());
} else {
// Otherwise, the entry cannot be modified (no suitable edit link was found)
report("The Entry cannot be modified", null);
}
// Delete the entry. Again, we need to make sure that we have the current
// edit link for the entry
doc = client.get(entryUri.toString()).getDocument();
editUri = getEditUri(doc.getRoot());
if (editUri != null) {
client.delete(editUri.toString());
report("The Enry has been deleted", null);
} else {
report("The Entry cannot be deleted", null);
}
}