PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-camel/src/main/scala/org/fusesource/scalate/camel/ScalateEndpoint.scala

http://github.com/scalate/scalate
Scala | 117 lines | 71 code | 22 blank | 24 comment | 11 complexity | 601ed98808493920301d01f3402dfaa8 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
 19package camel
 20
 21import java.io._
 22import java.{ util => ju }
 23import org.apache.camel._
 24import org.apache.camel.util.{ ExchangeHelper, ObjectHelper }
 25
 26import org.apache.commons.logging.LogFactory
 27
 28import impl.ProcessorEndpoint
 29import collection.JavaConverters._
 30
 31/**
 32 * @version $Revision : 1.1 $
 33 */
 34@deprecated("because Apache Camel-Scala has become deprecated", since = "1.9.4")
 35class ScalateEndpoint(
 36  component: ScalateComponent,
 37  uri: String,
 38  templateUri: String,
 39  defaultTemplateExtension: String = "ssp") extends ProcessorEndpoint(uri, component) {
 40
 41  val log = LogFactory.getLog(getClass)
 42
 43  val RESOURCE_URI = "CamelScalateResourceUri"
 44  val TEMPLATE = "CamelScalateTemplate"
 45
 46  override def isSingleton = true
 47
 48  override def getExchangePattern = ExchangePattern.InOut
 49
 50  override def createEndpointUri = "scalate:" + templateUri
 51
 52  def findOrCreateEndpoint(uri: String, newResourceUri: String): ScalateEndpoint = {
 53    val newUri = "scalate:" + component.templateEngine.resourceLoader.resolve(templateUri, newResourceUri)
 54    debug("Getting endpoint with URI: " + newUri)
 55    getCamelContext.getEndpoint(newUri, classOf[ScalateEndpoint])
 56  }
 57
 58  override def onExchange(exchange: Exchange): Unit = {
 59    ObjectHelper.notNull(templateUri, "resourceUri")
 60    val templateEngine = component.templateEngine
 61    val newResourceUri = exchange.getIn().getHeader(RESOURCE_URI, classOf[String])
 62    if (newResourceUri != null) {
 63      exchange.getIn().removeHeader(RESOURCE_URI)
 64
 65      debug(RESOURCE_URI + " set to " + newResourceUri + " creating new endpoint to handle exchange")
 66
 67      val newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri)
 68      newEndpoint.onExchange(exchange)
 69    } else {
 70
 71      val content = exchange.getIn().getHeader(TEMPLATE, classOf[String])
 72      val template = if (content != null) {
 73        // use content from header
 74        debug("Scalate content read from header " + TEMPLATE + " for endpoint " + getEndpointUri())
 75
 76        // remove the header to avoid it being propagated in the routing
 77        exchange.getIn().removeHeader(TEMPLATE)
 78
 79        templateEngine.compileText(defaultTemplateExtension, content)
 80      } else {
 81        templateEngine.load(templateUri)
 82      }
 83
 84      //val logTag = getClass().getName()
 85      val buffer = new StringWriter()
 86      val context = new DefaultRenderContext(uri, templateEngine, new PrintWriter(buffer))
 87
 88      val variableMap = ExchangeHelper.createVariableMap(exchange)
 89      for ((key, value) <- variableMap.asScala) {
 90        debug("setting " + key + " = " + value)
 91        context.attributes(key) = value
 92      }
 93      context.attributes("context") = context
 94      template.render(context)
 95
 96      val out = if (exchange.getPattern.isOutCapable) exchange.getOut() else exchange.getIn
 97      val response = buffer.toString()
 98
 99      debug("Eval of " + this + " = " + response)
100      out.setBody(response)
101
102      // now lets output the headers to the exchange
103      variableMap.get("headers") match {
104        case map: ju.Map[_, _] =>
105          for ((key, value) <- map.asInstanceOf[ju.Map[String, AnyRef]].asScala) {
106            out.setHeader(key, value)
107          }
108        case _ =>
109      }
110    }
111  }
112
113  protected def debug(message: => String): Unit = if (log.isDebugEnabled) {
114    val text: String = message
115    log.debug(text)
116  }
117}