use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class RestApiServlet method replyBinaryResult.
@SuppressWarnings("resource")
static long replyBinaryResult(@Nullable HttpServletRequest req, HttpServletResponse res, BinaryResult bin) throws IOException {
final BinaryResult appResult = bin;
try {
if (bin.getAttachmentName() != null) {
res.setHeader("Content-Disposition", "attachment; filename=\"" + bin.getAttachmentName() + "\"");
}
if (bin.isBase64()) {
if (req != null && JSON_TYPE.equals(req.getHeader(HttpHeaders.ACCEPT))) {
bin = stackJsonString(res, bin);
} else {
bin = stackBase64(res, bin);
}
}
if (bin.canGzip() && acceptsGzip(req)) {
bin = stackGzip(res, bin);
}
res.setContentType(bin.getContentType());
long len = bin.getContentLength();
if (0 <= len && len < Integer.MAX_VALUE) {
res.setContentLength((int) len);
} else if (0 <= len) {
res.setHeader("Content-Length", Long.toString(len));
}
if (req == null || !"HEAD".equals(req.getMethod())) {
try (CountingOutputStream dst = new CountingOutputStream(res.getOutputStream())) {
bin.writeTo(dst);
return dst.getCount();
}
}
return 0;
} finally {
appResult.close();
}
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class GetPatch method apply.
@Override
public BinaryResult apply(RevisionResource rsrc) throws ResourceConflictException, IOException, ResourceNotFoundException {
Project.NameKey project = rsrc.getControl().getProject().getNameKey();
final Repository repo = repoManager.openRepository(project);
boolean close = true;
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 {
// Only add header if no path is specified
if (path == null) {
out.write(formatEmailHeader(commit).getBytes(UTF_8));
}
try (DiffFormatter fmt = new DiffFormatter(out)) {
fmt.setRepository(repo);
if (path != null) {
fmt.setPathFilter(PathFilter.create(path));
}
fmt.format(base.getTree(), commit.getTree());
fmt.flush();
}
}
@Override
public void close() throws IOException {
rw.close();
repo.close();
}
};
if (path != null && bin.asString().isEmpty()) {
throw new ResourceNotFoundException(String.format(FILE_NOT_FOUND, path));
}
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.close();
}
}
} finally {
if (close) {
repo.close();
}
}
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class FileContentUtil method asBinaryResult.
private static BinaryResult asBinaryResult(byte[] raw, final ObjectLoader obj) {
if (raw != null) {
return BinaryResult.create(raw);
}
BinaryResult result = new BinaryResult() {
@Override
public void writeTo(OutputStream os) throws IOException {
obj.copyTo(os);
}
};
result.setContentLength(obj.getSize());
return result;
}
Aggregations