Skip to content
This repository
tree: 655789cb5a
Fetching contributors…

Cannot retrieve contributors at this time

file 721 lines (616 sloc) 22.558 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/*
* Copyright 2010-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb
package http
package rest


import net.liftweb._
import json._
import common._
import util._
import scala.xml.{Elem, Node, Text}

/**
* Mix this trait into a class to provide a list of REST helper methods
*/
trait RestHelper extends LiftRules.DispatchPF {
  import JsonAST._

  /**
* Will the request accept a JSON response? Yes if
* the Accept header contains "text/json", "application/json" or
* the Accept header is missing or contains "star/star" and the
* suffix is "json". Override this method to provide your
* own logic
*/
  protected def jsonResponse_?(in: Req): Boolean = {
    (in.acceptsJson_? && !in.acceptsStarStar) ||
    ((in.weightedAccept.isEmpty ||
      in.acceptsStarStar) && in.path.suffix.equalsIgnoreCase("json")) ||
    suplimentalJsonResponse_?(in)
  }

  /**
* Take any value and convert it into a JValue. Full box if
* it works, empty if it does
*/
  protected def anyToJValue(in: Any): Box[JValue] = Helpers.tryo{
    implicit def formats = DefaultFormats
    Extraction.decompose(in)
  }
  
  /**
* If there are additional custom rules (e.g., looking at query parameters)
* you can override this method which is consulted if the other rules
* in jsonResponse_? fail
*/
  protected def suplimentalJsonResponse_?(in: Req): Boolean = false
  
  /**
* Will the request accept an XML response? Yes if
* the Accept header contains "application/xml" or "text/xml" or
* the Accept header is missing or contains "star/star" and the
* suffix is "xml". Override this method to provide your
* own logic
*/
  protected def xmlResponse_?(in: Req): Boolean = {
    (in.acceptsXml_? && !in.acceptsStarStar) ||
    ((in.weightedAccept.isEmpty ||
      in.acceptsStarStar) && in.path.suffix.equalsIgnoreCase("xml")) ||
    suplimentalXmlResponse_?(in)
  }
  
  /**
* If there are additional custom rules (e.g., looking at query parameters)
* you can override this method which is consulted if the other rules
* in xmlResponse_? fail
*/
  protected def suplimentalXmlResponse_?(in: Req): Boolean = false
  
  /**
* A trait that defines the TestReq extractor. Is
* the request something that expects JSON, XML in the response.
* Subclass this trait to change the behavior
*/
  protected trait TestReq {
    /**
* Test to see if the request is expecting JSON, XML in the response.
* The path and a Tuple2 representing RequestType and the Req
* instance are extracted.
*/
    def unapply(r: Req): Option[(List[String], (RequestType, Req))] =
      if (testResponse_?(r))
        Some(r.path.partPath -> (r.requestType -> r)) else None

    def testResponse_?(r: Req): Boolean
  }

  protected trait XmlTest {
    def testResponse_?(r: Req): Boolean = xmlResponse_?(r)
  }

  protected trait JsonTest {
    def testResponse_?(r: Req): Boolean = jsonResponse_?(r)
  }

  /**
* The stable identifier for JsonReq. You can use it
* as an extractor.
*/
  protected lazy val JsonReq = new TestReq with JsonTest

  /**
* The stable identifier for XmlReq. You can use it
* as an extractor.
*/
  protected lazy val XmlReq = new TestReq with XmlTest
  

  /**
* A trait that defines the TestGet extractor. Is
* the request a GET and something that expects JSON or XML in the response.
* Subclass this trait to change the behavior
*/
  protected trait TestGet {
    /**
* Test to see if the request is a GET and expecting JSON in the response.
* The path and the Req instance are extracted.
*/
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.get_? && testResponse_?(r))
        Some(r.path.partPath -> r) else None


