From c8b4b7f8f9aa2ada52a57d3b3142332fb52169d4 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 23 Oct 2010 13:00:13 +0200 Subject: [PATCH] improve GitCGIServlet --- .../java/sonia/scm/web/GitCGIServlet.java | 92 ++++++++++++++++++- .../main/resources/sonia/scm/git.index.html | 58 ++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 plugins/scm-git-plugin/src/main/resources/sonia/scm/git.index.html diff --git a/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitCGIServlet.java b/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitCGIServlet.java index 7da4f5114b..753bb2299d 100644 --- a/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitCGIServlet.java +++ b/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitCGIServlet.java @@ -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 */ diff --git a/plugins/scm-git-plugin/src/main/resources/sonia/scm/git.index.html b/plugins/scm-git-plugin/src/main/resources/sonia/scm/git.index.html new file mode 100644 index 0000000000..4adbcf280d --- /dev/null +++ b/plugins/scm-git-plugin/src/main/resources/sonia/scm/git.index.html @@ -0,0 +1,58 @@ + + + SCM :: Manager - Git Repository + + + + +

SCM :: Manager - Git Repository

+ + + + + \ No newline at end of file