Blogs have long since used Atom and RSS feeds for both the consuming and publishing of information. So the Atom feeds used for the Blogs API are a natural fit. Since there are a variety of tools and applications that exist to consume blog feeds, this article focuses on the publishing of blog entries and comments.
To post a new blog entry, first retrieve the service document for the blog. The service document contains the URL necessary for posting new entries. The input for the API is a blog entry document.
// Create an Abdera client and add credentials to it.
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
AbderaClient.registerTrustManager();
client.addCredentials("https://server", null, null, new UsernamePasswordCredentials("user","password"));
// After authentication, we use Abdera to get the service document and parse the document to get required collection.
ClientResponse resp = client.get("https://server/blogs/services/atom?lang=en_us");
Document<Service> ServiceDocument = resp.getDocument();
ServiceDocument.writeTo(System.out);
Service service = ServiceDocument.getRoot();
// Provide title of workspace and title of the Blogs collection at this stage to retrieve the collection.
// In this example, Sample Blog is the name of the blog. Weblog Entries is the collection of entries to be returned.
Collection collection = service.getCollection("Sample Blog", "Weblog Entries");
// Obtain collections uri where entry needs to be posted.
String CollectionURI = collection.getResolvedHref().toASCIIString();
// Create a new atom entry document as below which needs to be posted to the blog.
Entry entry = abdera.newEntry();
entry.newId();
entry.addAuthor("John Doe");
entry.setUpdated(new Date());
entry.setTitle("Test entry from Java");
entry.setContentAsHtml("This is the content of the entry.");
entry.addCategory("tag1");
entry.addCategory("tag2");
//Post the created entry document to the collections uri obtained from service document.
RequestOptions opt = client.getDefaultRequestOptions();
resp = client.post(CollectionURI, entry, opt );
To post a comment to a blog entry, we first retrieve the service document for the blog. Then we use the Comment Entries collection to find the URL for posting comments. Each comment must be attached to an entry or another comment so we also must find the ID of the blog entry to post the comment against.
// Create an Abdera client and add credentials to it.
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
AbderaClient client = new AbderaClient(abdera);
AbderaClient.registerTrustManager();
client.addCredentials("https://server", null, null, new UsernamePasswordCredentials("user","password"));
// After authentication we use Abdera to get the service document and parse the document to get required collection.
ClientResponse resp = client.get("https://server/blogs/api");
Document<Service> ServiceDocument = resp.getDocument();
ServiceDocument.writeTo(System.out);
Service service = ServiceDocument.getRoot();
// Provide title of workspace and title of the Blogs collection at this stage to retrieve the collection.
// In this example, Sample Blog is the name of the blog. Weblog Entries is the collection of entries to be returned.
Collection collection = service.getCollection("Sample Blog", "Weblog Entries");
String CollectionURI = collection.getResolvedHref().toASCIIString();
// Use the Comment Entries collection to find the URL to post comments
Collection collectionComments = service.getCollection("Sample Blog", "Comment Entries");
String CommentURI = collectionComments.getHref().toASCIIString();
Document<Element> doc;
URL url = new URL(CollectionURI);
resp = client.get(url.toString());
doc = parser.parse(resp.getInputStream());
Feed feed = (Feed) doc.getRoot();
Entry entry2 = null;
// Loop through the entries to find the id of the entry to post the comment
for (Entry entry : feed.getEntries()) {
System.out.println("\t" + entry.getTitle()); // This prints the title of each comment entry
System.out.println("\t" + entry.getContent()); // This prints the actual content of the comment .
System.out.println("\t" + entry.getId()); // This prints the comment entry id .
System.out.println("\t" + entry.getLink("edit"));
entry2 = entry;
}
// Creating the atom entry that needs to be posted .
Entry entry1 = abdera.newEntry();
entry1.setTitle("Sample Comment");
entry1.setContent("html");
entry1.setContentAsHtml("This is another sample comment HTML content");
// ThreadHelper Class used to add the tag to the entry1
// and entry 2 is the Blog entry to which this comment is a response . The of entry2 is added to the tag on entry1
ThreadHelper.addInReplyTo(entry1, entry2);
System.out.println(entry1);
//Construction in entry1 completed now post to the Blogs API.
RequestOptions opt = client.getDefaultRequestOptions();
resp = client.post(CommentURI , entry1, opt);
System.out.println("posted?" + resp.getStatus() + "/" + resp.getStatusText());