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:
Shawn Pearce
2014-02-19 16:51:26 -08:00
committed by David Ostrovsky
parent e666236369
commit d5db8c0109

View File

@@ -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.BinaryResult;
import com.google.gerrit.extensions.restapi.ResourceConflictException; 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.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
@@ -57,84 +56,80 @@ public class GetPatch implements RestReadView<RevisionResource> {
@Override @Override
public BinaryResult apply(RevisionResource rsrc) public BinaryResult apply(RevisionResource rsrc)
throws ResourceNotFoundException, ResourceConflictException { throws ResourceConflictException, IOException {
Project.NameKey project = rsrc.getControl().getProject().getNameKey(); Project.NameKey project = rsrc.getControl().getProject().getNameKey();
final Repository repo = repoManager.openRepository(project);
boolean close = true; boolean close = true;
try { try {
final Repository repo = repoManager.openRepository(project); final RevWalk rw = new RevWalk(repo);
try { try {
final RevWalk rw = new RevWalk(repo); final RevCommit commit =
try { rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet()
final RevCommit commit = .getRevision().get()));
rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet() RevCommit[] parents = commit.getParents();
.getRevision().get())); if (parents.length > 1) {
RevCommit[] parents = commit.getParents(); throw new ResourceConflictException(
if (parents.length > 1) { "Revision has more than 1 parent.");
throw new ResourceConflictException( } else if (parents.length == 0) {
"Revision has more than 1 parent."); throw new ResourceConflictException("Revision has no 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 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 { } finally {
if (close) { if (close) {
repo.close(); rw.release();
} }
} }
} catch (IOException e) { } finally {
throw new ResourceNotFoundException(); if (close) {
repo.close();
}
} }
} }