    def testResponse_?(r: Req): Boolean
  }

  /**
* The stable identifier for JsonGet. You can use it
* as an extractor.
*/
  protected lazy val JsonGet = new TestGet with JsonTest

  /**
* The stable identifier for XmlGet. You can use it
* as an extractor.
*/
  protected lazy val XmlGet = new TestGet with XmlTest



  /**
* A trait that defines the TestDelete extractor. Is
* the request a DELETE and something that expects
* JSON or XML in the response.
* Subclass this trait to change the behavior
*/
  protected trait TestDelete {
    /**
* Test to see if the request is a DELETE and
* expecting JSON or XML in the response.
* The path and the Req instance are extracted.
*/
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.requestType.delete_? && testResponse_?(r))
        Some(r.path.partPath -> r) else None


    def testResponse_?(r: Req): Boolean
  }

  /**
* The stable identifier for JsonDelete. You can use it
* as an extractor.
*/
  protected lazy val JsonDelete = new TestDelete with JsonTest

  /**
* The stable identifier for XmlDelete. You can use it
* as an extractor.
*/
  protected lazy val XmlDelete = new TestDelete with XmlTest

  /**
* A trait that defines the TestPost extractor. Is
* the request a POST, has JSON or XML data in the post body
* and something that expects JSON or XML in the response.
* Subclass this trait to change the behavior
*/
  protected trait TestPost[T] {
    /**
* Test to see if the request is a POST, has JSON data in the
* body and expecting JSON in the response.
* The path, JSON Data and the Req instance are extracted.
*/
    def unapply(r: Req): Option[(List[String], (T, Req))] =
      if (r.post_? && testResponse_?(r))
        body(r).toOption.map(t => (r.path.partPath -> (t -> r)))
      else None

    
    def testResponse_?(r: Req): Boolean

    def body(r: Req): Box[T]
  }

  /**
* a trait that extracts the JSON body from a request It is
* composed with a TestXXX to get the correct thing for the extractor
*/
  protected trait JsonBody {
    def body(r: Req): Box[JValue] = r.json
  }

  /**
* a trait that extracts the XML body from a request It is
* composed with a TestXXX to get the correct thing for the extractor
*/
  protected trait XmlBody {
    def body(r: Req): Box[Elem] = r.xml
  }

  /**
* An extractor that tests the request to see if it's a GET and
* if it is, the path and the request are extracted. It can
* be used as:<br/>
* <pre>case "api" :: id :: _ Get req => ...</pre><br/>
* or<br/>
* <pre>case Get("api" :: id :: _, req) => ...</pre><br/> *
*/
  protected object Get {
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.get_?) Some(r.path.partPath -> r) else None
                                
  }

  /**
* An extractor that tests the request to see if it's a POST and
* if it is, the path and the request are extracted. It can
* be used as:<br/>
* <pre>case "api" :: id :: _ Post req => ...</pre><br/>
* or<br/>
* <pre>case Post("api" :: id :: _, req) => ...</pre><br/>
*/
  protected object Post {
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.post_?) Some(r.path.partPath -> r) else None
                                
  }

  /**
* An extractor that tests the request to see if it's a PUT and
* if it is, the path and the request are extracted. It can
* be used as:<br/>
* <pre>case "api" :: id :: _ Put req => ...</pre><br/>
* or<br/>
* <pre>case Put("api" :: id :: _, req) => ...</pre><br/>
*/
  protected object Put {
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.put_?) Some(r.path.partPath -> r) else None
                                
  }

  /**
* An extractor that tests the request to see if it's a DELETE and
* if it is, the path and the request are extracted. It can
* be used as:<br/>
* <pre>case "api" :: id :: _ Delete req => ...</pre><br/>
* or<br/>
* <pre>case Delete("api" :: id :: _, req) => ...</pre><br/>
*/
  protected object Delete {
    def unapply(r: Req): Option[(List[String], Req)] =
      if (r.requestType.delete_?) Some(r.path.partPath -> r) else None
                                
  }

  /**
* A function that chooses JSON or XML based on the request..
* Use with serveType
*/
  implicit def jxSel(req: Req): BoxOrRaw[JsonXmlSelect] =
    if (jsonResponse_?(req)) JsonSelect
    else if (xmlResponse_?(req)) XmlSelect
    else None

  /**
* Serve a given request by determining the request type, computing
* the response and then converting the response to the given
* type (e.g., JSON or XML).<br/><br/>
* @param selection -- a function that determines the response type
* based on the Req.
* @parama pf -- a PartialFunction that converts the request to a
* response type (e.g., a case class that contains the response).
* @param cvt -- a function that converts from the response type
* to a the appropriate LiftResponse based on the selected response
* type.
*/
  protected def serveType[T, SelectType](selection: Req => BoxOrRaw[SelectType])
  (pf: PartialFunction[Req, BoxOrRaw[T]])
  (implicit cvt: PartialFunction[(SelectType, T, Req), LiftResponse]): Unit = {
    serve(new PartialFunction[Req, () => Box[LiftResponse]] {
      def isDefinedAt(r: Req): Boolean =
        selection(r).isDefined && pf.isDefinedAt(r)
   
      def apply(r: Req): () => Box[LiftResponse] =
        () => {
          pf(r).box match {
            case Full(resp) =>
              val selType = selection(r).open_! // Full because pass isDefinedAt
              if (cvt.isDefinedAt((selType, resp, r)))
                  Full(cvt((selType, resp, r)))
              else emptyToResp(ParamFailure("Unabled to convert the message",
                                            Empty, Empty, 500))

            case e: EmptyBox => emptyToResp(e)
          }
        }
    }
    )
  }

  /**
* Serve a request returning either JSON or XML.
*
* @parama pf -- a Partial Function that converts the request into
* an intermediate response.
* @param cvt -- convert the intermediate response to a LiftResponse
* based on the request being for XML or JSON. If T is JsonXmlAble,
* there are built-in converters. Further, you can return auto(thing)
* and that will invoke built-in converters as well. The built-in
* converters use Lift JSON's Extraction.decompose to convert the object
* into JSON and then Xml.toXml() to convert to XML.
*/
  protected def serveJx[T](pf: PartialFunction[Req, BoxOrRaw[T]])
  (implicit cvt: JxCvtPF[T]): Unit =
    serveType(jxSel)(pf)(cvt)

  protected type JxCvtPF[T] =
    PartialFunction[(JsonXmlSelect, T, Req), LiftResponse]

  /**
* Serve a request returning either JSON or XML.
*
* @parama pf -- a Partial Function that converts the request into
* Any (note that the response must be convertable into
* JSON vis Lift JSON Extraction.decompose
*/
  protected def serveJxa(pf: PartialFunction[Req, BoxOrRaw[Any]]): Unit =
    serveType(jxSel)(pf)(new PartialFunction[(JsonXmlSelect,
                                              Any, Req), LiftResponse] {
      def isDefinedAt(p: (JsonXmlSelect, Any, Req)) =
        convertAutoJsonXmlAble.isDefinedAt((p._1, AutoJsonXmlAble(p._2), p._3))
      
      def apply(p: (JsonXmlSelect, Any, Req)) =
        convertAutoJsonXmlAble.apply((p._1, AutoJsonXmlAble(p._2), p._3))
    })

  /**
* Return the implicit Formats instance for JSON conversion
*/
  protected implicit def formats: Formats = net.liftweb.json.DefaultFormats

  /**
* The default way to convert a JsonXmlAble into JSON or XML
*/
  protected implicit lazy val convertJsonXmlAble:
  PartialFunction[(JsonXmlSelect, JsonXmlAble, Req), LiftResponse] = {
    case (JsonSelect, obj, _) => Extraction.decompose(obj)

    case (XmlSelect, obj, _) =>
      Xml.toXml(Extraction.decompose(obj)).toList match {
        case x :: _ => x
        case _ => Text("")
      }
  }

  /**
* The class that wraps anything for auto conversion to JSON or XML
*/
  protected final case class AutoJsonXmlAble(obj: Any)

  /**
* wrap anything for autoconversion to JSON or XML
*/
  protected def auto(in: Any): Box[AutoJsonXmlAble] =
    Full(new AutoJsonXmlAble(in))

  /**
* Wrap a Box of anything for autoconversion to JSON or XML
*/
  protected def auto(in: Box[Any]): Box[AutoJsonXmlAble] =
    in.map(obj => new AutoJsonXmlAble(obj))

  /**
* An implicit conversion that converts AutoJsonXmlAble into
* JSON or XML
*/
  protected implicit lazy val convertAutoJsonXmlAble:
  PartialFunction[(JsonXmlSelect, AutoJsonXmlAble, Req), LiftResponse] = {
    case (JsonSelect, AutoJsonXmlAble(obj), _) =>
      Extraction.decompose(obj)
    case (XmlSelect, AutoJsonXmlAble(obj), _) =>
      Xml.toXml(Extraction.decompose(obj)).toList match {
        case x :: _ => x
        case _ => Text("")
      }
  }

  /**
* The stable identifier for JsonPost. You can use it
* as an extractor.
*/
  protected lazy val JsonPost = new TestPost[JValue] with JsonTest with JsonBody

  /**
* The stable identifier for XmlPost. You can use it
* as an extractor.
*/
  protected lazy val XmlPost = new TestPost[Elem] with XmlTest with XmlBody

  /**
* A trait that defines the TestPut extractor. Is
* the request a PUT, has JSON or XML data in the put body
* and something that expects JSON or XML in the response.
* Subclass this trait to change the behavior
*/
  protected trait TestPut[T] {
    /**
* Test to see if the request is a PUT, has JSON or XML data in the
* body and expecting JSON or XML in the response.
* The path, Data and the Req instance are extracted.
*/
    def unapply(r: Req): Option[(List[String], (T, Req))] =
      if (r.put_? && testResponse_?(r))
        body(r).toOption.map(b => (r.path.partPath -> (b -> r))) else None

    
    def testResponse_?(r: Req): Boolean

    def body(r: Req): Box[T]
  }

  /**
* The stable identifier for JsonPut. You can use it
* as an extractor.
*/
  protected lazy val JsonPut = new TestPut[JValue] with JsonTest with JsonBody

  /**
* The stable identifier for XmlPut. You can use it
* as an extractor.
*/
  protected lazy val XmlPut = new TestPut[Elem] with XmlTest with XmlBody

  /**
* Extract a Pair using the same syntax that you use to make a Pair
*/
  protected object -> {
    def unapply[A, B](s: (A, B)): Option[(A, B)] = Some(s._1 -> s._2)
  }

  @volatile private var _dispatch: List[LiftRules.DispatchPF] = Nil
  
  private lazy val nonDevDispatch = _dispatch.reverse

  private def dispatch: List[LiftRules.DispatchPF] =
    if (Props.devMode) _dispatch.reverse else nonDevDispatch

  /**
* Is the Rest helper defined for a given request
*/
  def isDefinedAt(in: Req) = dispatch.find(_.isDefinedAt(in)).isDefined

  /**
* Apply the Rest helper
*/
  def apply(in: Req): () => Box[LiftResponse] =
    dispatch.find(_.isDefinedAt(in)).get.apply(in)

  /**
* Add request handlers
*/
  protected def serve(handler: PartialFunction[Req, () => Box[LiftResponse]]):
  Unit = _dispatch ::= handler
  
  /**
* Turn T into the return type expected by DispatchPF as long
* as we can convert T to a LiftResponse.
*/
  protected implicit def thingToResp[T](in: T)(implicit c: T => LiftResponse):
  () => Box[LiftResponse] = () => Full(c(in))

  /**
* Turn a Box[T] into the return type expected by
* DispatchPF. Note that this method will return
* messages from Failure() and return codes and messages
* from ParamFailure[Int[(msg, _, _, code)
*/
  protected implicit def boxToResp[T](in: Box[T])
  (implicit c: T => LiftResponse): () => Box[LiftResponse] =
    in match {
      case Full(v) => () => Full(c(v))
      case e: EmptyBox => () => emptyToResp(e)
    }

  protected def emptyToResp(eb: EmptyBox): Box[LiftResponse] =
    eb match {
      case ParamFailure(msg, _, _, code: Int) =>
        Full(InMemoryResponse(msg.getBytes("UTF-8"),
                              ("Content-Type" ->
                               "text/plain; charset=utf-8") ::
                              Nil, Nil, code))
      
      case Failure(msg, _, _) =>
        Full(NotFoundResponse(msg))

      case _ => Empty
    }

  /**
* Turn an Option[T] into the return type expected by
* DispatchPF.
*/
  protected implicit def optionToResp[T](in: Option[T])
  (implicit c: T => LiftResponse): () => Box[LiftResponse] =
    in match {
      case Some(v) => () => Full(c(v))
      case _ => () => Empty
    }

  /**
* Turn a () => Box[T] into the return type expected by
* DispatchPF. Note that this method will return
* messages from Failure() and return codes and messages
* from ParamFailure[Int[(msg, _, _, code)
*/
  protected implicit def boxFuncToResp[T](in: () => Box[T])
  (implicit c: T => LiftResponse): () => Box[LiftResponse] =
    () => {
      in() match {
        case ParamFailure(msg, _, _, code: Int) =>
          Full(InMemoryResponse(msg.getBytes("UTF-8"),
                                ("Content-Type" ->
                                 "text/plain; charset=utf-8") ::
                                Nil, Nil, code))
        
        case Failure(msg, _, _) =>
          Full(NotFoundResponse(msg))
        
        case Full(v) => Full(c(v))
        case _ => Empty
      }
    }
    
  /**
* Turn an Option[T] into the return type expected by
* DispatchPF.
*/
  protected implicit def optionFuncToResp[T](in: () => Option[T])
  (implicit c: T => LiftResponse): () => Box[LiftResponse] =
    () =>
      in() match {
        case Some(v) => Full(c(v))
        case _ => Empty
      }


  /**
* Override this method to create an AppXmlResponse with the
* mime type application/xml rather then text/xml
*/
  protected def createXmlResponse(in: scala.xml.Node): LiftResponse =
    XmlResponse(in)

  /**
* Convert a Node to an XmlResponse
*/
  protected implicit def nodeToResp(in: scala.xml.Node): LiftResponse =
    createXmlResponse(in)

  /**
* Convert a JValue to a LiftResponse
*/
  implicit def jsonToResp(in: JsonAST.JValue): LiftResponse =
    JsonResponse(in)

  /**
* Convert a JsExp to a LiftResponse
*/
  implicit def jsExpToResp(in: js.JsExp): LiftResponse =
    JsonResponse(in)

  /** @return a SuperString with more available methods such as roboSplit or commafy */
  protected implicit def listStringToSuper(in: List[String]):
  SuperListString = new SuperListString(in)

  /** @return a SuperString with more available methods such as roboSplit or commafy */
  protected implicit def stringToSuper(in: String): SuperString = new SuperString(in)

  /**
* Allows you to use >> after a path list
* to handle all the cases where you have a prefix
* for a series of differ suffixes with the same
* path prefix. For example:
* <code>
* serve("foo" / "bar" >> {
* case baz :: Nil Post _ => ...
* case Nil Get _ => ...
* })
* </code>
*/
  protected implicit def listToServeMagic(in: List[String]):
  ListServeMagic = new ListServeMagic(in)

  /**
* Take an original piece of JSON (most probably, JObject
* and replace all the JFields with those in toMerge
*/
  protected def mergeJson(original: JValue, toMerge: JValue): JValue = {
    def replace(lst: List[JField], f: JField): List[JField] =
      f :: lst.filterNot(_.name == f.name)

    original match {
      case JObject(fields) =>
        toMerge match {
          case jf: JField => JObject(replace(fields, jf))
          case JObject(otherFields) =>
            JObject(otherFields.foldLeft(fields)(replace(_, _)))
          case _ => original
        }

      case jf: JField =>
        toMerge match {
          case jfMerge: JField => JObject(replace(List(jf), jfMerge))
          case JObject(otherFields) =>
            JObject(otherFields.foldLeft(List(jf))(replace(_, _)))
          case _ => original
        }
      case _ => original // can't merge
    }
  }
}


