mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Server now allows clone/pull from existing repositories and rejects all pushes.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package uk.ac.ic.wlgitbridge.application;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.application.exception.InvalidProgramArgumentsException;
|
||||
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
@@ -40,6 +41,9 @@ public class WLGitBridgeApplication {
|
||||
new WLGitBridgeServer(port, rootGitDirectoryPath).start();
|
||||
} catch (ServletException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidRootDirectoryPathException e) {
|
||||
printUsage();
|
||||
System.exit(EXIT_CODE_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import uk.ac.ic.wlgitbridge.git.WLGitServlet;
|
||||
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.net.BindException;
|
||||
@@ -11,18 +12,30 @@ import java.net.BindException;
|
||||
/**
|
||||
* Created by Winston on 02/11/14.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class for the actual server.
|
||||
*/
|
||||
public class WLGitBridgeServer {
|
||||
|
||||
private final Server jettyServer;
|
||||
private final int port;
|
||||
|
||||
public WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException {
|
||||
/**
|
||||
* Constructs an instance of the server.
|
||||
* @param port the port number to listen on
|
||||
* @param rootGitDirectoryPath the root directory path containing the git repositories
|
||||
* @throws ServletException if the servlet throws an exception
|
||||
*/
|
||||
public WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
|
||||
this.port = port;
|
||||
jettyServer = new Server(port);
|
||||
configureJettyServer();
|
||||
|
||||
configureJettyServer(rootGitDirectoryPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the server on the port given on construction.
|
||||
*/
|
||||
public void start() {
|
||||
try {
|
||||
jettyServer.start();
|
||||
@@ -38,10 +51,14 @@ public class WLGitBridgeServer {
|
||||
}
|
||||
}
|
||||
|
||||
private void configureJettyServer() throws ServletException {
|
||||
private void configureJettyServer(String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
|
||||
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||
servletContextHandler.setContextPath("/");
|
||||
servletContextHandler.addServlet(new ServletHolder(new WLGitServlet(servletContextHandler)), "/*");
|
||||
servletContextHandler.addServlet(
|
||||
new ServletHolder(
|
||||
new WLGitServlet(servletContextHandler,rootGitDirectoryPath)),
|
||||
"/*"
|
||||
);
|
||||
jettyServer.setHandler(servletContextHandler);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.git;
|
||||
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jgit.http.server.GitServlet;
|
||||
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
@@ -10,8 +11,8 @@ import javax.servlet.ServletException;
|
||||
*/
|
||||
public class WLGitServlet extends GitServlet {
|
||||
|
||||
public WLGitServlet(ServletContextHandler servletContextHandler) throws ServletException {
|
||||
setRepositoryResolver(new WLRepositoryResolver());
|
||||
public WLGitServlet(ServletContextHandler servletContextHandler, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
|
||||
setRepositoryResolver(new WLRepositoryResolver(rootGitDirectoryPath));
|
||||
setReceivePackFactory(new WLReceivePackFactory());
|
||||
setUploadPackFactory(new WLUploadPackFactory());
|
||||
init(new WLGitServletConfig(servletContextHandler));
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.eclipse.jgit.transport.ServiceMayNotContinueException;
|
||||
import org.eclipse.jgit.transport.resolver.RepositoryResolver;
|
||||
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
|
||||
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
||||
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
@@ -21,44 +22,36 @@ import java.io.IOException;
|
||||
*/
|
||||
public class WLRepositoryResolver implements RepositoryResolver<HttpServletRequest> {
|
||||
|
||||
private File rootGitDirectory;
|
||||
|
||||
public WLRepositoryResolver(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
|
||||
initRootGitDirectory(rootGitDirectoryPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository open(HttpServletRequest httpServletRequest, String name) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException {
|
||||
System.out.println(name);
|
||||
File repositoryRoot = new File(rootGitDirectory.getAbsolutePath(), name);
|
||||
|
||||
|
||||
File workspace = new File("/Users/Roxy/git-test/hello");
|
||||
workspace.mkdirs();
|
||||
Repository r = null;
|
||||
Repository repository = null;
|
||||
try {
|
||||
r = new FileRepositoryBuilder().setWorkTree(workspace).build();
|
||||
repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
|
||||
// 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("i am java miaow");
|
||||
co.call();
|
||||
} catch (GitAPIException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!repository.getObjectDatabase().exists()) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
return repository;
|
||||
}
|
||||
|
||||
private void initRootGitDirectory(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
|
||||
rootGitDirectory = new File(rootGitDirectoryPath);
|
||||
/* throws SecurityException */
|
||||
rootGitDirectory.mkdirs();
|
||||
rootGitDirectory.getAbsolutePath();
|
||||
if (!rootGitDirectory.isDirectory()) {
|
||||
throw new InvalidRootDirectoryPathException();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package uk.ac.ic.wlgitbridge.git.exception;
|
||||
|
||||
/**
|
||||
* Created by Winston on 03/11/14.
|
||||
*/
|
||||
public class InvalidRootDirectoryPathException extends Throwable {
|
||||
}
|
||||
Reference in New Issue
Block a user