Advertisement
Guest User

ScalateView.scala

a guest
Feb 27th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. /*
  2. * Copyright 2010-2011 WorldWide Conferencing, LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package code
  18. package lib
  19.  
  20. import xml.NodeSeq
  21.  
  22. import net.liftweb.common._
  23. import net.liftweb.util._
  24. import net.liftweb.http._
  25.  
  26.  
  27. /**
  28. * A {@link LiftView} which uses a <a href="http://scalate.fusesource.org/">Scalate</a>
  29. * template engine to resolve a URI and render it as markup
  30. */
  31. class ScalateView(engine: LiftTemplateEngine = new LiftTemplateEngine()) extends LiftView with Logger {
  32.  
  33. /**
  34. * Registers this view with Lift's dispatcher
  35. */
  36. def register: Unit = {
  37. val scalateView: ScalateView = this
  38.  
  39. // TODO no idea why viewDispatch doesn't work, so lets just plugin the dispatcher instead
  40. LiftRules.dispatch.prepend(NamedPF("Scalate Dispatch") {
  41. case Req(path, ext, GetRequest) if (scalateView.canRender(path, ext)) => scalateView.render(path, ext)
  42. })
  43.  
  44. // TODO view dispatch doesn't seem to do anything....
  45. /*
  46. LiftRules.viewDispatch.prepend(NamedPF("Scalate View") {
  47. case Req(path, ext, GetRequest) =>
  48. info("scalate viewDispatch Path: " + path + " ext: " + ext)
  49. Right(scalateView)
  50. })
  51. */
  52. }
  53.  
  54.  
  55. def dispatch: PartialFunction[String, () => Box[NodeSeq]] = {
  56. case v if (canLoad(v)) =>
  57. () => Full(engine.layoutAsNodes(v))
  58. }
  59.  
  60.  
  61. def canRender(path: List[String], ext: String): Boolean = {
  62. debug("=== attempting to find: " + path + " ext: '" + ext + "'")
  63.  
  64. if (ext == "") {
  65. canLoad(createUri(path, "scaml")) || canLoad(createUri(path, "ssp")) || canLoad(createUri(path, "jade"))
  66. }
  67. else {
  68. val uri = createUri(path, ext)
  69. (uri.endsWith(".ssp") || uri.endsWith(".scaml") || uri.endsWith(".jade")) && canLoad(uri)
  70. }
  71. }
  72.  
  73.  
  74. def render(path: List[String], ext: String): () => Box[LiftResponse] = {
  75. debug("attempting to render: " + path + " extension: " + ext)
  76.  
  77. () => {
  78. val uri: String = if (ext != "") createUri(path, ext) else {
  79. List("scaml", "ssp", "jade").map(createUri(path, _)).find(engine.canLoad(_)).get
  80. }
  81. Full(TextResponse(engine.layout(uri)))
  82. }
  83. }
  84.  
  85.  
  86. protected def createUri(path: List[String], ext: String): String = path.mkString("/") +
  87. (if (ext.length > 0) "." + ext else "")
  88.  
  89. protected def canLoad(v: String): Boolean = {
  90. engine.canLoad(v)
  91. }
  92.  
  93.  
  94. case class TextResponse(text: String, headers: List[(String, String)] = Nil, code: Int = 200, contentType: String = "text/html; charset=utf-8") extends LiftResponse {
  95. def toResponse = {
  96. val bytes = text.getBytes("UTF-8")
  97. InMemoryResponse(bytes, ("Content-Length", bytes.length.toString) :: ("Content-Type", contentType) :: headers, Nil, code)
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement