mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 06:55:54 +01:00
Plug-in action to be Scalatra controller
This commit is contained in:
@@ -162,12 +162,13 @@ object AutoUpdate {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update database schema automatically in the context initializing.
|
||||
* Initialize GitBucket system.
|
||||
* Update database schema and load plug-ins automatically in the context initializing.
|
||||
*/
|
||||
class AutoUpdateListener extends ServletContextListener {
|
||||
class InitializeListener extends ServletContextListener {
|
||||
import AutoUpdate._
|
||||
|
||||
private val logger = LoggerFactory.getLogger(classOf[AutoUpdateListener])
|
||||
private val logger = LoggerFactory.getLogger(classOf[InitializeListener])
|
||||
|
||||
override def contextInitialized(event: ServletContextEvent): Unit = {
|
||||
val dataDir = event.getServletContext.getInitParameter("gitbucket.home")
|
||||
@@ -184,14 +185,14 @@ class AutoUpdateListener extends ServletContextListener {
|
||||
}
|
||||
// Load plugins
|
||||
logger.debug("Initialize plugins")
|
||||
PluginRegistry.initialize(conn)
|
||||
PluginRegistry.initialize(event.getServletContext, conn)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def contextDestroyed(sce: ServletContextEvent): Unit = {
|
||||
def contextDestroyed(event: ServletContextEvent): Unit = {
|
||||
// Shutdown plugins
|
||||
PluginRegistry.shutdown()
|
||||
PluginRegistry.shutdown(event.getServletContext)
|
||||
}
|
||||
|
||||
private def getConnection(): Connection =
|
||||
@@ -1,60 +0,0 @@
|
||||
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
|
||||
import plugin.Results._
|
||||
import plugin.Sessions._
|
||||
|
||||
class PluginActionFilter extends Filter with SystemSettingsService {
|
||||
|
||||
def init(config: FilterConfig) = {}
|
||||
|
||||
def destroy(): Unit = {}
|
||||
|
||||
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = (req, res) match {
|
||||
case (req: HttpServletRequest, res: HttpServletResponse) => {
|
||||
val method = req.getMethod.toLowerCase
|
||||
val path = req.getRequestURI.substring(req.getContextPath.length)
|
||||
val registry = PluginRegistry()
|
||||
|
||||
registry.getGlobalAction(method, path).map { action =>
|
||||
// Create Context
|
||||
val loginAccount = req.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||
implicit val context = Context(loadSystemSettings(), Option(loginAccount), req)
|
||||
sessions.set(Database.getSession(req))
|
||||
try {
|
||||
// Invoke global action
|
||||
action(req, res, context) match {
|
||||
// TODO to be type classes?
|
||||
case x: String =>
|
||||
res.setContentType("text/plain; charset=UTF-8")
|
||||
res.getWriter.write(x)
|
||||
res.getWriter.flush()
|
||||
case x: Html =>
|
||||
res.setContentType("text/html; charset=UTF-8")
|
||||
// TODO title of plugin action
|
||||
res.getWriter.write(html.main("TODO")(x).body)
|
||||
res.getWriter.flush()
|
||||
case Redirect(x) =>
|
||||
res.sendRedirect(x)
|
||||
case Fragment(x) =>
|
||||
res.getWriter.write(x.body)
|
||||
res.getWriter.flush()
|
||||
}
|
||||
} finally {
|
||||
sessions.remove()
|
||||
}
|
||||
}.getOrElse {
|
||||
chain.doFilter(req, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user