Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some helpful REST extensions

  • Loading branch information...
commit 2018263b528fb8dbbe57693a544503d9ee9a3aff 1 parent 85ce62c
@dpp dpp authored
View
63 web/webkit/src/main/scala/net/liftweb/http/Req.scala
@@ -713,6 +713,61 @@ class Req(val path: ParsePath,
_addlParams)
/**
+ * Build a new Req, except it has a different path.
+ * Useful for creating Reqs with sub-paths
+ */
+ def withNewPath(newPath: ParsePath): Req = {
+ val outer = this
+
+ new Req(newPath,
+ contextPath,
+ requestType,
+ contentType,
+ request,
+ nanoStart,
+ nanoEnd,
+ _stateless_?,
+ paramCalculator,
+ addlParams) {
+ override lazy val json: Box[JsonAST.JValue] = outer.json
+
+ override lazy val xml: Box[Elem] = outer.xml
+
+ override lazy val location: Box[Loc[_]] = outer.location
+
+ override lazy val buildMenu: CompleteMenu = outer.buildMenu
+
+ /**
+ * the accept header
+ */
+ override lazy val accepts: Box[String] = outer.accepts
+
+ /**
+ * What is the content type in order of preference by the requestor
+ * calculated via the Accept header
+ */
+ override lazy val weightedAccept: List[ContentType] =
+ outer.weightedAccept
+
+ /**
+ * Returns true if the request accepts XML
+ */
+ override lazy val acceptsXml_? = outer.acceptsXml_?
+
+ /**
+ * Returns true if the request accepts JSON
+ */
+ override lazy val acceptsJson_? = outer.acceptsJson_?
+
+ /**
+ * Returns true if the request accepts JavaScript
+ */
+ override lazy val acceptsJavaScript_? =
+ outer.acceptsJavaScript_?
+ }
+ }
+
+ /**
* Should the request be treated as stateless (no session created for it)?
*/
lazy val stateless_? = {
@@ -823,10 +878,10 @@ class Req(val path: ParsePath,
*/
def rawInputStream: Box[InputStream] = _body.flatMap(_.stream)
- private lazy val ParamCalcInfo(_paramNames: List[String],
- __params: Map[String, List[String]],
- _uploadedFiles: List[FileParamHolder],
- _body: Box[BodyOrInputStream]) = {
+ private lazy val ParamCalcInfo(_paramNames /*: List[String]*/,
+ __params /*: Map[String, List[String]]*/,
+ _uploadedFiles /*: List[FileParamHolder]*/,
+ _body /*: Box[BodyOrInputStream]*/) = {
val ret = paramCalculator()
ret
}
View
58 web/webkit/src/main/scala/net/liftweb/http/rest/RestHelper.scala
@@ -1,5 +1,5 @@
/*
- * Copyright 2010 WorldWide Conferencing, LLC
+ * 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.
@@ -14,14 +14,15 @@
* limitations under the License.
*/
-package net.liftweb {
-package http {
-package rest {
+package net.liftweb
+package http
+package rest
-import net.liftweb.json._
-import net.liftweb.common._
-import net.liftweb.util.Props
+import net.liftweb._
+import json._
+import common._
+import util._
import scala.xml.{Elem, Node, Text}
/**
@@ -583,6 +584,46 @@ trait RestHelper extends LiftRules.DispatchPF {
*/
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)
+}
+
+
+final class ListServeMagic(list: List[String]) {
+ val listLen = list.length
+
+ def >>(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)))
+ }
+
}
/**
@@ -606,6 +647,3 @@ final case object JsonSelect extends JsonXmlSelect
*/
final case object XmlSelect extends JsonXmlSelect
-}
-}
-}
Please sign in to comment.
Something went wrong with that request. Please try again.