Christoph Hartmann on November 15th, 2008

I set up an mobile application that sends data via a REST based interface to a Grails-based server. Unfortunately J2ME does not support all HTTP methods. To overcome this weakness we have to emulate the methods PUT and DELETE. Like Rails just did this by adding a “_method=PUT” parameter to the query URL, we will do the same. Rails does the HTTP method emulation handling automatically, whilst in Grails we have do this by ourselves. By using the UrlMapping.groovy it will become quite easy to overcome this weakness. A standard REST mapping should look like:

 
class UrlMappings {
   static mappings = {
          // identifies a rest object like /rest/airport/1
	  "/rest/$domain/$id"{
		  controller = "rest"
		  action = [GET:"show", PUT:"create", POST:"update",DELETE:"delete"]
         }
   }
}

The action attribute may be a simple string, a hash map or a closure. Grails calls the following method evaluateNameForValue(Object value, GrailsWebRequest webRequest) to retrieve the value. The method is located within the org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.

private String evaluateNameForValue(Object value, GrailsWebRequest webRequest) {
        if (value == null) {
            return null;
        }
        String name;
        if (value instanceof Closure) {
            Closure callable = (Closure) value;
            Object result = ((Closure) callable.clone()).call();
            name = result != null ? result.toString() : null;
        } else if (value instanceof Map) {
            Map httpMethods = (Map) value;
            name = (String) httpMethods.get(webRequest.getCurrentRequest().getMethod());
        } else {
            name = value.toString();
        }
        return name;
    }

Due to our knowledge of the action retrieval process, we are able to implement a “_method” emulation. This could look like:

 
class UrlMappings {
   static mappings = {
          // identifies a rest object like /rest/airport/1
	  "/rest/$domain/$id"{
		  controller = "rest"
		  // action = [GET:"show", PUT:"create", POST:"update",DELETE:"delete"]
 
	         action = {
		   // closure will not be invoked with request, log ... therefore we have to do this manually
		  GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
		  String method = webRequest.getCurrentRequest().getMethod();
		  String methodParam = webRequest.getCurrentRequest().getParameter("_method");
 
		  // check if we can use _method
		  if (methodParam != null) {
		    methodParam = methodParam.toUpperCase()
 
		    switch (methodParam) {
			case "PUT" :
			case "DELETE" :
			case "POST" :
			case "GET" :
			  method = methodParam;
			break
		  }
		} 
 
		switch (method.toUpperCase()) {
		  case "PUT" : return "create"
			break
		  case "DELETE" : return "delete"
			break
		  case "POST" : return "update"
			break
		  case "GET" : return "show"
			break
		  default:  return "show"
		        break
		}
		return method;
		}
         }
   }
}

I hope that helps all Grails REST developers to enable their services for J2ME.

Tags: , , ,

4 Responses to “J2ME, REST and Grails”

  1. Hi,
    Do you know how to create REST web service provider in J2ME ?
    Thanks,
    Niko

  2. Hey Niko,

    i am not sure what your intention is. Do you mean a full HTTP stack that provide a web service? To be honest.. I think it’s possible but you have to do a lot of things by your own. Because you have to use the ServerSocketConnection, you have to write the whole connection management. Would not recommend such solution but it depends on your ideas.

    Did i got you right?

    Christoph

  3. Hi Christoph,
    Nice article. Quite interesting to know some attempts of REST for j2me. I am finding it difficult to develop a REST client in J2Me..using java. I have a simple smart card web server..and it has to consume a REST service from a GlassFish server. The SCWS does support GET and PUT, but I am not able to capture the response from the REST service, it lands on my browser and displays as xml structure. I need to process them in my code need to land it on my doGet method..could you give any Suggestions on this please..
    thanks
    Domnic

  4. Hey Domnic, I wrote a new post that should answer your questions. I hope that article helps you. Take a look at J2ME Rest Client

Leave a Reply