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>

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. XMLApiHelper
  2. AnyRef
  3. Any
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: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. 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.

  8. 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
  9. 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.

  10. 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".

  11. def clone(): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  12. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  14. def finalize(): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  15. final def getClass(): java.lang.Class[_]

    Definition Classes
    AnyRef → Any
  16. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  17. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  18. 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.

  19. def msgAttrName: String

    The name for the msg attribute

  20. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  21. final def notify(): Unit

    Definition Classes
    AnyRef
  22. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  23. 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
  24. def operationAttrName: String

    The name for the operation attribue

  25. 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.

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

    Converts a given LiftResponse into a Full[LiftResponse]

  27. def successAttrName: String

    The name for the success attribute

  28. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  29. def toString(): String

    Definition Classes
    AnyRef → Any
  30. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  31. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  32. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any