Fix plugin controller handling

This commit is contained in:
Naoki Takezoe
2017-07-13 01:52:26 +09:00
parent db88458a14
commit 5641fee39a
2 changed files with 13 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package gitbucket.core.servlet
import javax.servlet._
import javax.servlet.http.HttpServletRequest
import gitbucket.core.controller.ControllerBase
import gitbucket.core.plugin.PluginRegistry
class PluginControllerFilter extends Filter {
@@ -20,18 +21,25 @@ class PluginControllerFilter extends Filter {
}
override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = {
val controller = PluginRegistry().getControllers().find { case (_, path) =>
val controller = PluginRegistry().getControllers().filter { case (_, path) =>
val requestUri = request.asInstanceOf[HttpServletRequest].getRequestURI
path.endsWith("/*") && requestUri.startsWith(path.replaceFirst("/\\*$", "/"))
val start = path.replaceFirst("/\\*$", "/")
path.endsWith("/*") && (requestUri + "/").startsWith(start)
}
controller.map { case (controller, _) =>
val filterChainWrapper = controller.foldLeft(chain){ case (chain, (controller, _)) =>
new FilterChainWrapper(controller, chain)
}
filterChainWrapper.doFilter(request, response)
}
class FilterChainWrapper(controller: ControllerBase, chain: FilterChain) extends FilterChain {
override def doFilter(request: ServletRequest, response: ServletResponse): Unit = {
if(controller.config == null){
controller.init(filterConfig)
}
controller.doFilter(request, response, chain)
}.getOrElse{
chain.doFilter(request, response)
}
}
}