Search in sources :

Example 21 with BinaryResult

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();
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 22 with BinaryResult

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();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ZipOutputStream(java.util.zip.ZipOutputStream) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 23 with BinaryResult

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;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Aggregations

BinaryResult (com.google.gerrit.extensions.restapi.BinaryResult)23 Test (org.junit.Test)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)10 OutputStream (java.io.OutputStream)6 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)5 RobotCommentInfo (com.google.gerrit.extensions.common.RobotCommentInfo)5 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)5 IOException (java.io.IOException)5 FixReplacementInfo (com.google.gerrit.extensions.common.FixReplacementInfo)4 FixSuggestionInfo (com.google.gerrit.extensions.common.FixSuggestionInfo)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 CountingOutputStream (com.google.common.io.CountingOutputStream)3 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)3 Project (com.google.gerrit.reviewdb.client.Project)3 ChangeApi (com.google.gerrit.extensions.api.changes.ChangeApi)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 IdString (com.google.gerrit.extensions.restapi.IdString)2 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)2