2010-07-03

REST with Scala's Lift framework. Part 3 - PUT

In the previous posts I explained how to handle GET and POST requests in Scala's Lift framework in a RESTful manner. Now it's time to look into...


PUT requests


PUT is for updating objects. It's not really different from POST from developer's point of view. It's just that you get an identifier of the object from request.

(I already did that for Spring 3, you may want to take a look: REST with Spring 3: PUT.)


Retrieve the request


package bootstrap.liftweb

import me.m1key.rest.DogsRestService
import net.liftweb.http._

class Boot {
   def boot {
       // where to search snippet
       LiftRules.addToPackages("me.m1key")

       LiftRules.statelessDispatchTable.append {
           // ...
           case request @ Req(List("rest", "dogs"), _, PutRequest) =>
               () => DogsRestService.updateDog(request)
       }
   }
}

It's very similar to POST, so you can check out the POST post (really cunning game play here, folks!) for more details.


Processing the request


Processing the request is very similar to the PUT case.

    def updateDog(request: Req): Box[LiftResponse] = {
       var dogId = ""
       var dogName = ""
       request.xml match {
             case Full(<dog>{parameters @ _*}</dog>) => {
               for(parameter <- parameters){
                   parameter match {
                       case <name>{name}</name> => dogName = name.text
                       case <id>{id}</id> => dogId = id.text
                       case _ =>
                   }
               }
               val dog = new Dog(dogId, dogName)
               Log.info("Updating a dog with id: " + dog.id)
               return Full(InMemoryResponse(dog.toXml.toString.getBytes("UTF-8"), List("Content-Type" -> "text/xml"), Nil, 200))
           }
           case _ => Log.error("Invalid request");
               Log.error("Request: " + request);
               Log.error("Request.xml: " + request.xml);Full(BadResponse())
       }
   }


Summary


Similarly to the POST case, handling RESTful PUT requests in Lift 1.0 is not as comfortable as it is with Spring 3. I'm looking forward to seeing Lift 2.0 improvements in that matter.

Download source code for this article

1 comment:

  1. CPR courses in Perth offer essential life-saving skills, covering techniques to handle cardiac and breathing emergencies. Accredited by organizations like the West Coast First Aid Training, these courses teach participants how to perform effective chest compressions, rescue breaths, and the use of automated external defibrillators (AEDs). Cpr courses Perth are available for all skill levels, from beginners to advanced.

    They are typically short, hands-on, and can be completed within a few hours, often with certification upon completion. With flexible scheduling at locations across Perth, these courses make it convenient for individuals, professionals, and workplaces to meet safety requirements and be prepared in emergencies.

    ReplyDelete