mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 06:55:54 +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 javax.servlet.http.{HttpServletResponse, HttpServletRequest}
|
||||||
import org.apache.commons.io.IOUtils
|
import org.apache.commons.io.IOUtils
|
||||||
import twirl.api.Html
|
import twirl.api.Html
|
||||||
import service.SystemSettingsService
|
import service.{AccountService, RepositoryService, SystemSettingsService}
|
||||||
import model.Account
|
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) = {}
|
def init(config: FilterConfig) = {}
|
||||||
|
|
||||||
@@ -17,17 +17,53 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService {
|
|||||||
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
|
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
|
||||||
(req, res) match {
|
(req, res) match {
|
||||||
case (request: HttpServletRequest, response: HttpServletResponse) => {
|
case (request: HttpServletRequest, response: HttpServletResponse) => {
|
||||||
val path = req.asInstanceOf[HttpServletRequest].getRequestURI
|
// TODO Why withTransaction is needed?
|
||||||
val action = plugin.PluginSystem.globalActions.find(_.path == path)
|
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){
|
private def processGlobalAction(path: String, request: HttpServletRequest, response: HttpServletResponse): Boolean = {
|
||||||
val result = action.get.function(request, response)
|
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 {
|
result match {
|
||||||
case x: String => {
|
case x: String => {
|
||||||
response.setContentType("text/html; charset=UTF-8")
|
response.setContentType("text/html; charset=UTF-8")
|
||||||
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||||
val context = app.Context(loadSystemSettings(), Option(loginAccount), request)
|
implicit val context = app.Context(loadSystemSettings(), Option(loginAccount), request)
|
||||||
val html = _root_.html.main("GitBucket", None)(Html(x))(context)
|
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)
|
IOUtils.write(html.toString.getBytes("UTF-8"), response.getOutputStream)
|
||||||
}
|
}
|
||||||
case x => {
|
case x => {
|
||||||
@@ -36,12 +72,10 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
true
|
||||||
chain.doFilter(req, res)
|
|
||||||
}
|
}
|
||||||
}
|
} getOrElse false
|
||||||
}
|
} else false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user