diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/BridgeAPI.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java similarity index 87% rename from services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/BridgeAPI.java rename to services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java index 30e3fcc38c..9586c7f521 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/BridgeAPI.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java @@ -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 diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/GitProjectRepo.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/GitProjectRepo.java new file mode 100644 index 0000000000..aa6aefeaa8 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/GitProjectRepo.java @@ -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 getFiles() + throws IOException, GitUserException { + return new RepositoryObjectTreeWalker( + repository + ).getDirectoryContents().getFileTable(); + } + + @Override + public Collection commitAndGetMissing( + GitDirectoryContents contents + ) throws IOException { + try { + return doCommitAndGetMissing(contents); + } catch (GitAPIException e) { + throw new IOException(e); + } + } + + private Collection 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 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; + } + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/ProjectRepo.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/ProjectRepo.java new file mode 100644 index 0000000000..068ad76bbb --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/ProjectRepo.java @@ -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 getFiles() throws IOException, GitUserException; + + Collection commitAndGetMissing( + GitDirectoryContents gitDirectoryContents + ) throws IOException; + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java index 9fc2b694c4..7afc1bff9f 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java @@ -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); } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotFetcher.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotFetcher.java index bd9b59af7f..532411489d 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotFetcher.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotFetcher.java @@ -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 getSnapshotsForProjectAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, SnapshotPostException, ForbiddenException { + public LinkedList getSnapshotsForProjectAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, GitUserException { List snapshotInfos = getSnapshotInfosAfterVersion(oauth2, projectName, version); List snapshotDatas = getMatchingSnapshotData(oauth2, projectName, snapshotInfos); LinkedList snapshots = combine(snapshotInfos, snapshotDatas); return snapshots; } - private List getSnapshotInfosAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, SnapshotPostException, ForbiddenException { + private List getSnapshotInfosAfterVersion(Credential oauth2, String projectName, int version) throws FailedConnectionException, GitUserException { SortedSet versions = new TreeSet(); GetDocRequest getDoc = new GetDocRequest(oauth2, projectName); GetSavedVersRequest getSavedVers = new GetSavedVersRequest(oauth2, projectName); diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotRepositoryBuilder.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotRepositoryBuilder.java index e0b6ad93da..5b0818cecb 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotRepositoryBuilder.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/SnapshotRepositoryBuilder.java @@ -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); } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java index b26a819b06..302edaa9ff 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java @@ -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 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 snapshots) - throws IOException, GitAPIException, SnapshotPostException { + throws IOException, GitUserException { + String name = repo.getProjectName(); for (Snapshot snapshot : snapshots) { - Map fileTable = new RepositoryObjectTreeWalker(repository).getDirectoryContents().getFileTable(); - List files = new LinkedList(); + Map fileTable = repo.getFiles(); + List files = new LinkedList<>(); files.addAll(snapshot.getSrcs()); - Map fetchedUrls = new HashMap(); + Map 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 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 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), diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java index 257033088b..a689cf7153 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java @@ -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 fileTable, Map fetchedUrls) throws IOException, SnapshotPostException { + public RawFile get(String projectName, String url, String newPath, Map fileTable, Map fetchedUrls) throws IOException { String path = persistentStore.getPathForURLInProject(projectName, url); byte[] contents; if (path == null) { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/GitUserException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/GitUserException.java new file mode 100644 index 0000000000..93f5fe22aa --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/GitUserException.java @@ -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 getDescriptionLines(); + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java index a48d9bdb0b..b80343beaf 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java @@ -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) { - - } } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SnapshotAPIException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SnapshotAPIException.java new file mode 100644 index 0000000000..bd200d5314 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SnapshotAPIException.java @@ -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 { +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLReceivePackFactory.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLReceivePackFactory.java index 6ae12ca2f2..542963767d 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLReceivePackFactory.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLReceivePackFactory.java @@ -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 { - private final BridgeAPI bridgeAPI; + private final Bridge bridgeAPI; - public WLReceivePackFactory(BridgeAPI bridgeAPI) { + public WLReceivePackFactory(Bridge bridgeAPI) { this.bridgeAPI = bridgeAPI; } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLRepositoryResolver.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLRepositoryResolver.java index 341a05c4b2..4aade82f18 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLRepositoryResolver.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/WLRepositoryResolver.java @@ -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 walkGitObjectTree() throws IOException, SnapshotPostException { + private Map walkGitObjectTree() throws IOException, GitUserException { Map fileContentsTable = new HashMap(); if (treeWalk == null) { return fileContentsTable; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/FileHandler.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/FileHandler.java index de95b4b8d6..fc44ad7b07 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/FileHandler.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/FileHandler.java @@ -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; } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java index 5df3037be2..dd4a8c671f 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java @@ -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()); diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackContents.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackContents.java index 41135a57be..0f937f7716 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackContents.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackContents.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackHandler.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackHandler.java index 540e1b5383..35abdd37fe 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackHandler.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PostbackHandler.java @@ -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; } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/ForbiddenException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/ForbiddenException.java index 26e095af65..e2cc5aae52 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/ForbiddenException.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/ForbiddenException.java @@ -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 getDescriptionLines() { + return Arrays.asList(getMessage()); + } + } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/GetDocResult.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/GetDocResult.java index 63c864bc3a..8b621c60ca 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/GetDocResult.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/GetDocResult.java @@ -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; } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/exception/InvalidProjectException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/exception/InvalidProjectException.java index e6e895a483..a9d4bd1c6e 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/exception/InvalidProjectException.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/exception/InvalidProjectException.java @@ -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 errors; - public InvalidProjectException(JsonObject json) { - super(json); - } - public InvalidProjectException() { super(); errors = new LinkedList(); @@ -37,7 +32,8 @@ public class InvalidProjectException extends SnapshotPostException { @Override public void fromJSON(JsonElement json) { errors = new LinkedList(); - JsonArray errors = json.getAsJsonObject().get("errors").getAsJsonArray(); + JsonArray errors = + json.getAsJsonObject().get("errors").getAsJsonArray(); for (JsonElement error : errors) { this.errors.add(error.getAsString()); } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/exception/SnapshotPostException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/exception/SnapshotPostException.java index 6252642eef..d89a592fec 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/exception/SnapshotPostException.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/exception/SnapshotPostException.java @@ -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 getDescriptionLines(); - } diff --git a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcherTest.java b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcherTest.java index d171c21cd7..f4ba462ba0 100644 --- a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcherTest.java +++ b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcherTest.java @@ -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";