How To Use Java With Blogger: A Tutorial
Blogger has a REST API. I use it to retrieve particular posts or to post transformed data. There is no Java API that I know of, but you will see here it is not very difficult to interface with Blogger API in Java using plain old XML.Using libraries commons-httpclient and DOM4J it would be quite easy to implement your own Java Blogger API as the following code will suggest.
Authenticate
All requests need to be authenticated and are done in HTTPS. I use common-httpclient to perform requests. Here is how to setup the client:
private HttpClient initHttpClient()
{
HttpClient client = new HttpClient();
List authPrefs = new ArrayList(2);
authPrefs.add(AuthPolicy.DIGEST );
authPrefs.add(AuthPolicy.BASIC);
client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
client.getState().setCredentials(new AuthScope( "www.blogger.com", 443, AuthScope.ANY_REALM), defaultcreds);
return client;
}
Get Your Posts
To retrieve the posts, you just have to query the right url, and parse the XML response. I prefer to use DOM4J, because of its handy asXML() method to print a node as XML. For simplicity I use a Map to store an XML entry.
public Collection getPosts() throws HttpException, IOException, ParserConfigurationException, SAXException, DocumentException
{
GetMethod get = new GetMethod("https://www.blogger.com/atom" +"/"+blogId);
int statusCode = client.executeMethod(get);
if (statusCode != HttpStatus.SC_OK)
{
throw new RuntimeException(" Could not make HTTP request properly: " +get.getStatusLine());
}
InputStream response = get.getResponseBodyAsStream();
SAXReader reader = new SAXReader();
Document doc = reader.read(response);
Collection posts = new ArrayList();
List entries = doc.getRootElement().elements("entry");
if (LOG.isDebugEnabled())
{
LOG.debug("found "+entries.size()+" entries");
}
for (int i = 0; i <entries.size();i ++)
{
Element entry = (Element) entries.get(i);
Map m = new HashMap();
for (Iterator it = entry.elementIterator();it. hasNext();)
{
Element detail = (Element) it.next();
String name = detail.getName();
if (name.equals("link"))
{
m.put("link ",detail.attribute("href").getValue());
}
else if (name. equals("content"))
{
m.put("content",detail.asXML());
}
else
{
m.put(name,detail.getTextTrim());
}
}
posts.add(m);
if (LOG.isDebugEnabled())
{
LOG.debug( "found="+m.get(" title")+", url= "+m.get("link"));
}
}
return posts;
}
Create XML for a new Post
Nothing particular here, just XML production.
private String createXmlForCreatePost(String postTitle, String postContent) throws IOException, DocumentException
{
SAXReader xmlReader = new SAXReader();
xmlReader.setValidation(false );
Document doc = DocumentHelper.createDocument();
QName rootName = DocumentHelper.createQName("entry", new Namespace("", "http://purl.org/atom/ns# "));
Element root = doc.addElement(rootName);
Element title = root.addElement("title");
title.addAttribute("mode"," escaped");
title.addAttribute("type ","text/plain");
title.setText (postTitle);
Element generator = root.addElement("generator ");
generator.addAttribute("url" , "http://31416.org");
generator. setText("31416 Java Generator ");
Element content = root.addElement("content ");
content.addAttribute("type" , "application/xhtml+xml");
//Element div = content.addElement(DocumentHelper.createQName("div",new Namespace("","http://www.w3.org/1999/xhtml")));
//div.add(...); //YOUR XHTML HERE
StringWriter result = new StringWriter();
XMLWriter writer = new XMLWriter (result);
writer.write(doc);
writer.close();
return result.toString();
}
That's it