Start to implement WikiController.

Changed controllers from servlet to filter by mapping flexibility.
This commit is contained in:
takezoe
2013-05-02 02:01:51 +09:00
parent f1957e5c1f
commit 36ce53477f
14 changed files with 253 additions and 207 deletions

View File

@@ -4,8 +4,9 @@ import javax.servlet._
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
context.mount(new CreateRepositoryServlet, "/new")
context.mount(new RepositoryViewerServlet, "/*")
context.mount(new CreateRepositoryController, "/new")
context.mount(new WikiController, "/*")
context.mount(new RepositoryViewerController, "/*")
context.addListener(new ServletContextListener(){
def contextInitialized(e: ServletContextEvent): Unit = {

View File

@@ -11,7 +11,7 @@ import jp.sf.amateras.scalatra.forms._
/**
* Creates new repository.
*/
class CreateRepositoryServlet extends ServletBase {
class CreateRepositoryController extends ControllerBase {
case class RepositoryCreationForm(name: String, description: String)

View File

@@ -64,7 +64,7 @@ case class ContentInfo(viewType: String, content: Option[String])
/**
* The repository viewer.
*/
class RepositoryViewerServlet extends ServletBase {
class RepositoryViewerController extends ControllerBase {
/**
* Displays user information.

View File

@@ -9,7 +9,7 @@ import jp.sf.amateras.scalatra.forms._
/**
* Provides generic features for ScalatraServlet implementations.
*/
abstract class ServletBase extends ScalatraServlet with ClientSideValidationFormSupport with JacksonJsonSupport {
abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFormSupport with JacksonJsonSupport {
implicit val jsonFormats = DefaultFormats

View File

@@ -0,0 +1,9 @@
package app
class UsersController extends ControllerBase {
get("/"){
}
}

View File

@@ -1,9 +0,0 @@
package app
class UsersServlet extends ServletBase {
get("/"){
}
}

View File

@@ -0,0 +1,14 @@
package app
import util.{WikiUtil, JGitUtil}
class WikiController extends ControllerBase {
get("/:owner/:repository/wiki"){
val owner = params("owner")
val repository = params("repository")
html.wiki(WikiUtil.getPage(owner, repository, "Home"), JGitUtil.getRepositoryInfo(owner, repository, servletContext))
}
}

View File

@@ -1,77 +1,97 @@
package util
import java.io.File
import java.util.Date
import org.eclipse.jgit.api.Git
import org.apache.commons.io.FileUtils
object WikiUtil {
/**
* The model for wiki page.
*
* @param name the page name
package util
import java.io.File
import java.util.Date
import org.eclipse.jgit.api.Git
import org.apache.commons.io.FileUtils
import org.eclipse.jgit.lib.RepositoryBuilder
object WikiUtil {
/**
* The model for wiki page.
*
* @param name the page name
* @param content the page content
*/
case class WikiPageInfo(name: String, content: String)
/**
* The model for wiki page history.
*
* @param name the page name
* @param committer the committer the committer
* @param message the commit message
*/
case class WikiPageInfo(name: String, content: String)
/**
* The model for wiki page history.
*
* @param name the page name
* @param committer the committer the committer
* @param message the commit message
* @param date the commit date
*/
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
/**
*/
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
/**
* Returns the directory of the wiki repository.
*/
def getWikiRepositoryDir(owner: String, repository: String): File =
new File("%s/%s/%s-wiki.git".format(Directory.RepositoryHome, owner, repository))
/**
*/
def getWikiRepositoryDir(owner: String, repository: String): File =
new File("%s/%s/%s-wiki.git".format(Directory.RepositoryHome, owner, repository))
/**
* Returns the directory of the wiki working directory which is cloned from the wiki repository.
*/
def getWikiWorkDir(owner: String, repository: String): File =
new File("%s/tmp/%s/%s-wiki".format(Directory.RepositoryHome, owner, repository))
/**
*/
def getWikiWorkDir(owner: String, repository: String): File =
new File("%s/tmp/%s/%s-wiki".format(Directory.RepositoryHome, owner, repository))
// TODO synchronized?
def createWikiRepository(owner: String, repository: String): Unit = {
val dir = getWikiRepositoryDir(owner, repository)
if(!dir.exists){
val repo = new RepositoryBuilder().setGitDir(dir).setBare.build
repo.create
}
}
/**
* Returns the wiki page.
*/
def getPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
val git = Git.open(getWikiRepositoryDir(owner, repository))
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName).map { file =>
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"))
}
}
// TODO
//def getPageHistory(owner: String, repository: String, pageName: String): List[WikiPageHistoryInfo]
// TODO synchronized
/**
* Save the wiki page.
*/
def savePage(owner: String, repository: String, pageName: String, content: String, committer: String, message: String): Unit = {
val workDir = getWikiWorkDir(owner, repository)
// clone
if(!workDir.exists){
Git.cloneRepository.setURI(getWikiRepositoryDir(owner, repository).toURI.toString).setDirectory(workDir).call
}
// write as file
val file = new File(workDir, pageName + ".md")
FileUtils.writeStringToFile(file, content, "UTF-8")
// commit and push
val cloned = Git.open(workDir)
cloned.add.addFilepattern(file.getName).call
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
cloned.push.call
}
*/
def getPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
createWikiRepository(owner, repository)
val git = Git.open(getWikiRepositoryDir(owner, repository))
try {
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName).map { file =>
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"))
}
} catch {
// TODO no commit, but it should not judge by exception.
case e: NullPointerException => None
}
}
// TODO
// def getPageList(owner: String, repository: String): List[WikiPageHistoryInfo]
// TODO
//def getPageHistory(owner: String, repository: String, pageName: String): List[WikiPageHistoryInfo]
// TODO synchronized
/**
* Save the wiki page.
*/
def savePage(owner: String, repository: String, pageName: String, content: String, committer: String, message: String): Unit = {
createWikiRepository(owner, repository)
val workDir = getWikiWorkDir(owner, repository)
// clone
if(!workDir.exists){
Git.cloneRepository.setURI(getWikiRepositoryDir(owner, repository).toURI.toString).setDirectory(workDir).call
}
// write as file
val file = new File(workDir, pageName + ".md")
FileUtils.writeStringToFile(file, content, "UTF-8")
// commit and push
val cloned = Git.open(workDir)
cloned.add.addFilepattern(file.getName).call
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
cloned.push.call
}
}

View File

@@ -2,7 +2,7 @@
@import context._
@import view.helpers
@main(repository.owner+"/"+repository.name) {
@header(branch, repository)
@header("code", repository)
@navtab(branch, repository, "files")
<div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> /

View File

@@ -1,115 +1,115 @@
@(branch: String, commit: app.CommitInfo, repository: app.RepositoryInfo, diffs: Seq[app.DiffInfo])(implicit context: app.Context)
@import context._
@import view.helpers
@import org.eclipse.jgit.diff.DiffEntry.ChangeType
@main(helpers.cut(commit.message, 20)){
@header(branch, repository)
@navtab(branch, repository, "commits")
<table class="table table-bordered">
<tr>
<th>
<div>@helpers.format(commit.message)</div>
<div class="small" style="font-weight: normal;"><span class="description">@branch</span></div>
</th>
</tr>
<tr>
<td>
<a href="@path/@commit.committer">@commit.committer</a> <span class="description">@helpers.datetime(commit.time)</span>
<div class="pull-right align-right">
<span class="description">commit</span> @commit.id
</div>
</td>
</tr>
</table>
@diffs.zipWithIndex.map { case (diff, i) =>
<table class="table table-bordered">
<tr>
<th style="font-weight: normal;">
@if(diff.changeType == ChangeType.COPY || diff.changeType == ChangeType.RENAME){
@diff.oldPath -> @diff.newPath
}
@if(diff.changeType == ChangeType.ADD || diff.changeType == ChangeType.DELETE || diff.changeType == ChangeType.MODIFY){
@diff.newPath
}
<div class="pull-right align-right">
<a href="@path/@repository.owner/@repository.name/blob/@commit.id/@diff.newPath" class="btn btn-small">View file @@ @commit.id.substring(0, 10)</a>
</div>
</th>
</tr>
<tr>
<td>
@if(diff.newContent != None || diff.oldContent != None){
<div id="diff-@i"></div>
<textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea>
<textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea>
} else {
Too big file not shown
}
</td>
</tr>
</table>
}
}
<script type="text/javascript" src="@path/assets/jsdifflib/difflib.js"></script>
<script type="text/javascript" src="@path/assets/jsdifflib/diffview.js"></script>
<link href="@path/assets/jsdifflib/diffview.css" type="text/css" rel="stylesheet" />
<style type="text/css">
table.inlinediff {
width: 100%;
}
table.inlinediff thead {
display: none;
}
td.insert, td.equal, td.delete {
width: 100%;
}
</style>
<script>
function diffUsingJS(oldTextId, newTextId, outputId) {
// get the baseText and newText values from the two textboxes, and split them into lines
var oldText = document.getElementById(oldTextId).value;
if(oldText == ''){
var oldLines = [];
} else {
var oldLines = difflib.stringAsLines(oldText);
}
var newText = document.getElementById(newTextId).value
if(newText == ''){
var newLines = [];
} else {
var newLines = difflib.stringAsLines(newText);
}
// create a SequenceMatcher instance that diffs the two sets of lines
var sm = new difflib.SequenceMatcher(oldLines, newLines);
// get the opcodes from the SequenceMatcher instance
// opcodes is a list of 3-tuples describing what changes should be made to the base text
// in order to yield the new text
var opcodes = sm.get_opcodes();
var diffoutputdiv = document.getElementById(outputId);
while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
// build the diff view and add it to the current DOM
diffoutputdiv.appendChild(diffview.buildView({
baseTextLines: oldLines,
newTextLines: newLines,
opcodes: opcodes,
contextSize: 4,
viewType: 1
}));
}
$(function(){
@diffs.zipWithIndex.map { case (diff, i) =>
@if(diff.newContent != None || diff.oldContent != None){
diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i');
}
}
});
@(branch: String, commit: app.CommitInfo, repository: app.RepositoryInfo, diffs: Seq[app.DiffInfo])(implicit context: app.Context)
@import context._
@import view.helpers
@import org.eclipse.jgit.diff.DiffEntry.ChangeType
@main(helpers.cut(commit.message, 20)){
@header("code", repository)
@navtab(branch, repository, "commits")
<table class="table table-bordered">
<tr>
<th>
<div>@helpers.format(commit.message)</div>
<div class="small" style="font-weight: normal;"><span class="description">@branch</span></div>
</th>
</tr>
<tr>
<td>
<a href="@path/@commit.committer">@commit.committer</a> <span class="description">@helpers.datetime(commit.time)</span>
<div class="pull-right align-right">
<span class="description">commit</span> @commit.id
</div>
</td>
</tr>
</table>
@diffs.zipWithIndex.map { case (diff, i) =>
<table class="table table-bordered">
<tr>
<th style="font-weight: normal;">
@if(diff.changeType == ChangeType.COPY || diff.changeType == ChangeType.RENAME){
@diff.oldPath -> @diff.newPath
}
@if(diff.changeType == ChangeType.ADD || diff.changeType == ChangeType.DELETE || diff.changeType == ChangeType.MODIFY){
@diff.newPath
}
<div class="pull-right align-right">
<a href="@path/@repository.owner/@repository.name/blob/@commit.id/@diff.newPath" class="btn btn-small">View file @@ @commit.id.substring(0, 10)</a>
</div>
</th>
</tr>
<tr>
<td>
@if(diff.newContent != None || diff.oldContent != None){
<div id="diff-@i"></div>
<textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea>
<textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea>
} else {
Too big file not shown
}
</td>
</tr>
</table>
}
}
<script type="text/javascript" src="@path/assets/jsdifflib/difflib.js"></script>
<script type="text/javascript" src="@path/assets/jsdifflib/diffview.js"></script>
<link href="@path/assets/jsdifflib/diffview.css" type="text/css" rel="stylesheet" />
<style type="text/css">
table.inlinediff {
width: 100%;
}
table.inlinediff thead {
display: none;
}
td.insert, td.equal, td.delete {
width: 100%;
}
</style>
<script>
function diffUsingJS(oldTextId, newTextId, outputId) {
// get the baseText and newText values from the two textboxes, and split them into lines
var oldText = document.getElementById(oldTextId).value;
if(oldText == ''){
var oldLines = [];
} else {
var oldLines = difflib.stringAsLines(oldText);
}
var newText = document.getElementById(newTextId).value
if(newText == ''){
var newLines = [];
} else {
var newLines = difflib.stringAsLines(newText);
}
// create a SequenceMatcher instance that diffs the two sets of lines
var sm = new difflib.SequenceMatcher(oldLines, newLines);
// get the opcodes from the SequenceMatcher instance
// opcodes is a list of 3-tuples describing what changes should be made to the base text
// in order to yield the new text
var opcodes = sm.get_opcodes();
var diffoutputdiv = document.getElementById(outputId);
while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
// build the diff view and add it to the current DOM
diffoutputdiv.appendChild(diffview.buildView({
baseTextLines: oldLines,
newTextLines: newLines,
opcodes: opcodes,
contextSize: 4,
viewType: 1
}));
}
$(function(){
@diffs.zipWithIndex.map { case (diff, i) =>
@if(diff.newContent != None || diff.oldContent != None){
diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i');
}
}
});
</script>

View File

@@ -2,7 +2,7 @@
@import context._
@import view.helpers
@main(repository.owner+"/"+repository.name) {
@header(branch, repository)
@header("code", repository)
@navtab(branch, repository, "commits")
<div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> / Commit History

View File

@@ -2,7 +2,7 @@
@import context._
@import view.helpers
@main(repository.owner+"/"+repository.name) {
@header(branch, repository)
@header("code", repository)
@navtab(branch, repository, "files")
<div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> /

View File

@@ -1,4 +1,4 @@
@(branch: String, repository: app.RepositoryInfo)(implicit context: app.Context)
@(active: String, repository: app.RepositoryInfo)(implicit context: app.Context)
@import context._
<div class="head">
<a href="@path/@repository.owner">@repository.owner</a> / <a href="@path/@repository.owner/@repository.name">@repository.name</a>
@@ -6,10 +6,10 @@
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li class="active"><a href="#">Code</a></li>
<li><a href="#">Issue</a></li>
<li><a href="#">Wiki</a></li>
<li><a href="#">Settings</a></li>
<li@if(active=="code"){ class="active"}><a href="@path/@repository.owner/@repository.name">Code</a></li>
<li@if(active=="issue"){ class="active"}><a href="#">Issue</a></li>
<li@if(active=="wiki"){ class="active"}><a href="@path/@repository.owner/@repository.name/wiki">Wiki</a></li>
<li@if(active=="settings"){ class="active"}><a href="#">Settings</a></li>
</ul>
</div>
</div>

View File

@@ -0,0 +1,11 @@
@(page: Option[util.WikiUtil.WikiPageInfo], repository: app.RepositoryInfo)(implicit context: app.Context)
@main("Wiki"){
@header("wiki", repository)
<ul class="nav nav-tabs">
<li><a href="">Home</a></li>
<li><a href="">Pages</a></li>
<li><a href="">Wiki History</a></li>
<li><a href="">Git Access</a></li>
</ul>
xxxx
}