All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yoojia.web.VelocityTemplates.kt Maven / Gradle / Ivy

There is a newer version: 3.0.5-alpha
Show newest version
package com.github.yoojia.web

import com.github.yoojia.web.kernel.Config
import com.github.yoojia.web.kernel.Context
import com.github.yoojia.web.kernel.DispatchChain
import com.github.yoojia.web.kernel.Module
import com.github.yoojia.web.supports.Logger
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.apache.velocity.app.VelocityEngine
import java.io.StringWriter
import java.util.*

/**
 * @author Yoojia Chen ([email protected])
 * @since 2.0
 */
class VelocityTemplates : Module {

    private val mEngine = VelocityEngine()

    override fun onCreated(context: Context, config: Config) {
        val path = context.resolvePath("/templates")
        val conf = Properties()
        conf.setProperty("resource.loader", "file")
        conf.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader")
        conf.setProperty("file.resource.loader.path", path.toString())
        conf.setProperty("file.resource.loader.cache", "true")
        conf.setProperty("file.resource.loader.modificationCheckInterval", "2")
        conf.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8")
        conf.setProperty(Velocity.INPUT_ENCODING, "UTF-8")
        conf.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8")
        mEngine.init(conf)
    }

    override fun onDestroy() {
        // NOP
    }

    @Throws(Exception::class)
    override fun process(request: Request, response: Response, dispatch: DispatchChain) {
        val name = response.args[Response.TEMPLATE_NAME]
        if(name != null && name.isNotEmpty()) {
            Logger.vv("Template-Module-Processing: ${request.path}, template: $name")
            if ( ! mEngine.resourceExists(name)) {
                throw RuntimeException("Template resource($name) not exists !");
            }
            val text = StringWriter()
            mEngine.getTemplate(name).merge(VelocityContext(request.params()), text)
            response.sendHtml(text.toString())
        }
        dispatch.process(request, response, dispatch)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy