t

net.liftweb.http.rest

XMLApiHelper

trait XMLApiHelper extends AnyRef

Mix this trait into your REST service provider to convert between different response types and a LiftResponse. You need to define the createTag method to provide a root element for your API. You may optionally override the successAttrName, operationAttrName, and/or msgAttrName defs to control the attributes that will be applied to your root element based on the return from your API.

For example, the following code implements a simple API that takes a comma- separated string of integers and reduces them with various operations.

object CalculatorApi extends XmlApiHelper {
  // Define our root tag
  def createTag(contents : NodeSeq) : Elem = <api>{contents}</api>

  // The LiftResponses here will be converted to Box[LiftResponse]
  // via the putResponseInBox implicit conversion
  def calculator : LiftRules.DispatchPF = {
    case r @ Req(List("api","sum"), _, GetRequest) => () => doSum(r)
    case r @ Req(List("api","product"), _, GetRequest) => () => doProduct(r)
    case r @ Req(List("api","max"), _, GetRequest) => () => doMax(r)
    case r @ Req(List("api","min"), _, GetRequest) => () => doMin(r)
    case Req("api" :: _, _, _) => () => BadResponse()
  }

  // Define a common handler
  def reduceOp (operation : (Int,Int) => Int)(r : Req) : Box[Elem] = tryo {
    (r.param("args").map {
      args => <result>{args.split(",").map(_.toInt).reduceLeft(operation)}</result>
     }) ?~ "Missing args"
  } match {
    case Full(x) => x
    case f : Failure => f
    case Empty => Empty
  }

  // Using a return type of LiftResponse causes the canNodeToResponse
  // implicit to be invoked
  def doSum (r : Req) : LiftResponse = reduceOp(_ + _)(r)
  def doProduct (r : Req) : LiftResponse = reduceOp(_ * _)(r)
  def doMax (r : Req) : LiftResponse = reduceOp(_ max _)(r)
  def doMin (r : Req) : LiftResponse = reduceOp(_ min _)(r)
}

With this API, the URL

http://foo.com/api/sum?args=1,2,3,4,5
would return
<api operation="sum" success="true"><result>15</result></api>

http://foo.com/api/sum?args=1,2,3,4,5 return

<api operation="sum" success="true"><result>15</result></api>

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. XMLApiHelper
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def createTag(in: NodeSeq): Elem

    The method that wraps the outer-most tag around the body.

    The method that wraps the outer-most tag around the body. The success, operation and msg attributes will be merged into the returned Elem.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. implicit def boolToResponse(in: Boolean): LiftResponse

    Converts a boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter.

  6. def buildResponse(success: Boolean, msg: Box[NodeSeq], body: NodeSeq): LiftResponse

    Build the Response based on Success, an optional message and the body

    Build the Response based on Success, an optional message and the body

    Attributes
    protected
  7. implicit def canBoolToResponse(in: Box[Boolean]): LiftResponse

    Converts a boxed boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter.

    Converts a boxed boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter. If the Box is a Failure, the "msg" attribute of the root element will be set to the Failure's msg value.

  8. implicit def canNodeToResponse(in: Box[Seq[Node]]): LiftResponse

    Converts a boxed Seq[Node] into a response.

    Converts a boxed Seq[Node] into a response. If the Box is a Full, the root element uses the contents of the Box as its contents, and sets the "success" attribute to "true". If the Box is a Failure, the "success" attribute is set to "false" and the "msg" attribute is set to the Failure's msg value. If the Box is Empty then the root element is returned with no contents and the "success" attribute set to "false".

  9. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int
    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. implicit def listElemToResponse(in: Seq[Node]): LiftResponse

    Converts a Seq[Node] into a root element with the "success" attribute set to "true" and the Seq[Node] as the contents.

  17. def msgAttrName: String

    The name for the msg attribute

  18. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  19. final def notify(): Unit
    Definition Classes
    AnyRef
  20. final def notifyAll(): Unit
    Definition Classes
    AnyRef
  21. def operation: Option[NodeSeq]

    Determines the value to place in the "operation" attribute of the root element based on the second element of the request path.

    Determines the value to place in the "operation" attribute of the root element based on the second element of the request path.

    Attributes
    protected
  22. def operationAttrName: String

    The name for the operation attribue

  23. implicit def pairToResponse(in: (Boolean, String)): LiftResponse

    Converts a pair of (Boolean,String) into a response of a root element with no contents, the "success" attribute set to the value of the first element of the pair, and the "msg" attribute set to the value of the second element of the pair.

  24. implicit def putResponseInBox(in: LiftResponse): Box[LiftResponse]

    Converts a given LiftResponse into a Full[LiftResponse]

  25. def successAttrName: String

    The name for the success attribute

  26. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  27. def toString(): String
    Definition Classes
    AnyRef → Any
  28. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  29. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped