I want to validate REST requests (GET and PUT) in LIFT before processing it. i.e. i need to check whether requests has a parameter requestor. if not need to response with exception saying missing parameter. Could you please let me know how to do this.
There are a few things you could do. The two I would try would be a helper function that would wrap your Rest call, something like:
def checkParam(r:Req):Boolean = {
r.param("paramName").isDefined
}
def requireParams[T<:LiftResponse](r:Req)(methodBody: => T):LiftResponse = {
if(checkParam(r))
methodBody
else
InMemoryResponse("Parameters not specified".getBytes(), List("content-type" -> "text/plain"), Nil, 500)
}
The function will check for the parameters and return an error if it doesn't work, or execute the call if it does. In your Rest call, you would use it like:
case "location" :: Nil Get req => requireParams(req){
//your rest body
}
Alternately, you could probably use a guard on the entire RestHelper
assuming you wanted to check every method call, something like this might work:
val ensureParams: PartialFunction[Req, Unit] = {
case r if (r.get_? || r.put_?) && checkParam(r) =>
case r if (!r.get_? && !r.put_?) =>
}
and then guard your RestHelper instance in Boot with:
LiftRules.dispatch.append(ensureParams guard YourRestHelper)
I haven't tested the above code, so there may be some mistakes - but hopefully it should help get you started.