/**
* Do some magic to prefix path patterns with a single List
*/
final class ListServeMagic(list: List[String]) {
  val listLen = list.length

  /**
* Used in conjunction with RestHelper.serveJx to
* prefix the pattern matched path. For example:
* <code>
* serveJx[Foo]("foo" / "bar" prefixJx {
* case FindBaz(baz) :: Nil Get _ => baz
* })
* </code>
*/
  def prefixJx[T](pf: PartialFunction[Req, BoxOrRaw[T]]): PartialFunction[Req, BoxOrRaw[T]] =
    new PartialFunction[Req, BoxOrRaw[T]] {
      def isDefinedAt(req: Req): Boolean =
        req.path.partPath.startsWith(list) && {
          pf.isDefinedAt(req.withNewPath(req.path.drop(listLen)))
        }

      def apply(req: Req): BoxOrRaw[T] =
        pf.apply(req.withNewPath(req.path.drop(listLen)))
    }


  /**
* Used in conjunction with RestHelper.serveJx to
* prefix the pattern matched path. For example:
* <code>
* serve("foo" / "bar" prefix {
* case FindBaz(baz) :: Nil GetJson _ => baz: JValue
* case FindBaz(baz) :: Nil GetXml _ => baz: Node
* })
* </code>
*/
  def prefix(pf: PartialFunction[Req, () => Box[LiftResponse]]):
  PartialFunction[Req, () => Box[LiftResponse]] =
    new PartialFunction[Req, () => Box[LiftResponse]] {
      def isDefinedAt(req: Req): Boolean =
        req.path.partPath.startsWith(list) && {
          pf.isDefinedAt(req.withNewPath(req.path.drop(listLen)))
        }

      def apply(req: Req): () => Box[LiftResponse] =
        pf.apply(req.withNewPath(req.path.drop(listLen)))
    }
 
}

/**
* A trait that can be mixed into an class (probably a case class)
* so that the class can be converted automatically into JSON or XML
*/
trait JsonXmlAble

/**
* This trait is part of the ADT that allows the choice between
*/
sealed trait JsonXmlSelect

/**
* The Type for JSON
*/
final case object JsonSelect extends JsonXmlSelect

/**
* The type for XML
*/
final case object XmlSelect extends JsonXmlSelect
Something went wrong with that request. Please try again.