mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 14:35:52 +01:00
(refs #32)Implementing repository action processing
This commit is contained in:
@@ -4,11 +4,11 @@ import javax.servlet._
|
||||
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
|
||||
import org.apache.commons.io.IOUtils
|
||||
import twirl.api.Html
|
||||
import service.SystemSettingsService
|
||||
import service.{AccountService, RepositoryService, SystemSettingsService}
|
||||
import model.Account
|
||||
import util.Keys
|
||||
import util.{JGitUtil, Keys}
|
||||
|
||||
class PluginActionInvokeFilter extends Filter with SystemSettingsService {
|
||||
class PluginActionInvokeFilter extends Filter with SystemSettingsService with RepositoryService with AccountService {
|
||||
|
||||
def init(config: FilterConfig) = {}
|
||||
|
||||
@@ -17,17 +17,53 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService {
|
||||
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
|
||||
(req, res) match {
|
||||
case (request: HttpServletRequest, response: HttpServletResponse) => {
|
||||
val path = req.asInstanceOf[HttpServletRequest].getRequestURI
|
||||
val action = plugin.PluginSystem.globalActions.find(_.path == path)
|
||||
// TODO Why withTransaction is needed?
|
||||
Database(req.getServletContext) withTransaction {
|
||||
val path = req.asInstanceOf[HttpServletRequest].getRequestURI
|
||||
if(!processGlobalAction(path, request, response) && !processRepositoryAction(path, request, response)){
|
||||
chain.doFilter(req, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(action.isDefined){
|
||||
val result = action.get.function(request, response)
|
||||
private def processGlobalAction(path: String, request: HttpServletRequest, response: HttpServletResponse): Boolean = {
|
||||
plugin.PluginSystem.globalActions.find(_.path == path).map { action =>
|
||||
val result = action.function(request, response)
|
||||
result match {
|
||||
case x: String => {
|
||||
response.setContentType("text/html; charset=UTF-8")
|
||||
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||
implicit val context = app.Context(loadSystemSettings(), Option(loginAccount), request)
|
||||
val html = _root_.html.main("GitBucket", None)(Html(x))
|
||||
IOUtils.write(html.toString.getBytes("UTF-8"), response.getOutputStream)
|
||||
}
|
||||
case x => {
|
||||
// TODO returns as JSON?
|
||||
response.setContentType("application/json; charset=UTF-8")
|
||||
|
||||
}
|
||||
}
|
||||
true
|
||||
} getOrElse false
|
||||
}
|
||||
|
||||
private def processRepositoryAction(path: String, request: HttpServletRequest, response: HttpServletResponse): Boolean = {
|
||||
val elements = path.split("/")
|
||||
if(elements.length > 3){
|
||||
val owner = elements(1)
|
||||
val name = elements(2)
|
||||
val remain = elements.drop(3).mkString("/", "/", "")
|
||||
getRepository(owner, name, "").flatMap { repository => // TODO fill baseUrl
|
||||
plugin.PluginSystem.repositoryActions.find(_.path == remain).map { action =>
|
||||
val result = action.function(request, response)
|
||||
result match {
|
||||
case x: String => {
|
||||
response.setContentType("text/html; charset=UTF-8")
|
||||
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||
val context = app.Context(loadSystemSettings(), Option(loginAccount), request)
|
||||
val html = _root_.html.main("GitBucket", None)(Html(x))(context)
|
||||
implicit val context = app.Context(loadSystemSettings(), Option(loginAccount), request)
|
||||
val html = _root_.html.main("GitBucket", None)(_root_.html.menu("", repository)(Html(x))) // TODO specify active side menu
|
||||
IOUtils.write(html.toString.getBytes("UTF-8"), response.getOutputStream)
|
||||
}
|
||||
case x => {
|
||||
@@ -36,12 +72,10 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService {
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
chain.doFilter(req, res)
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
} getOrElse false
|
||||
} else false
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user