From 535c54b5a5c57db5f2fe624916108d43849808f2 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Sun, 2 Nov 2014 17:15:31 +0000 Subject: [PATCH] Initial test server. --- services/git-bridge/.idea/workspace.xml | 14 ++- services/git-bridge/src/uk/ac/ic/Main.java | 111 ++++++++++++++++++++- 2 files changed, 113 insertions(+), 12 deletions(-) diff --git a/services/git-bridge/.idea/workspace.xml b/services/git-bridge/.idea/workspace.xml index e49aa86066..898cdb6f0a 100644 --- a/services/git-bridge/.idea/workspace.xml +++ b/services/git-bridge/.idea/workspace.xml @@ -3,8 +3,6 @@ - - @@ -60,8 +58,8 @@ - - + + @@ -387,7 +385,7 @@ - + @@ -406,7 +404,7 @@ - + @@ -505,8 +503,8 @@ - - + + diff --git a/services/git-bridge/src/uk/ac/ic/Main.java b/services/git-bridge/src/uk/ac/ic/Main.java index 1870376bef..a24eed17c1 100644 --- a/services/git-bridge/src/uk/ac/ic/Main.java +++ b/services/git-bridge/src/uk/ac/ic/Main.java @@ -1,18 +1,121 @@ package uk.ac.ic; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.http.server.GitServlet; import org.eclipse.jgit.lib.*; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.eclipse.jgit.transport.ReceivePack; +import org.eclipse.jgit.transport.ServiceMayNotContinueException; +import org.eclipse.jgit.transport.UploadPack; +import org.eclipse.jgit.transport.resolver.*; -import java.io.IOException; +import javax.servlet.*; +import javax.servlet.descriptor.JspConfigDescriptor; +import javax.servlet.http.HttpServletRequest; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; /** - * Created by Roxy on 01/11/14. + * Created by Winston on 01/11/14. */ public class Main { - public static void main(String[] args) { - + public static void main(String[] args) throws Exception { + Server server = new Server(8080); + final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + servletContextHandler.setContextPath("/"); + server.setHandler(servletContextHandler); + GitServlet servlet = new GitServlet(); + servlet.setRepositoryResolver(new RepositoryResolver() { + @Override + public Repository open(HttpServletRequest httpServletRequest, String s) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException { + System.out.println(s); + + + File workspace = new File("/Users/Roxy/git-test/hello"); + workspace.mkdirs(); + Repository r = null; + try { + r = new FileRepositoryBuilder().setWorkTree(workspace).build(); + } catch (IOException e) { + e.printStackTrace(); + } + + // if the repository doesn't exist, create it + if (!r.getObjectDatabase().exists()) { + try { + r.create(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + // import initial content + Git git = new Git(r); + AddCommand cmd = git.add(); + cmd.addFilepattern("."); + cmd.call(); + + CommitCommand co = git.commit(); + co.setAuthor("Winston","wl3912@ic.ac.uk"); + co.setMessage("Initial import of the existing contents"); + co.call(); + } catch (GitAPIException e) { + e.printStackTrace(); + } + } + return r; + } + }); + + servlet.setReceivePackFactory(new ReceivePackFactory() { + @Override + public ReceivePack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException { + return new ReceivePack(repository); + } + }); + + servlet.setUploadPackFactory(new UploadPackFactory() { + @Override + public UploadPack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException { + return new UploadPack(repository); + } + }); + servlet.init(new ServletConfig() { + @Override + public String getServletName() { + return ""; + } + + @Override + public ServletContext getServletContext() { + return servletContextHandler.getServletContext(); + } + + @Override + public String getInitParameter(String s) { + return "default_init_parameter"; + } + + @Override + public Enumeration getInitParameterNames() { + return null; + } + }); + servletContextHandler.addServlet(new ServletHolder(servlet), "/*"); + server.start(); + server.join(); + + } }