mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 22:23:42 +01:00
Abstracted snapshot work into interface.
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ public class WLGitBridgeApplication {
|
||||
|
||||
/**
|
||||
* Constructs an instance of the WriteLatex-Git Bridge application.
|
||||
* @param args args from main, which should be in the format "port root_git_directory_path"
|
||||
* @param args args from main, which should be in the format [port, root_git_directory_path]
|
||||
*/
|
||||
public WLGitBridgeApplication(String[] args) {
|
||||
try {
|
||||
|
||||
@@ -12,6 +12,8 @@ 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 uk.ac.ic.wlgitbridge.writelatex.RepositorySource;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.SnapshotRepositorySource;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
@@ -23,25 +25,16 @@ import java.io.IOException;
|
||||
public class WLRepositoryResolver implements RepositoryResolver<HttpServletRequest> {
|
||||
|
||||
private File rootGitDirectory;
|
||||
private RepositorySource repositorySource;
|
||||
|
||||
public WLRepositoryResolver(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
|
||||
initRootGitDirectory(rootGitDirectoryPath);
|
||||
repositorySource = new SnapshotRepositorySource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository open(HttpServletRequest httpServletRequest, String name) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException {
|
||||
File repositoryRoot = new File(rootGitDirectory.getAbsolutePath(), name);
|
||||
|
||||
Repository repository = null;
|
||||
try {
|
||||
repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
|
||||
} catch (IOException e) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
if (!repository.getObjectDatabase().exists()) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
return repository;
|
||||
return repositorySource.getRepositoryWithNameAtRootDirectory(name, rootGitDirectory);
|
||||
}
|
||||
|
||||
private void initRootGitDirectory(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex;
|
||||
|
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by Winston on 03/11/14.
|
||||
*/
|
||||
public interface RepositorySource {
|
||||
|
||||
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package uk.ac.ic.wlgitbridge.writelatex;
|
||||
|
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 03/11/14.
|
||||
*/
|
||||
public class SnapshotRepositorySource implements RepositorySource {
|
||||
|
||||
@Override
|
||||
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException {
|
||||
File repositoryRoot = new File(rootDirectory.getAbsolutePath(), name);
|
||||
|
||||
Repository repository = null;
|
||||
try {
|
||||
repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
|
||||
} catch (IOException e) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
if (!repository.getObjectDatabase().exists()) {
|
||||
throw new RepositoryNotFoundException(name);
|
||||
}
|
||||
return repository;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user