diff --git a/src/main/scala/plugin/PluginRegistory.scala b/src/main/scala/plugin/PluginRegistory.scala index 5d94010..186b38a 100644 --- a/src/main/scala/plugin/PluginRegistory.scala +++ b/src/main/scala/plugin/PluginRegistory.scala @@ -11,6 +11,7 @@ import util.{Version, Versions} import scala.collection.mutable.ListBuffer +import app.Context class PluginRegistry { @@ -25,25 +26,25 @@ 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 @@ 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 ) } diff --git a/src/main/scala/servlet/PluginActionFilter.scala b/src/main/scala/servlet/PluginActionFilter.scala index d434aee..d5b72c7 100644 --- a/src/main/scala/servlet/PluginActionFilter.scala +++ b/src/main/scala/servlet/PluginActionFilter.scala @@ -3,10 +3,14 @@ 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 @@ 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")