Thursday, June 20, 2013

Restlet how to post XML data from client

Recently, I was issued a project to set up a restful web server on Android platform. Googling landed me Restlet Framework and I must say it's a pretty good framework that gives a nice abstraction layer to do any REST related work.

Two things I really didn't like about Restlet is that initial setup was a bit painful, but what really ticked me off is the lack of documentations, which forced me to delve into the source code whenever I was stuck with something. Seriously guys, if you are going to make a framework and you actually want other developers using it, save us the trouble and provide a good documentation. Not everyone has the patience to look through source codes.

Anyways, I had a really weird issue with posting XML data from client to server using DomRepresentation. Both ends are using Restlet, using the exact same class to handle XML data but the server wasn't receiving the DOM no matter what I tried:

public void sendPostRequest(final String uri, final DomRepresentation entity) throws ResourceException, IOException {
 try {
  ClientResource client = new ClientResource(uri);
  client.post(entity); 
 }
 catch (Exception ex) {
  ex.printStackTrace();
 }
}


@Post
public void handleQuery(Representation rep) {
 try {
  if (rep == null) {
   this.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
   return;
  }
  DomRepresentation domRep = new DomRepresentation(rep); 
  Document doc = domRep.getDocument(); //THROWS ERROR. 
                ....


Needless to say, this left me bewildered and I almost considered tossing the abstraction from the framework out the window and handling this at the HTTP level. Luckily, I eventually found out that DomRepresentation didn't work as advertised and instead, I had to work with StringRepresentation:

public void sendPostRequest(final String uri, final DomRepresentation entity) throws public static void sendPostRequest(final String uri, final Document doc) throws ResourceException, IOException {
 try {
  ClientResource client = new ClientResource(uri);
  StringRepresentation entity = new StringRepresentation(XMLUtils.getStringFromDocument(doc)); //Turn DOM into string and then instantiate StringRepresentation
  entity.setCharacterSet(null); 
  entity.setMediaType(MediaType.TEXT_XML);
  client.post(entity); 
 }
 catch (Exception ex) {
  ex.printStackTrace();
 }

I'm still not sure why DomRepresentation did not work in my case. It's either a bug or some obscure configuration that I must've missed (which clearly isn't mentioned in the documentation). Either way, this was pretty frustrating and I thought it merited a post in case other people had a similar problem.
By the way, if you are using Restlet, get yourself a copy of Restlet in Action. Given the lack of documentation, this will help you out a bunch. Seriously, just do it.

Monday, June 10, 2013

IE 9/10 Enter Key causing Form Submit / Button Click workaround

If you ever run into a problem where if you press enter inside a page and the page suddenly submits or a button click event launches, you need to manually stop the enter event from propagating outside a particular div.

//"Enter" Key is taken as a submit action by default on IE 9/10
//Prevent Enter key event from propagating outside the grid.
$('#SUBUL_01_DIV_GRID').keypress(function(e) {
    if(e.which == 13) { 
        e.preventDefault(); 
    }
});
IE, causing massive amount of frustration to web developers like always..