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