mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
(refs #464)Some improve for plugin API
- Place holder support for db API - Redirect support for plugin action
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
|
import java.sql.PreparedStatement
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
import scala.collection.mutable.ListBuffer
|
import scala.collection.mutable.ListBuffer
|
||||||
|
|
||||||
package object plugin {
|
package object plugin {
|
||||||
|
|
||||||
|
case class Redirect(path: String)
|
||||||
|
|
||||||
object db {
|
object db {
|
||||||
// TODO Use JavaScript Map instead of java.util.Map
|
// TODO labelled place holder support
|
||||||
def select(sql: String): Seq[Map[String, String]] = {
|
def select(sql: String, params: Any*): Seq[Map[String, String]] = {
|
||||||
defining(PluginConnectionHolder.threadLocal.get){ conn =>
|
defining(PluginConnectionHolder.threadLocal.get){ conn =>
|
||||||
using(conn.prepareStatement(sql)){ stmt =>
|
using(conn.prepareStatement(sql)){ stmt =>
|
||||||
|
setParams(stmt, params: _*)
|
||||||
using(stmt.executeQuery()){ rs =>
|
using(stmt.executeQuery()){ rs =>
|
||||||
val list = new ListBuffer[Map[String, String]]()
|
val list = new ListBuffer[Map[String, String]]()
|
||||||
while(rs.next){
|
while(rs.next){
|
||||||
@@ -25,13 +29,25 @@ package object plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def update(sql: String): Int = {
|
// TODO labelled place holder support
|
||||||
|
def update(sql: String, params: Any*): Int = {
|
||||||
defining(PluginConnectionHolder.threadLocal.get){ conn =>
|
defining(PluginConnectionHolder.threadLocal.get){ conn =>
|
||||||
using(conn.prepareStatement(sql)){ stmt =>
|
using(conn.prepareStatement(sql)){ stmt =>
|
||||||
|
setParams(stmt, params: _*)
|
||||||
stmt.executeUpdate()
|
stmt.executeUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private def setParams(stmt: PreparedStatement, params: Any*): Unit = {
|
||||||
|
params.zipWithIndex.foreach { case (p, i) =>
|
||||||
|
p match {
|
||||||
|
case x: String => stmt.setString(i + 1, x)
|
||||||
|
case x: Int => stmt.setInt(i + 1, x)
|
||||||
|
case x: Boolean => stmt.setBoolean(i + 1, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import util.{JGitUtil, Keys}
|
|||||||
import plugin.PluginConnectionHolder
|
import plugin.PluginConnectionHolder
|
||||||
import service.RepositoryService.RepositoryInfo
|
import service.RepositoryService.RepositoryInfo
|
||||||
import plugin.Security._
|
import plugin.Security._
|
||||||
|
import plugin.Redirect
|
||||||
|
|
||||||
class PluginActionInvokeFilter extends Filter with SystemSettingsService with RepositoryService with AccountService {
|
class PluginActionInvokeFilter extends Filter with SystemSettingsService with RepositoryService with AccountService {
|
||||||
|
|
||||||
@@ -33,17 +34,23 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
|||||||
private def processGlobalAction(path: String, request: HttpServletRequest, response: HttpServletResponse)
|
private def processGlobalAction(path: String, request: HttpServletRequest, response: HttpServletResponse)
|
||||||
(implicit session: Session): Boolean = {
|
(implicit session: Session): Boolean = {
|
||||||
plugin.PluginSystem.globalActions.find(x =>
|
plugin.PluginSystem.globalActions.find(x =>
|
||||||
x.method.toLowerCase == request.getMethod.toLowerCase && x.path == path
|
x.method.toLowerCase == request.getMethod.toLowerCase && path.matches(x.path)
|
||||||
).map { action =>
|
).map { action =>
|
||||||
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
val loginAccount = request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||||
val systemSettings = loadSystemSettings()
|
val systemSettings = loadSystemSettings()
|
||||||
implicit val context = app.Context(systemSettings, Option(loginAccount), request)
|
implicit val context = app.Context(systemSettings, Option(loginAccount), request)
|
||||||
|
|
||||||
if(authenticate(action.security, context)){
|
if(authenticate(action.security, context)){
|
||||||
val result = action.function(request, response, context)
|
val result = try {
|
||||||
|
PluginConnectionHolder.threadLocal.set(session.conn)
|
||||||
|
action.function(request, response, context)
|
||||||
|
} finally {
|
||||||
|
PluginConnectionHolder.threadLocal.remove()
|
||||||
|
}
|
||||||
result match {
|
result match {
|
||||||
case x: String => renderGlobalHtml(request, response, context, x)
|
case x: String => renderGlobalHtml(request, response, context, x)
|
||||||
case x: Html => renderGlobalHtml(request, response, context, x.toString)
|
case x: Html => renderGlobalHtml(request, response, context, x.toString)
|
||||||
|
case x: Redirect => response.sendRedirect(x.path)
|
||||||
case x: AnyRef => renderJson(request, response, x)
|
case x: AnyRef => renderJson(request, response, x)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -66,7 +73,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
|||||||
implicit val context = app.Context(systemSettings, Option(loginAccount), request)
|
implicit val context = app.Context(systemSettings, Option(loginAccount), request)
|
||||||
|
|
||||||
getRepository(owner, name, systemSettings.baseUrl(request)).flatMap { repository =>
|
getRepository(owner, name, systemSettings.baseUrl(request)).flatMap { repository =>
|
||||||
plugin.PluginSystem.repositoryActions.find(_.path == remain).map { action =>
|
plugin.PluginSystem.repositoryActions.find(x => remain.matches(x.path)).map { action =>
|
||||||
if(authenticate(action.security, context, repository)){
|
if(authenticate(action.security, context, repository)){
|
||||||
val result = try {
|
val result = try {
|
||||||
PluginConnectionHolder.threadLocal.set(session.conn)
|
PluginConnectionHolder.threadLocal.set(session.conn)
|
||||||
@@ -77,6 +84,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
|||||||
result match {
|
result match {
|
||||||
case x: String => renderRepositoryHtml(request, response, context, repository, x)
|
case x: String => renderRepositoryHtml(request, response, context, repository, x)
|
||||||
case x: Html => renderGlobalHtml(request, response, context, x.toString)
|
case x: Html => renderGlobalHtml(request, response, context, x.toString)
|
||||||
|
case x: Redirect => response.sendRedirect(x.path)
|
||||||
case x: AnyRef => renderJson(request, response, x)
|
case x: AnyRef => renderJson(request, response, x)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user