AtomME: Posting from a Java mobile phone via the Atom API
Here's a copy of an email I just sent out to Atom Syntax mailing list:
Sorry for the delay on this, I just ran into a bug I couldn't work out, then got caught up in other issues and couldn't get back to it until this evening.
Last week we were deep in the POST vs. PUT/DELETE debate when I started working with Sam Ruby on a simple J2ME app which would post to an Atom-enabled weblog via J2ME. Sam did the heavy lifting of working out the Base64 and SHA1 encoding in J2ME-compatible Java (which he published on his weblog last week) and I used that code to create a small J2ME application which simply allows you to post a title and content field to a weblog.
If you want to just play with the app on your mobile, I added a link to the .jad/.jar to a WAP page, which you can get to from my short url:
http://rbeattie.com/wap
Navigate to Links, then "Install AtomME". It should download and install. I tested it on the phones I have handy at the moment: a Nokia 7610, 6200, SonyEricsson 616 and a Motorola v400. It's a *very* basic app - the J2ME equivalent of a command-line program. I usually use this sort of code as a harness to test J2ME functionality. It simply allows you to fill out a form with your posting information for a server like TypePad (in fact it defaults to their API) then dumps out the XML being passed over the wire to the screen.
I've posted the code under an MIT license here:
http://www.russellbeattie.com/download/AtomME.zip
In order to use it, you'll need to have Sun's Wireless Toolkit (WTK), Ant, and Antenna installed. Edit the build.properties file to point at the at the directory where the WTK is and you should be able to compile it quickly. To run the app in the emulator, simply type "ant emulate."
There were several problems getting this working. First, the Base64 and SHA1 code is non-trivial, but Sam has gotten that down to a small class which I just grabbed and included, but if I had to develop that myself it wouldn't have happened. The second problem was the date formatting. Some phones (like my beloved, but flawed Nokia 6600) have real problems with the Calendar object. I finally gave up trying to get that phone to work.
And thirdly and most importantly, neither TypePad nor Blogger (my two testing servers) supported chunked HTTP posting. For various reasons having to do with unreliable wireless networks, the J2ME Connection class will chunk the HTTP whether you like it or not. I did find a way of not forcing the chunking by not calling OutputStream.flush(), but that only works on some phones and if the data gets long enough the KVM/JVM will still chunk whether you like it or not. Since chunking is part of the HTTP 1.1 spec, I don't feel this isn't a big deal in terms of the Atom Spec: the servers just have a bug at the moment which they need to fix.
So my thoughts are this: SHA1 encoding isn't bad if you have someone else write it for you ;-), setting headers on the phone works (at least on my carrier), and if POST were the only HTTP verb required, Atom could be implemented on a wide variety of handsets right now. The fact that this app runs on the 6200 and T616 show a lot - those are *not* very powerful phones.
This is just a first step. I'm sure someone can take this code and build on it to create a more full-featured Atom client which can do much more. For example, using the MultiMedia APIs, it should be possible to post PNG images and maybe even video via Atom from J2ME as well.
The next step is for everyone who has a Java-enabled mobile to try the app. (Feel free to send bug reports to me, just don't expect a prompt answer. :-) ) Do you have a phone which doesn't work? We'll have to figure out why - maybe it's a bug, maybe it's something more serious. Do you have a carrier which strips out the headers? Etc. etc. Then we can move on to trying to implement the SOAP-based PUT/DELETE alternative and see how well that works. I would *much* rather have some sort of action header or stanza, as it would make this code much more reusable and maintainable, but let's try what's speced now and see.
Thanks,
-Russ