Don't parse non-existing files in files REST collections
For non-existing members REST collections are expected to throw ResourceNotFoundException. Change-Id: I97680b9fc17d0348aef1a5926f73c7e948737e07 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -14,16 +14,39 @@
|
||||
|
||||
package com.google.gerrit.server.project;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestResource;
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevTree;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileResource implements RestResource {
|
||||
public static final TypeLiteral<RestView<FileResource>> FILE_KIND =
|
||||
new TypeLiteral<RestView<FileResource>>() {};
|
||||
|
||||
public static FileResource create(GitRepositoryManager repoManager,
|
||||
ProjectControl project, ObjectId rev, String path)
|
||||
throws ResourceNotFoundException, IOException {
|
||||
try (Repository repo =
|
||||
repoManager.openRepository(project.getProject().getNameKey());
|
||||
RevWalk rw = new RevWalk(repo)) {
|
||||
RevTree tree = rw.parseTree(rev);
|
||||
if (TreeWalk.forPath(repo, path, tree) != null) {
|
||||
return new FileResource(project, rev, path);
|
||||
}
|
||||
}
|
||||
throw new ResourceNotFoundException(IdString.fromDecoded(path));
|
||||
}
|
||||
|
||||
private final ProjectControl project;
|
||||
private final ObjectId rev;
|
||||
private final String path;
|
||||
|
||||
@@ -19,19 +19,25 @@ import com.google.gerrit.extensions.restapi.ChildCollection;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Singleton
|
||||
public class FilesCollection implements
|
||||
ChildCollection<BranchResource, FileResource> {
|
||||
private final DynamicMap<RestView<FileResource>> views;
|
||||
private final GitRepositoryManager repoManager;
|
||||
|
||||
@Inject
|
||||
FilesCollection(DynamicMap<RestView<FileResource>> views) {
|
||||
FilesCollection(DynamicMap<RestView<FileResource>> views,
|
||||
GitRepositoryManager repoManager) {
|
||||
this.views = views;
|
||||
this.repoManager = repoManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,11 +46,10 @@ public class FilesCollection implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileResource parse(BranchResource parent, IdString id) {
|
||||
return new FileResource(
|
||||
parent.getControl(),
|
||||
ObjectId.fromString(parent.getRevision()),
|
||||
id.get());
|
||||
public FileResource parse(BranchResource parent, IdString id)
|
||||
throws ResourceNotFoundException, IOException {
|
||||
return FileResource.create(repoManager, parent.getControl(),
|
||||
ObjectId.fromString(parent.getRevision()), id.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,17 +19,23 @@ import com.google.gerrit.extensions.restapi.ChildCollection;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Singleton
|
||||
public class FilesInCommitCollection implements
|
||||
ChildCollection<CommitResource, FileResource> {
|
||||
private final DynamicMap<RestView<FileResource>> views;
|
||||
private final GitRepositoryManager repoManager;
|
||||
|
||||
@Inject
|
||||
FilesInCommitCollection(DynamicMap<RestView<FileResource>> views) {
|
||||
FilesInCommitCollection(DynamicMap<RestView<FileResource>> views,
|
||||
GitRepositoryManager repoManager) {
|
||||
this.views = views;
|
||||
this.repoManager = repoManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,8 +45,9 @@ public class FilesInCommitCollection implements
|
||||
|
||||
@Override
|
||||
public FileResource parse(CommitResource parent, IdString id)
|
||||
throws ResourceNotFoundException {
|
||||
return new FileResource(parent.getProject(), parent.getCommit(), id.get());
|
||||
throws ResourceNotFoundException, IOException {
|
||||
return FileResource.create(repoManager, parent.getProject(),
|
||||
parent.getCommit(), id.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user