use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class PreviewSubmit method getBundles.
private BinaryResult getBundles(BatchUpdate.Factory updateFactory, RevisionResource rsrc, ArchiveFormat f) throws OrmException, RestApiException {
ReviewDb db = dbProvider.get();
ChangeControl control = rsrc.getControl();
IdentifiedUser caller = control.getUser().asIdentifiedUser();
Change change = rsrc.getChange();
// Returned BinaryResult takes ownership and handles closing.
@SuppressWarnings("resource") MergeOp op = mergeOpFactory.create(updateFactory);
try {
op.merge(db, change, caller, false, new SubmitInput(), true);
BinaryResult bin = new SubmitPreviewResult(op, f, maxBundleSize);
bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName("submit-preview-" + change.getChangeId() + "." + format);
return bin;
} catch (OrmException | RestApiException | RuntimeException e) {
op.close();
throw e;
}
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class FileContentUtil method zipBlob.
@SuppressWarnings("resource")
private BinaryResult zipBlob(final String path, final ObjectLoader obj, RevCommit commit, @Nullable final String suffix) {
final String commitName = commit.getName();
final long when = commit.getCommitTime() * 1000L;
return new BinaryResult() {
@Override
public void writeTo(OutputStream os) throws IOException {
try (ZipOutputStream zipOut = new ZipOutputStream(os)) {
String decoration = randSuffix();
if (!Strings.isNullOrEmpty(suffix)) {
decoration = suffix + '-' + decoration;
}
ZipEntry e = new ZipEntry(safeFileName(path, decoration));
e.setComment(commitName + ":" + path);
e.setSize(obj.getSize());
e.setTime(when);
zipOut.putNextEntry(e);
obj.copyTo(zipOut);
zipOut.closeEntry();
}
}
}.setContentType(ZIP_TYPE).setAttachmentName(safeFileName(path, suffix) + ".zip").disableGzip();
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class GetArchive method apply.
@Override
public BinaryResult apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
if (Strings.isNullOrEmpty(format)) {
throw new BadRequestException("format is not specified");
}
final ArchiveFormat f = allowedFormats.extensions.get("." + format);
if (f == null) {
throw new BadRequestException("unknown archive format");
}
if (f == ArchiveFormat.ZIP) {
throw new MethodNotAllowedException("zip format is disabled");
}
boolean close = true;
final Repository repo = repoManager.openRepository(rsrc.getControl().getProject().getNameKey());
try {
final RevCommit commit;
String name;
try (RevWalk rw = new RevWalk(repo)) {
commit = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
name = name(f, rw, commit);
}
BinaryResult bin = new BinaryResult() {
@Override
public void writeTo(OutputStream out) throws IOException {
try {
new ArchiveCommand(repo).setFormat(f.name()).setTree(commit.getTree()).setOutputStream(out).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}
@Override
public void close() throws IOException {
repo.close();
}
};
bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName(name);
close = false;
return bin;
} finally {
if (close) {
repo.close();
}
}
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class RevisionIT method patchWithPath.
@Test
public void patchWithPath() throws Exception {
PushOneCommit.Result r = createChange();
ChangeApi changeApi = gApi.changes().id(r.getChangeId());
BinaryResult bin = changeApi.revision(r.getCommit().name()).patch(FILE_NAME);
ByteArrayOutputStream os = new ByteArrayOutputStream();
bin.writeTo(os);
String res = new String(os.toByteArray(), UTF_8);
assertThat(res).isEqualTo(PATCH_FILE_ONLY);
exception.expect(ResourceNotFoundException.class);
exception.expectMessage("File not found: nonexistent-file.");
changeApi.revision(r.getCommit().name()).patch("nonexistent-file");
}
use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.
the class RevisionIT method patch.
@Test
public void patch() throws Exception {
PushOneCommit.Result r = createChange();
ChangeApi changeApi = gApi.changes().id(r.getChangeId());
BinaryResult bin = changeApi.revision(r.getCommit().name()).patch();
ByteArrayOutputStream os = new ByteArrayOutputStream();
bin.writeTo(os);
String res = new String(os.toByteArray(), UTF_8);
ChangeInfo change = changeApi.get();
RevisionInfo rev = change.revisions.get(change.currentRevision);
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
String date = df.format(rev.commit.author.date);
assertThat(res).isEqualTo(String.format(PATCH, r.getCommit().name(), date, r.getChangeId()));
}
Aggregations