(refs #115)ServletContext is passed to Command

This commit is contained in:
takezoe
2014-03-08 14:48:58 +09:00
parent 07a108760c
commit 9e2c66c341
3 changed files with 7 additions and 13 deletions

View File

@@ -64,7 +64,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
defining(request.paths){ paths =>
val owner = paths(1)
val repository = paths(2).replaceFirst("\\.git$", "")
val baseURL = request.getRequestURL.toString.replaceFirst("/git/.*", "")
val baseURL = request.getRequestURL.toString.replaceFirst("/git/.*", "") // TODO Use base URL in SystemSettings
logger.debug("repository:" + owner + "/" + repository)
logger.debug("baseURL:" + baseURL)

View File

@@ -11,6 +11,7 @@ import org.apache.sshd.server.command.UnknownCommand
import servlet.{Database, CommitLogHook}
import service.SystemSettingsService
import org.eclipse.jgit.errors.RepositoryNotFoundException
import javax.servlet.ServletContext
object GitCommand {
@@ -94,7 +95,7 @@ class GitUploadPack(override val command: String) extends GitCommand(command: St
}
class GitReceivePack(override val command: String) extends GitCommand(command: String) with SystemSettingsService {
class GitReceivePack(context: ServletContext, override val command: String) extends GitCommand(command: String) with SystemSettingsService {
// TODO Correct this info. where i get base url?
val BaseURL: String = loadSystemSettings().baseUrl.getOrElse("http://localhost:8080")
@@ -104,7 +105,7 @@ class GitReceivePack(override val command: String) extends GitCommand(command: S
val repository = git.getRepository
val receive = new ReceivePack(repository)
receive.setPostReceiveHook(new CommitLogHook(owner, repositoryName, user, BaseURL))
Database(SshServer.getServletContext) withTransaction {
Database(context) withTransaction {
receive.receive(in, out, err)
}
}
@@ -112,14 +113,14 @@ class GitReceivePack(override val command: String) extends GitCommand(command: S
}
class GitCommandFactory extends CommandFactory {
class GitCommandFactory(context: ServletContext) extends CommandFactory {
private val logger = LoggerFactory.getLogger(classOf[GitCommandFactory])
override def createCommand(command: String): Command = {
logger.debug(s"command: $command")
command match {
case GitCommand.CommandRegex("upload", owner, repoName) => new GitUploadPack(command)
case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(command)
case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(context, command)
case _ => new UnknownCommand(command)
}
}

View File

@@ -15,20 +15,15 @@ object SshServer {
private val server = org.apache.sshd.SshServer.setUpDefaultServer()
// TODO think other way. this is for create database session
private var context: ServletContext = null
private def configure() = {
server.setPort(DEFAULT_PORT) // TODO read from config
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(s"${Directory.GitBucketHome}/gitbucket.ser"))
server.setPublickeyAuthenticator(new PublicKeyAuthenticator)
server.setCommandFactory(new GitCommandFactory)
server.setCommandFactory(new GitCommandFactory(context))
}
def start(context: ServletContext) = this.synchronized {
if (SSH_SERVICE_ENABLE) {
this.context = context
configure()
server.start()
logger.info(s"Start SSH Server Listen on ${server.getPort}")
@@ -38,8 +33,6 @@ object SshServer {
def stop() = {
server.stop(true)
}
def getServletContext = this.context
}
/*