Fix dropped IOException in GetPatch
Do not drop the exception on the floor. Allow an unexpected IOException to go up to RestApiServlet where it will be logged and may turn into an HTTP 500 to the client. Change-Id: I3efe714ca30668c9d3bb93f2a3c68a50ce780531
This commit is contained in:

committed by
David Ostrovsky

parent
e666236369
commit
d5db8c0109
@@ -18,7 +18,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
@@ -57,84 +56,80 @@ public class GetPatch implements RestReadView<RevisionResource> {
|
||||
|
||||
@Override
|
||||
public BinaryResult apply(RevisionResource rsrc)
|
||||
throws ResourceNotFoundException, ResourceConflictException {
|
||||
throws ResourceConflictException, IOException {
|
||||
Project.NameKey project = rsrc.getControl().getProject().getNameKey();
|
||||
final Repository repo = repoManager.openRepository(project);
|
||||
boolean close = true;
|
||||
try {
|
||||
final Repository repo = repoManager.openRepository(project);
|
||||
final RevWalk rw = new RevWalk(repo);
|
||||
try {
|
||||
final RevWalk rw = new RevWalk(repo);
|
||||
try {
|
||||
final RevCommit commit =
|
||||
rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet()
|
||||
.getRevision().get()));
|
||||
RevCommit[] parents = commit.getParents();
|
||||
if (parents.length > 1) {
|
||||
throw new ResourceConflictException(
|
||||
"Revision has more than 1 parent.");
|
||||
} else if (parents.length == 0) {
|
||||
throw new ResourceConflictException("Revision has no parent.");
|
||||
}
|
||||
final RevCommit base = parents[0];
|
||||
rw.parseBody(base);
|
||||
|
||||
BinaryResult bin = new BinaryResult() {
|
||||
@Override
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
if (zip) {
|
||||
ZipOutputStream zos = new ZipOutputStream(out);
|
||||
ZipEntry e = new ZipEntry(fileName(rw, commit));
|
||||
e.setTime(commit.getCommitTime() * 1000L);
|
||||
zos.putNextEntry(e);
|
||||
format(zos);
|
||||
zos.closeEntry();
|
||||
zos.finish();
|
||||
} else {
|
||||
format(out);
|
||||
}
|
||||
}
|
||||
|
||||
private void format(OutputStream out) throws IOException {
|
||||
out.write(formatEmailHeader(commit).getBytes(UTF_8));
|
||||
DiffFormatter fmt = new DiffFormatter(out);
|
||||
fmt.setRepository(repo);
|
||||
fmt.format(base.getTree(), commit.getTree());
|
||||
fmt.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
rw.release();
|
||||
repo.close();
|
||||
}
|
||||
};
|
||||
|
||||
if (zip) {
|
||||
bin.disableGzip()
|
||||
.setContentType("application/zip")
|
||||
.setAttachmentName(fileName(rw, commit) + ".zip");
|
||||
} else {
|
||||
bin.base64()
|
||||
.setContentType("application/mbox")
|
||||
.setAttachmentName(download
|
||||
? fileName(rw, commit) + ".base64"
|
||||
: null);
|
||||
}
|
||||
|
||||
close = false;
|
||||
return bin;
|
||||
} finally {
|
||||
if (close) {
|
||||
rw.release();
|
||||
}
|
||||
final RevCommit commit =
|
||||
rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet()
|
||||
.getRevision().get()));
|
||||
RevCommit[] parents = commit.getParents();
|
||||
if (parents.length > 1) {
|
||||
throw new ResourceConflictException(
|
||||
"Revision has more than 1 parent.");
|
||||
} else if (parents.length == 0) {
|
||||
throw new ResourceConflictException("Revision has no parent.");
|
||||
}
|
||||
final RevCommit base = parents[0];
|
||||
rw.parseBody(base);
|
||||
|
||||
BinaryResult bin = new BinaryResult() {
|
||||
@Override
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
if (zip) {
|
||||
ZipOutputStream zos = new ZipOutputStream(out);
|
||||
ZipEntry e = new ZipEntry(fileName(rw, commit));
|
||||
e.setTime(commit.getCommitTime() * 1000L);
|
||||
zos.putNextEntry(e);
|
||||
format(zos);
|
||||
zos.closeEntry();
|
||||
zos.finish();
|
||||
} else {
|
||||
format(out);
|
||||
}
|
||||
}
|
||||
|
||||
private void format(OutputStream out) throws IOException {
|
||||
out.write(formatEmailHeader(commit).getBytes(UTF_8));
|
||||
DiffFormatter fmt = new DiffFormatter(out);
|
||||
fmt.setRepository(repo);
|
||||
fmt.format(base.getTree(), commit.getTree());
|
||||
fmt.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
rw.release();
|
||||
repo.close();
|
||||
}
|
||||
};
|
||||
|
||||
if (zip) {
|
||||
bin.disableGzip()
|
||||
.setContentType("application/zip")
|
||||
.setAttachmentName(fileName(rw, commit) + ".zip");
|
||||
} else {
|
||||
bin.base64()
|
||||
.setContentType("application/mbox")
|
||||
.setAttachmentName(download
|
||||
? fileName(rw, commit) + ".base64"
|
||||
: null);
|
||||
}
|
||||
|
||||
close = false;
|
||||
return bin;
|
||||
} finally {
|
||||
if (close) {
|
||||
repo.close();
|
||||
rw.release();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ResourceNotFoundException();
|
||||
} finally {
|
||||
if (close) {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user