Decouple jgit's Repository class from the Bridge

This commit is contained in:
Winston Li 2016-08-20 17:18:22 +01:00 committed by Michael Mazour
parent c3625163bf
commit 5b810b64ba
25 changed files with 271 additions and 175 deletions

View file

@ -1,16 +1,14 @@
package uk.ac.ic.wlgitbridge.bridge;
import com.google.api.client.auth.oauth2.Credential;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import uk.ac.ic.wlgitbridge.data.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.data.ProjectLock;
import uk.ac.ic.wlgitbridge.data.ShutdownHook;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.data.model.DataStore;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.snapshot.push.PostbackManager;
@ -24,13 +22,13 @@ import java.io.IOException;
/**
* Created by Winston on 16/11/14.
*/
public class BridgeAPI {
public class Bridge {
private final DataStore dataStore;
private final PostbackManager postbackManager;
private final ProjectLock mainProjectLock;
public BridgeAPI(String rootGitDirectoryPath) {
public Bridge(String rootGitDirectoryPath) {
dataStore = new DataStore(rootGitDirectoryPath);
postbackManager = new PostbackManager();
mainProjectLock = new ProjectLock();
@ -46,7 +44,7 @@ public class BridgeAPI {
}
public boolean repositoryExists(Credential oauth2, String projectName)
throws ServiceMayNotContinueException, ForbiddenException {
throws ServiceMayNotContinueException, GitUserException {
lockForProject(projectName);
GetDocRequest getDocRequest = new GetDocRequest(oauth2, projectName);
getDocRequest.request();
@ -54,25 +52,19 @@ public class BridgeAPI {
getDocRequest.getResult().getVersionID();
} catch (InvalidProjectException e) {
return false;
} catch (FailedConnectionException e) {
throw e;
} catch (SnapshotPostException e) {
throw new ServiceMayNotContinueException(e.getMessage());
} finally {
unlockForProject(projectName);
}
return true;
}
public void getWritableRepositories(Credential oauth2,
String projectName,
Repository repository)
throws IOException,
SnapshotPostException,
GitAPIException,
ForbiddenException {
Log.info("[{}] Fetching", projectName);
dataStore.updateProjectWithName(oauth2, projectName, repository);
public void getWritableRepositories(
Credential oauth2,
ProjectRepo repo
) throws IOException,
GitUserException {
Log.info("[{}] Fetching", repo.getProjectName());
dataStore.updateProjectWithName(oauth2, repo);
}
public void

View file

@ -0,0 +1,93 @@
package uk.ac.ic.wlgitbridge.bridge;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.util.Log;
import uk.ac.ic.wlgitbridge.util.Util;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
/**
* Created by winston on 20/08/2016.
*/
public class GitProjectRepo implements ProjectRepo {
private final Repository repository;
private final String projectName;
public GitProjectRepo(Repository repository, String projectName) {
this.repository = repository;
this.projectName = projectName;
}
@Override
public String getProjectName() {
return projectName;
}
@Override
public Map<String, RawFile> getFiles()
throws IOException, GitUserException {
return new RepositoryObjectTreeWalker(
repository
).getDirectoryContents().getFileTable();
}
@Override
public Collection<String> commitAndGetMissing(
GitDirectoryContents contents
) throws IOException {
try {
return doCommitAndGetMissing(contents);
} catch (GitAPIException e) {
throw new IOException(e);
}
}
private Collection<String> doCommitAndGetMissing(
GitDirectoryContents contents
) throws IOException, GitAPIException {
String name = getProjectName();
Log.info("[{}] Writing commit", name);
contents.write();
Git git = new Git(repository);
Log.info("[{}] Getting missing files", name);
Set<String> missingFiles = git.status().call().getMissing();
for (String missing : missingFiles) {
Log.info("[{}] Git rm {}", name, missing);
git.rm().setCached(true).addFilepattern(missing).call();
}
Log.info("[{}] Calling Git add", name);
git.add().addFilepattern(".").call();
Log.info("[{}] Calling Git commit", name);
git.commit(
).setAuthor(
new PersonIdent(
contents.getUserName(),
contents.getUserEmail(),
contents.getWhen(),
TimeZone.getDefault()
)
).setMessage(
contents.getCommitMessage()
).call();
Log.info(
"[{}] Deleting files in directory: {}",
name,
contents.getDirectory().getAbsolutePath()
);
Util.deleteInDirectoryApartFrom(contents.getDirectory(), ".git");
return missingFiles;
}
}

View file

@ -0,0 +1,24 @@
package uk.ac.ic.wlgitbridge.bridge;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
/**
* Created by winston on 20/08/2016.
*/
public interface ProjectRepo {
String getProjectName();
Map<String, RawFile> getFiles() throws IOException, GitUserException;
Collection<String> commitAndGetMissing(
GitDirectoryContents gitDirectoryContents
) throws IOException;
}

View file

@ -1,13 +1,11 @@
package uk.ac.ic.wlgitbridge.bridge;
import com.google.api.client.auth.oauth2.Credential;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import java.io.IOException;
@ -18,15 +16,15 @@ public class WLBridgedProject {
private final Repository repository;
private final String name;
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
public WLBridgedProject(Repository repository, String name, BridgeAPI bridgeAPI) {
public WLBridgedProject(Repository repository, String name, Bridge bridgeAPI) {
this.repository = repository;
this.name = name;
this.bridgeAPI = bridgeAPI;
}
public void buildRepository(Credential oauth2) throws RepositoryNotFoundException, ServiceMayNotContinueException, ForbiddenException {
public void buildRepository(Credential oauth2) throws RepositoryNotFoundException, ServiceMayNotContinueException, GitUserException {
bridgeAPI.lockForProject(name);
try {
if (repository.getObjectDatabase().exists()) {
@ -42,21 +40,20 @@ public class WLBridgedProject {
}
}
private void updateRepositoryFromSnapshots(Credential oauth2, Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException, ForbiddenException {
private void updateRepositoryFromSnapshots(Credential oauth2, Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException, GitUserException {
try {
bridgeAPI.getWritableRepositories(oauth2, name, repository);
bridgeAPI.getWritableRepositories(
oauth2,
new GitProjectRepo(repository, name)
);
} catch (InvalidProjectException e) {
throw new RepositoryNotFoundException(name);
} catch (SnapshotPostException e) {
throw new ServiceMayNotContinueException(e.getDescriptionLines().get(0), e);
} catch (GitAPIException e) {
throw new ServiceMayNotContinueException(e);
} catch (IOException e) {
throw new ServiceMayNotContinueException(e);
}
}
private void buildRepositoryFromScratch(Credential oauth2, Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException, ForbiddenException {
private void buildRepositoryFromScratch(Credential oauth2, Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException, GitUserException {
if (!bridgeAPI.repositoryExists(oauth2, name)) {
throw new RepositoryNotFoundException(name);
}

View file

@ -1,16 +1,16 @@
package uk.ac.ic.wlgitbridge.data;
import com.google.api.client.auth.oauth2.Credential;
import uk.ac.ic.wlgitbridge.data.model.Snapshot;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocRequest;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.GetForVersionRequest;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.GetSavedVersRequest;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.SnapshotInfo;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.data.model.Snapshot;
import java.util.*;
@ -19,14 +19,14 @@ import java.util.*;
*/
public class SnapshotFetcher {
public LinkedList<Snapshot> getSnapshotsForProjectAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, SnapshotPostException, ForbiddenException {
public LinkedList<Snapshot> getSnapshotsForProjectAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, GitUserException {
List<SnapshotInfo> snapshotInfos = getSnapshotInfosAfterVersion(oauth2, projectName, version);
List<SnapshotData> snapshotDatas = getMatchingSnapshotData(oauth2, projectName, snapshotInfos);
LinkedList<Snapshot> snapshots = combine(snapshotInfos, snapshotDatas);
return snapshots;
}
private List<SnapshotInfo> getSnapshotInfosAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, SnapshotPostException, ForbiddenException {
private List<SnapshotInfo> getSnapshotInfosAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, GitUserException {
SortedSet<SnapshotInfo> versions = new TreeSet<SnapshotInfo>();
GetDocRequest getDoc = new GetDocRequest(oauth2, projectName);
GetSavedVersRequest getSavedVers = new GetSavedVersRequest(oauth2, projectName);

View file

@ -5,9 +5,9 @@ import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.util.Log;
@ -19,13 +19,13 @@ import java.io.IOException;
*/
public class SnapshotRepositoryBuilder {
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
public SnapshotRepositoryBuilder(BridgeAPI bridgeAPI) {
public SnapshotRepositoryBuilder(Bridge bridgeAPI) {
this.bridgeAPI = bridgeAPI;
}
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory, Credential oauth2) throws RepositoryNotFoundException, ServiceMayNotContinueException, ForbiddenException {
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory, Credential oauth2) throws RepositoryNotFoundException, ServiceMayNotContinueException, GitUserException {
if (!bridgeAPI.repositoryExists(oauth2, name)) {
throw new RepositoryNotFoundException(name);
}

View file

@ -1,10 +1,7 @@
package uk.ac.ic.wlgitbridge.data.model;
import com.google.api.client.auth.oauth2.Credential;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.bridge.ProjectRepo;
import uk.ac.ic.wlgitbridge.data.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.data.SnapshotFetcher;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;
@ -12,8 +9,7 @@ import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.model.db.PersistentStore;
import uk.ac.ic.wlgitbridge.data.model.db.SqlitePersistentStore;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotAttachment;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.util.Log;
@ -47,39 +43,37 @@ public class DataStore {
resourceFetcher = new ResourceFetcher(persistentStore);
}
public void updateProjectWithName(Credential oauth2,
String name,
Repository repository)
throws IOException,
SnapshotPostException,
GitAPIException,
ForbiddenException {
public void updateProjectWithName(
Credential oauth2,
ProjectRepo repo
) throws IOException, GitUserException {
String projectName = repo.getProjectName();
LinkedList<Snapshot> snapshots =
snapshotFetcher.getSnapshotsForProjectAfterVersion(
oauth2,
name,
persistentStore.getLatestVersionForProject(name)
projectName,
persistentStore.getLatestVersionForProject(projectName)
);
makeCommitsFromSnapshots(name, repository, snapshots);
makeCommitsFromSnapshots(repo, snapshots);
if (!snapshots.isEmpty()) {
persistentStore.setLatestVersionForProject(
name,
projectName,
snapshots.getLast().getVersionID()
);
}
}
private void makeCommitsFromSnapshots(String name,
Repository repository,
private void makeCommitsFromSnapshots(ProjectRepo repo,
List<Snapshot> snapshots)
throws IOException, GitAPIException, SnapshotPostException {
throws IOException, GitUserException {
String name = repo.getProjectName();
for (Snapshot snapshot : snapshots) {
Map<String, RawFile> fileTable = new RepositoryObjectTreeWalker(repository).getDirectoryContents().getFileTable();
List<RawFile> files = new LinkedList<RawFile>();
Map<String, RawFile> fileTable = repo.getFiles();
List<RawFile> files = new LinkedList<>();
files.addAll(snapshot.getSrcs());
Map<String, byte[]> fetchedUrls = new HashMap<String, byte[]>();
Map<String, byte[]> fetchedUrls = new HashMap<>();
for (SnapshotAttachment snapshotAttachment : snapshot.getAtts()) {
files.add(
resourceFetcher.get(
@ -96,61 +90,27 @@ public class DataStore {
name,
snapshot.getVersionID()
);
commit(name,
Collection<String> missingFiles = repo.commitAndGetMissing(
new GitDirectoryContents(
files,
rootGitDirectory,
name,
snapshot),
repository);
snapshot
)
);
persistentStore.deleteFilesForProject(
name,
missingFiles.toArray(new String[missingFiles.size()])
);
}
}
private void commit(String name,
GitDirectoryContents contents,
Repository repository) throws IOException,
GitAPIException {
Log.info("[{}] Writing commit", name);
contents.write();
Git git = new Git(repository);
Log.info("[{}] Getting missing files", name);
Set<String> missingFiles = git.status().call().getMissing();
for (String missing : missingFiles) {
Log.info("[{}] Git rm {}", name, missing);
git.rm().setCached(true).addFilepattern(missing).call();
}
Log.info("[{}] Calling Git add", name);
git.add().addFilepattern(".").call();
Log.info("[{}] Calling Git commit", name);
git.commit(
).setAuthor(
new PersonIdent(
contents.getUserName(),
contents.getUserEmail(),
contents.getWhen(),
TimeZone.getDefault()
)
).setMessage(
contents.getCommitMessage()
).call();
persistentStore.deleteFilesForProject(
name,
missingFiles.toArray(new String[missingFiles.size()])
);
Log.info(
"[{}] Deleting files in directory: {}",
name,
contents.getDirectory().getAbsolutePath()
);
Util.deleteInDirectoryApartFrom(contents.getDirectory(), ".git");
}
public CandidateSnapshot
createCandidateSnapshot(String projectName,
RawDirectory directoryContents,
RawDirectory oldDirectoryContents)
throws SnapshotPostException,
IOException {
public CandidateSnapshot createCandidateSnapshot(
String projectName,
RawDirectory directoryContents,
RawDirectory oldDirectoryContents
) throws SnapshotPostException,
IOException {
CandidateSnapshot candidateSnapshot = new CandidateSnapshot(
projectName,
persistentStore.getLatestVersionForProject(projectName),

View file

@ -3,14 +3,11 @@ package uk.ac.ic.wlgitbridge.data.model;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.Response;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
import uk.ac.ic.wlgitbridge.data.model.db.PersistentStore;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.ByteArrayOutputStream;
@ -29,7 +26,7 @@ public class ResourceFetcher {
this.persistentStore = persistentStore;
}
public RawFile get(String projectName, String url, String newPath, Map<String, RawFile> fileTable, Map<String, byte[]> fetchedUrls) throws IOException, SnapshotPostException {
public RawFile get(String projectName, String url, String newPath, Map<String, RawFile> fileTable, Map<String, byte[]> fetchedUrls) throws IOException {
String path = persistentStore.getPathForURLInProject(projectName, url);
byte[] contents;
if (path == null) {

View file

@ -0,0 +1,13 @@
package uk.ac.ic.wlgitbridge.git.exception;
import java.util.List;
/**
* Created by winston on 20/08/2016.
*/
public abstract class GitUserException extends Exception {
public abstract String getMessage();
public abstract List<String> getDescriptionLines();
}

View file

@ -1,13 +1,11 @@
package uk.ac.ic.wlgitbridge.git.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.util.Util;
import java.util.Arrays;
import java.util.List;
public class SizeLimitExceededException extends SnapshotPostException {
public class SizeLimitExceededException extends GitUserException {
private static String path = null;
@ -26,12 +24,8 @@ public class SizeLimitExceededException extends SnapshotPostException {
String filename = path != null ? "File '" + path + "' is" : "There's a file";
return Arrays.asList(
filename + " too large to push to " + Util.getServiceName() + " via git",
"the recommended maximum file size is 15MiB"
"the recommended maximum file size is 50 MiB"
);
}
@Override
public void fromJSON(JsonElement json) {
}
}

View file

@ -0,0 +1,11 @@
package uk.ac.ic.wlgitbridge.git.exception;
import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
/**
* Created by winston on 20/08/2016.
*/
public abstract class SnapshotAPIException
extends GitUserException
implements JSONSource {
}

View file

@ -6,7 +6,7 @@ import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.git.handler.hook.WriteLatexPutHook;
import uk.ac.ic.wlgitbridge.server.Oauth2Filter;
import uk.ac.ic.wlgitbridge.util.Util;
@ -19,9 +19,9 @@ import javax.servlet.http.HttpServletRequest;
/* */
public class WLReceivePackFactory implements ReceivePackFactory<HttpServletRequest> {
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
public WLReceivePackFactory(BridgeAPI bridgeAPI) {
public WLReceivePackFactory(Bridge bridgeAPI) {
this.bridgeAPI = bridgeAPI;
}

View file

@ -8,6 +8,7 @@ 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.data.SnapshotRepositoryBuilder;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.server.Oauth2Filter;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
@ -51,6 +52,8 @@ public class WLRepositoryResolver implements RepositoryResolver<HttpServletReque
throw new ServiceMayNotContinueException(e);
} catch (ForbiddenException e) {
throw new ServiceNotAuthorizedException();
} catch (GitUserException e) {
throw new ServiceMayNotContinueException(e.getMessage(), e);
}
}

View file

@ -6,12 +6,12 @@ import org.eclipse.jgit.transport.PreReceiveHook;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.ReceiveCommand.Result;
import org.eclipse.jgit.transport.ReceivePack;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.WrongBranchException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
@ -26,11 +26,11 @@ import java.util.Iterator;
*/
public class WriteLatexPutHook implements PreReceiveHook {
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
private final String hostname;
private final Credential oauth2;
public WriteLatexPutHook(BridgeAPI bridgeAPI, String hostname, Credential oauth2) {
public WriteLatexPutHook(Bridge bridgeAPI, String hostname, Credential oauth2) {
this.bridgeAPI = bridgeAPI;
this.hostname = hostname;
this.oauth2 = oauth2;
@ -72,7 +72,7 @@ public class WriteLatexPutHook implements PreReceiveHook {
receiveCommand.setResult(Result.REJECTED_OTHER_REASON, message);
}
private void handleReceiveCommand(Credential oauth2, Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, ForbiddenException {
private void handleReceiveCommand(Credential oauth2, Repository repository, ReceiveCommand receiveCommand) throws IOException, GitUserException {
checkBranch(receiveCommand);
checkForcedPush(receiveCommand);
bridgeAPI.putDirectoryContentsToProjectWithName(
@ -97,13 +97,13 @@ public class WriteLatexPutHook implements PreReceiveHook {
}
}
private RawDirectory getPushedDirectoryContents(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException {
private RawDirectory getPushedDirectoryContents(Repository repository, ReceiveCommand receiveCommand) throws IOException, GitUserException {
return new RepositoryObjectTreeWalker(repository,
receiveCommand.getNewId())
.getDirectoryContents();
}
private RawDirectory getOldDirectoryContents(Repository repository) throws IOException, SnapshotPostException {
private RawDirectory getOldDirectoryContents(Repository repository) throws IOException, GitUserException {
return new RepositoryObjectTreeWalker(repository).getDirectoryContents();
}

View file

@ -7,7 +7,7 @@ import uk.ac.ic.wlgitbridge.git.handler.WLReceivePackFactory;
import uk.ac.ic.wlgitbridge.git.handler.WLRepositoryResolver;
import uk.ac.ic.wlgitbridge.git.handler.WLUploadPackFactory;
import uk.ac.ic.wlgitbridge.data.SnapshotRepositoryBuilder;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import javax.servlet.ServletException;
@ -16,7 +16,7 @@ import javax.servlet.ServletException;
*/
public class WLGitServlet extends GitServlet {
public WLGitServlet(ServletContextHandler servletContextHandler, BridgeAPI bridgeAPI, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
public WLGitServlet(ServletContextHandler servletContextHandler, Bridge bridgeAPI, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
setRepositoryResolver(new WLRepositoryResolver(rootGitDirectoryPath, new SnapshotRepositoryBuilder(bridgeAPI)));
setReceivePackFactory(new WLReceivePackFactory(bridgeAPI));
setUploadPackFactory(new WLUploadPackFactory());

View file

@ -8,8 +8,8 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import java.io.IOException;
import java.util.HashMap;
@ -36,7 +36,7 @@ public class RepositoryObjectTreeWalker {
this(repository, repository.resolve("HEAD~" + fromHead));
}
public RawDirectory getDirectoryContents() throws IOException, SnapshotPostException {
public RawDirectory getDirectoryContents() throws IOException, GitUserException {
return new RawDirectory(walkGitObjectTree());
}
@ -51,7 +51,7 @@ public class RepositoryObjectTreeWalker {
return treeWalk;
}
private Map<String, RawFile> walkGitObjectTree() throws IOException, SnapshotPostException {
private Map<String, RawFile> walkGitObjectTree() throws IOException, GitUserException {
Map<String, RawFile> fileContentsTable = new HashMap<String, RawFile>();
if (treeWalk == null) {
return fileContentsTable;

View file

@ -4,7 +4,7 @@ import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InvalidPostbackKeyException;
import javax.servlet.ServletException;
@ -22,10 +22,10 @@ import java.util.regex.Pattern;
public class FileHandler extends ResourceHandler {
private static final Logger LOG = LoggerFactory.getLogger(FileHandler.class);
private final BridgeAPI writeLatexDataSource;
private final Bridge writeLatexDataSource;
private final Pattern DOC_KEY_PATTERN = Pattern.compile("^/(\\w+)/.+$");
public FileHandler(BridgeAPI writeLatexDataSource) {
public FileHandler(Bridge writeLatexDataSource) {
this.writeLatexDataSource = writeLatexDataSource;
}

View file

@ -8,7 +8,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import uk.ac.ic.wlgitbridge.application.config.Config;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
import uk.ac.ic.wlgitbridge.snapshot.base.SnapshotAPIRequest;
@ -31,7 +31,7 @@ import java.util.EnumSet;
*/
public class GitBridgeServer {
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
private final Server jettyServer;
@ -43,7 +43,7 @@ public class GitBridgeServer {
org.eclipse.jetty.util.log.Log.setLog(new NullLogger());
this.port = config.getPort();
this.rootGitDirectoryPath = config.getRootGitDirectory();
bridgeAPI = new BridgeAPI(rootGitDirectoryPath);
bridgeAPI = new Bridge(rootGitDirectoryPath);
jettyServer = new Server(port);
configureJettyServer(config);
SnapshotAPIRequest.setBasicAuth(config.getUsername(), config.getPassword());

View file

@ -3,7 +3,7 @@ package uk.ac.ic.wlgitbridge.server;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
@ -17,7 +17,7 @@ public class PostbackContents implements JSONSource {
private static final String CODE_SUCCESS = "upToDate";
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
private final String projectName;
private final String postbackKey;
@ -26,7 +26,7 @@ public class PostbackContents implements JSONSource {
private int versionID;
private SnapshotPostException exception;
public PostbackContents(BridgeAPI bridgeAPI, String projectName, String postbackKey, String contents) {
public PostbackContents(Bridge bridgeAPI, String projectName, String postbackKey, String contents) {
this.bridgeAPI = bridgeAPI;
this.projectName = projectName;
this.postbackKey = postbackKey;

View file

@ -4,7 +4,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.util.Log;
import uk.ac.ic.wlgitbridge.util.Util;
@ -19,9 +19,9 @@ import java.io.IOException;
*/
public class PostbackHandler extends AbstractHandler {
private final BridgeAPI bridgeAPI;
private final Bridge bridgeAPI;
public PostbackHandler(BridgeAPI bridgeAPI) {
public PostbackHandler(Bridge bridgeAPI) {
this.bridgeAPI = bridgeAPI;
}

View file

@ -1,7 +1,28 @@
package uk.ac.ic.wlgitbridge.snapshot.base;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException;
import java.util.Arrays;
import java.util.List;
/**
* Created by winston on 25/10/15.
*/
public class ForbiddenException extends Throwable {
public class ForbiddenException extends SnapshotAPIException {
@Override
public void fromJSON(JsonElement json) {
}
@Override
public String getMessage() {
return "forbidden";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList(getMessage());
}
}

View file

@ -2,14 +2,14 @@ package uk.ac.ic.wlgitbridge.snapshot.getdoc;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.base.Result;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.WLUser;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.ProtectedProjectException;
/**
* Created by Winston on 06/11/14.
@ -21,7 +21,7 @@ public class GetDocResult extends Result {
private String createdAt;
private WLUser user;
private SnapshotPostException exception;
private SnapshotAPIException exception;
private ForbiddenException forbidden;
public GetDocResult(Request request, JsonElement json) throws FailedConnectionException {
@ -94,7 +94,7 @@ public class GetDocResult extends Result {
}
}
public int getVersionID() throws SnapshotPostException {
public int getVersionID() throws GitUserException {
if (exception != null) {
throw exception;
}

View file

@ -2,8 +2,7 @@ package uk.ac.ic.wlgitbridge.snapshot.getdoc.exception;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException;
import java.util.LinkedList;
import java.util.List;
@ -11,14 +10,10 @@ import java.util.List;
/**
* Created by Winston on 08/11/14.
*/
public class InvalidProjectException extends SnapshotPostException {
public class InvalidProjectException extends SnapshotAPIException {
private List<String> errors;
public InvalidProjectException(JsonObject json) {
super(json);
}
public InvalidProjectException() {
super();
errors = new LinkedList<String>();
@ -37,7 +32,8 @@ public class InvalidProjectException extends SnapshotPostException {
@Override
public void fromJSON(JsonElement json) {
errors = new LinkedList<String>();
JsonArray errors = json.getAsJsonObject().get("errors").getAsJsonArray();
JsonArray errors =
json.getAsJsonObject().get("errors").getAsJsonArray();
for (JsonElement error : errors) {
this.errors.add(error.getAsString());
}

View file

@ -1,14 +1,12 @@
package uk.ac.ic.wlgitbridge.snapshot.push.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
import java.util.List;
import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException;
/**
* Created by Winston on 16/11/14.
*/
public abstract class SnapshotPostException extends Exception implements JSONSource {
public abstract class SnapshotPostException extends SnapshotAPIException {
public SnapshotPostException() {
@ -18,7 +16,4 @@ public abstract class SnapshotPostException extends Exception implements JSONSou
fromJSON(jsonElement);
}
public abstract String getMessage();
public abstract List<String> getDescriptionLines();
}

View file

@ -11,8 +11,8 @@ import org.mockserver.client.server.MockServerClient;
import org.mockserver.junit.MockServerRule;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.data.model.db.PersistentStore;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import java.io.IOException;
import java.util.HashMap;
@ -32,7 +32,7 @@ public class ResourceFetcherTest {
private MockServerClient mockServerClient;
@Test
public void fetchesFilesThatAreMissingFromUrlStoreCache() throws IOException, SnapshotPostException {
public void fetchesFilesThatAreMissingFromUrlStoreCache() throws IOException, GitUserException {
final String testProjectName = "123abc";
final String testUrl = "http://localhost:" + mockServerRule.getPort() + "/123abc";
final String oldTestPath = "testPath";