Give Context to plugin actions

This commit is contained in:
Naoki Takezoe
2015-02-08 23:09:30 +09:00
parent 22d12d0488
commit fb6bb12c52
2 changed files with 19 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import util.JDBCUtil._
import util.{Version, Versions}
import scala.collection.mutable.ListBuffer
import app.Context
class PluginRegistry {
@@ -25,25 +26,25 @@ class PluginRegistry {
def getPlugins(): List[PluginInfo] = plugins.toList
def addGlobalAction(method: String, path: String)(f: (HttpServletRequest, HttpServletResponse) => Any): Unit = {
def addGlobalAction(method: String, path: String)(f: (HttpServletRequest, HttpServletResponse, Context) => Any): Unit = {
globalActions += GlobalAction(method.toLowerCase, path, f)
}
//def getGlobalActions(): List[GlobalAction] = globalActions.toList
def getGlobalAction(method: String, path: String): Option[(HttpServletRequest, HttpServletResponse) => Any] = {
def getGlobalAction(method: String, path: String): Option[(HttpServletRequest, HttpServletResponse, Context) => Any] = {
globalActions.find { globalAction =>
globalAction.method == method.toLowerCase && path.matches(globalAction.path)
}.map(_.function)
}
def addRepositoryAction(method: String, path: String)(f: (HttpServletRequest, HttpServletResponse, RepositoryInfo) => Any): Unit = {
def addRepositoryAction(method: String, path: String)(f: (HttpServletRequest, HttpServletResponse, Context, RepositoryInfo) => Any): Unit = {
repositoryActions += RepositoryAction(method.toLowerCase, path, f)
}
//def getRepositoryActions(): List[RepositoryAction] = repositoryActions.toList
def getRepositoryAction(method: String, path: String): Option[(HttpServletRequest, HttpServletResponse, RepositoryInfo) => Any] = {
def getRepositoryAction(method: String, path: String): Option[(HttpServletRequest, HttpServletResponse, Context, RepositoryInfo) => Any] = {
// TODO
null
}
@@ -61,13 +62,13 @@ class PluginRegistry {
private case class GlobalAction(
method: String,
path: String,
function: (HttpServletRequest, HttpServletResponse) => Any
function: (HttpServletRequest, HttpServletResponse, Context) => Any
)
private case class RepositoryAction(
method: String,
path: String,
function: (HttpServletRequest, HttpServletResponse, RepositoryInfo) => Any
function: (HttpServletRequest, HttpServletResponse, Context, RepositoryInfo) => Any
)
}

View File

@@ -3,10 +3,14 @@ package servlet
import javax.servlet._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import model.Account
import play.twirl.api.Html
import plugin.PluginRegistry
import service.SystemSettingsService
import util.Keys
import app.Context
class PluginActionFilter extends Filter {
class PluginActionFilter extends Filter with SystemSettingsService {
def init(config: FilterConfig) = {}
@@ -17,8 +21,14 @@ class PluginActionFilter extends Filter {
val method = req.getMethod.toLowerCase
val path = req.getRequestURI.substring(req.getContextPath.length)
val registry = PluginRegistry()
registry.getGlobalAction(method, path).map { action =>
action(req, res) match {
// Create Context
val loginAccount = req.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
val context = Context(loadSystemSettings(), Option(loginAccount), req)
// Invoke global action
action(req, res, context) match {
// TODO to be type classes?
case x: String =>
res.setContentType("text/plain; charset=UTF-8")