diff --git a/src/main/scala/servlet/PluginActionInvokeFilter.scala b/src/main/scala/servlet/PluginActionInvokeFilter.scala index a4cc48e..90d391c 100644 --- a/src/main/scala/servlet/PluginActionInvokeFilter.scala +++ b/src/main/scala/servlet/PluginActionInvokeFilter.scala @@ -4,11 +4,11 @@ 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 @@ 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 @@ } } - } else { - chain.doFilter(req, res) + true } - } - } + } getOrElse false + } else false } - }