improve GitCGIServlet

This commit is contained in:
Sebastian Sdorra
2010-10-23 13:00:13 +02:00
parent 0478552be3
commit c8b4b7f8f9
2 changed files with 148 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import com.google.inject.Singleton;
import sonia.scm.repository.GitConfig;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.util.IOUtil;
import sonia.scm.web.cgi.AbstractCGIServlet;
import sonia.scm.web.cgi.EnvList;
@@ -21,9 +22,12 @@ import sonia.scm.web.cgi.EnvList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
@@ -39,6 +43,16 @@ public class GitCGIServlet extends AbstractCGIServlet
/** Field description */
public static final String ENV_PROJECT_ROOT = "GIT_PROJECT_ROOT";
/** Field description */
public static final String MIMETYPE_HTML = "text/html";
/** Field description */
public static final String REGEX_GITHTTPBACKEND =
"(?x)^/git/(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\\.(pack|idx))|git-(upload|receive)-pack))$";
/** Field description */
public static final String RESOURCE_GITINDEX = "/sonia/scm/git.index.html";
/** Field description */
private static final long serialVersionUID = 9147517765161830847L;
@@ -90,13 +104,40 @@ public class GitCGIServlet extends AbstractCGIServlet
return env;
}
/**
* Method description
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String uri = getRelativePath(request);
if (uri.matches(REGEX_GITHTTPBACKEND))
{
super.service(request, response);
}
else
{
printGitInformation(response);
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param req
* @param request
*
* @return
*
@@ -104,7 +145,7 @@ public class GitCGIServlet extends AbstractCGIServlet
* @throws ServletException
*/
@Override
protected File getCommand(HttpServletRequest req)
protected File getCommand(HttpServletRequest request)
throws ServletException, IOException
{
GitConfig config = repositoryHandler.getConfig();
@@ -117,6 +158,53 @@ public class GitCGIServlet extends AbstractCGIServlet
return new File(config.getGitHttpBackend());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param response
*
* @throws IOException
* @throws ServletException
*/
private void printGitInformation(HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(MIMETYPE_HTML);
InputStream input = null;
OutputStream output = null;
try
{
input = GitCGIServlet.class.getResourceAsStream(RESOURCE_GITINDEX);
output = response.getOutputStream();
IOUtil.copy(input, output);
}
finally
{
IOUtil.close(input);
IOUtil.close(output);
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
private String getRelativePath(HttpServletRequest request)
{
return request.getRequestURI().substring(request.getContextPath().length());
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -0,0 +1,58 @@
<html>
<head>
<title>SCM :: Manager - Git Repository</title>
<style type="text/css">
body {
background-color: #ffffff;
font-size: 12px;
font-family: Verdana, "Verdana CE", Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
color: #000000;
margin: 10px;
}
h1, h2, h3, h4, h5 {
font-family: Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
font-weight: bold;
margin: 0px;
padding: 0px;
color: #D20005;
}
h1 {
font-size: 18px;
border-bottom: 1px solid #AFAFAF;
}
h2 {
font-size: 16px;
}
h3 {
font-size: 14px;
}
a:link, a:visited {
color: #045491;
font-weight : bold;
text-decoration: none;
}
a:link:hover, a:visited:hover {
color: #045491;
font-weight : bold;
text-decoration : underline;
}
</style>
</head>
<body>
<h1>SCM :: Manager - Git Repository</h1>
<ul>
<li><a href="http://git-scm.com/" target="_blank">Git Homepage</a></li>
<li><a href="http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html" target="_blank">Git Tutorial</a></li>
<li><a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html" target="_blank">Git User's Manual</a></li>
</ul>
</body>
</html>