PageRenderTime 36ms CodeModel.GetById 7ms app.highlight 19ms RepoModel.GetById 5ms app.codeStats 0ms

/scalate-spring-mvc/src/main/scala/org/fusesource/scalate/spring/view/ScalateView.scala

http://github.com/scalate/scalate
Scala | 131 lines | 88 code | 26 blank | 17 comment | 2 complexity | d3f888f8263f0a775cfef02465b1716c MD5 | raw file
  1/**
  2 * Copyright (C) 2009-2011 the original author or authors.
  3 * See the notice.md file distributed with this work for additional
  4 * information regarding copyright ownership.
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.fusesource.scalate.spring.view
 19
 20import _root_.java.util.Locale
 21import _root_.javax.servlet.http.HttpServletRequest
 22import _root_.javax.servlet.http.HttpServletResponse
 23import _root_.org.fusesource.scalate.RenderContext
 24import _root_.org.fusesource.scalate.servlet.ServletRenderContext
 25import _root_.org.fusesource.scalate.servlet.ServletTemplateEngine
 26import _root_.scala.collection.JavaConverters._
 27import _root_.org.fusesource.scalate.TemplateException
 28import _root_.org.springframework.web.servlet.view.{ AbstractView, AbstractTemplateView }
 29import _root_.org.slf4j.LoggerFactory
 30import org.fusesource.scalate.util.ResourceNotFoundException
 31
 32trait ScalateRenderStrategy {
 33
 34  protected val log = LoggerFactory.getLogger(getClass)
 35
 36  def render(context: ServletRenderContext, model: Map[String, Any]): Unit
 37
 38}
 39
 40trait LayoutScalateRenderStrategy
 41  extends AbstractTemplateView
 42  with ScalateRenderStrategy {
 43
 44  def templateEngine: ServletTemplateEngine
 45
 46  def render(context: ServletRenderContext, model: Map[String, Any]): Unit = {
 47    log.debug("Rendering view with name '" + getUrl + "' with model " + model)
 48    for ((key, value) <- model) {
 49      context.attributes(key) = value
 50    }
 51    templateEngine.layout(getUrl, context)
 52  }
 53}
 54
 55trait DefaultScalateRenderStrategy
 56  extends AbstractTemplateView
 57  with ScalateRenderStrategy {
 58
 59  override def render(context: ServletRenderContext, model: Map[String, Any]): Unit = {
 60    log.debug("Rendering view with name '" + getUrl + "' with model " + model)
 61    context.render(getUrl, model)
 62  }
 63}
 64
 65trait ViewScalateRenderStrategy
 66  extends ScalateRenderStrategy {
 67
 68  override def render(context: ServletRenderContext, model: Map[String, Any]): Unit = {
 69    log.debug("Rendering with model " + model)
 70    val it = model.get("it")
 71    if (it.isEmpty)
 72      throw new TemplateException("No 'it' model object specified.  Cannot render request")
 73    context.view(it.get.asInstanceOf[AnyRef])
 74  }
 75}
 76
 77trait AbstractScalateView extends AbstractView {
 78
 79  var templateEngine: ServletTemplateEngine = _
 80
 81  def checkResource(locale: Locale): Boolean
 82
 83}
 84
 85class ScalateUrlView
 86  extends AbstractTemplateView
 87  with AbstractScalateView
 88  with LayoutScalateRenderStrategy {
 89
 90  override def renderMergedTemplateModel(
 91    model: java.util.Map[String, Object],
 92    request: HttpServletRequest,
 93    response: HttpServletResponse): Unit = {
 94
 95    val context = new ServletRenderContext(templateEngine, request, response, getServletContext)
 96    RenderContext.using(context) {
 97      render(context, model.asInstanceOf[java.util.Map[String, Any]].asScala.toMap)
 98    }
 99  }
100
101  override def checkResource(locale: Locale): Boolean = try {
102    log.debug("Checking for resource " + getUrl)
103    templateEngine.load(getUrl)
104    true
105  } catch {
106    case e: ResourceNotFoundException => {
107      log.info("Could not find resource " + getUrl)
108      false
109    }
110  }
111
112}
113
114class ScalateView
115  extends AbstractScalateView
116  with ViewScalateRenderStrategy {
117
118  override def checkResource(locale: Locale) = true
119
120  override def renderMergedOutputModel(
121    model: java.util.Map[String, Object],
122    request: HttpServletRequest,
123    response: HttpServletResponse): Unit = {
124
125    val context = new ServletRenderContext(templateEngine, request, response, getServletContext)
126    RenderContext.using(context) {
127      render(context, model.asInstanceOf[java.util.Map[String, Any]].asScala.toMap)
128    }
129  }
130
131}