Add RawData and 404 error response for plugin action

This commit is contained in:
Naoki Takezoe
2014-09-03 02:26:06 +09:00
parent cde09d3a59
commit d33886db89
2 changed files with 31 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ package object plugin {
case class Redirect(path: String)
case class Fragment(html: Html)
case class RawData(contentType: String, content: Array[Byte])
object db {
// TODO labelled place holder support

View File

@@ -7,7 +7,7 @@ import play.twirl.api.Html
import service.{AccountService, RepositoryService, SystemSettingsService}
import model.{Account, Session}
import util.{JGitUtil, Keys}
import plugin.{Fragment, PluginConnectionHolder, Redirect}
import plugin.{RawData, Fragment, PluginConnectionHolder, Redirect}
import service.RepositoryService.RepositoryInfo
import plugin.Security._
@@ -46,13 +46,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
} finally {
PluginConnectionHolder.threadLocal.remove()
}
result match {
case x: String => renderGlobalHtml(request, response, context, x)
case x: Html => renderGlobalHtml(request, response, context, x.toString)
case x: Fragment => renderFragmentHtml(request, response, context, x.html.toString)
case x: Redirect => response.sendRedirect(x.path)
case x: AnyRef => renderJson(request, response, x)
}
processActionResult(result, request, response, context)
} else {
// TODO NotFound or Error?
}
@@ -81,13 +75,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
} finally {
PluginConnectionHolder.threadLocal.remove()
}
result match {
case x: String => renderRepositoryHtml(request, response, context, repository, x)
case x: Html => renderGlobalHtml(request, response, context, x.toString)
case x: Fragment => renderFragmentHtml(request, response, context, x.html.toString)
case x: Redirect => response.sendRedirect(x.path)
case x: AnyRef => renderJson(request, response, x)
}
processActionResult(result, request, response, context)
} else {
// TODO NotFound or Error?
}
@@ -97,6 +85,24 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
} else false
}
private def processActionResult(result: Any, request: HttpServletRequest, response: HttpServletResponse,
context: app.Context): Unit = {
result match {
case null|None => renderError(request, response, context, 404)
case x: String => renderGlobalHtml(request, response, context, x)
case Some(x: String) => renderGlobalHtml(request, response, context, x)
case x: Html => renderGlobalHtml(request, response, context, x.toString)
case Some(x: Html) => renderGlobalHtml(request, response, context, x.toString)
case x: Fragment => renderFragmentHtml(request, response, context, x.html.toString)
case Some(x: Fragment) => renderFragmentHtml(request, response, context, x.html.toString)
case x: RawData => renderRawData(request, response, context, x)
case Some(x: RawData) => renderRawData(request, response, context, x)
case x: Redirect => response.sendRedirect(x.path)
case Some(x: Redirect) => response.sendRedirect(x.path)
case x: AnyRef => renderJson(request, response, x)
}
}
/**
* Authentication for global action
*/
@@ -145,6 +151,10 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
}
}
private def renderError(request: HttpServletRequest, response: HttpServletResponse, context: app.Context, error: Int): Unit = {
response.sendError(error)
}
private def renderGlobalHtml(request: HttpServletRequest, response: HttpServletResponse, context: app.Context, body: String): Unit = {
response.setContentType("text/html; charset=UTF-8")
val html = _root_.html.main("GitBucket", None)(Html(body))(context)
@@ -162,6 +172,11 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
IOUtils.write(body.getBytes("UTF-8"), response.getOutputStream)
}
private def renderRawData(request: HttpServletRequest, response: HttpServletResponse, context: app.Context, rawData: RawData): Unit = {
response.setContentType(rawData.contentType)
IOUtils.write(rawData.content, response.getOutputStream)
}
private def renderJson(request: HttpServletRequest, response: HttpServletResponse, obj: AnyRef): Unit = {
import org.json4s._
import org.json4s.jackson.Serialization