Introduce CompositeScalatraFilter to merge controllers to one filter

This commit is contained in:
Naoki Takezoe
2017-11-17 17:44:35 +09:00
parent cd243f910a
commit 581bcb3dc8
2 changed files with 63 additions and 13 deletions

View File

@@ -32,20 +32,25 @@ class ScalatraBootstrap extends LifeCycle with SystemSettingsService {
context.addFilter("pluginControllerFilter", new PluginControllerFilter)
context.getFilterRegistration("pluginControllerFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
context.mount(new IndexController, "/")
context.mount(new ApiController, "/api/v3")
context.mount(new FileUploadController, "/upload")
context.mount(new SystemSettingsController, "/admin")
context.mount(new DashboardController, "/*")
context.mount(new AccountController, "/*")
context.mount(new RepositoryViewerController, "/*")
context.mount(new WikiController, "/*")
context.mount(new LabelsController, "/*")
context.mount(new PrioritiesController, "/*")
context.mount(new MilestonesController, "/*")
context.mount(new IssuesController, "/*")
context.mount(new PullRequestsController, "/*")
context.mount(new RepositorySettingsController, "/*")
val filter = new CompositeScalatraFilter()
filter.mount(new IndexController)
filter.mount(new ApiController)
filter.mount(new SystemSettingsController)
filter.mount(new DashboardController)
filter.mount(new AccountController)
filter.mount(new RepositoryViewerController)
filter.mount(new WikiController)
filter.mount(new LabelsController)
filter.mount(new PrioritiesController)
filter.mount(new MilestonesController)
filter.mount(new IssuesController)
filter.mount(new PullRequestsController)
filter.mount(new RepositorySettingsController)
context.addFilter("compositeScalatraFilter", filter)
context.getFilterRegistration("compositeScalatraFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
// Create GITBUCKET_HOME directory if it does not exist
val dir = new java.io.File(Directory.GitBucketHome)

View File

@@ -0,0 +1,45 @@
package gitbucket.core.controller
import javax.servlet._
import org.scalatra.ScalatraFilter
import scala.collection.mutable.ListBuffer
class CompositeScalatraFilter extends Filter {
private val filters = new ListBuffer[ScalatraFilter]()
def mount(filter: ScalatraFilter): Unit = {
filters += filter
}
override def init(filterConfig: FilterConfig): Unit = {
filters.foreach(_.init(filterConfig))
}
override def destroy(): Unit = {
filters.foreach(_.destroy())
}
override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = {
filters.foreach { filter =>
val mockChain = new MockFilterChain()
filter.doFilter(request, response, mockChain)
if(mockChain.continue == false){
return ()
}
}
chain.doFilter(request, response)
}
class MockFilterChain extends FilterChain {
var continue: Boolean = false
override def doFilter(request: ServletRequest, response: ServletResponse): Unit = {
continue = true
}
}
}