Factor creation of RawInput instances out to RawInputUtil
There are several places where a new RawInput is created. Factor these out to static methods in a new RawInputUtil class in gerrit-common. Inspired-by: David Ostrovsky <david@ostrovsky.org> Change-Id: I10b099e269427b0f887bfb41bbb3f10f46cf2620
This commit is contained in:
@@ -28,9 +28,7 @@ import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class RestSession extends HttpSession {
|
||||
|
||||
@@ -113,30 +111,4 @@ public class RestSession extends HttpSession {
|
||||
public RestResponse delete(String endPoint) throws IOException {
|
||||
return execute(Request.Delete(url + "/a" + endPoint));
|
||||
}
|
||||
|
||||
|
||||
public static RawInput newRawInput(String content) {
|
||||
return newRawInput(content.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static RawInput newRawInput(final byte[] bytes) {
|
||||
Preconditions.checkNotNull(bytes);
|
||||
Preconditions.checkArgument(bytes.length > 0);
|
||||
return new RawInput() {
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return bytes.length;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.PushOneCommit;
|
||||
import com.google.gerrit.acceptance.RestResponse;
|
||||
import com.google.gerrit.acceptance.RestSession;
|
||||
import com.google.gerrit.acceptance.TestProjectInput;
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.data.LabelType;
|
||||
import com.google.gerrit.extensions.api.changes.ReviewInput;
|
||||
import com.google.gerrit.extensions.client.InheritableBoolean;
|
||||
@@ -141,7 +141,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
editUtil.delete(editUtil.byChange(change).get());
|
||||
assertThat(editUtil.byChange(change).isPresent()).isFalse();
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
.isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW2))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW2))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
editUtil.publish(editUtil.byChange(change).get());
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(edit.isPresent()).isFalse();
|
||||
@@ -169,7 +169,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
adminSession.post(urlPublish()).assertNoContent();
|
||||
edit = editUtil.byChange(change);
|
||||
@@ -187,7 +187,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
adminSession.delete(urlEdit()).assertNoContent();
|
||||
edit = editUtil.byChange(change);
|
||||
@@ -202,7 +202,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
adminSession.post(urlPublish()).assertForbidden();
|
||||
setUseContributorAgreements(InheritableBoolean.FALSE);
|
||||
adminSession.post(urlPublish()).assertNoContent();
|
||||
@@ -213,7 +213,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
ChangeEdit edit = editUtil.byChange(change).get();
|
||||
PatchSet current = getCurrentPatchSet(changeId);
|
||||
assertThat(edit.getBasePatchSet().getPatchSetId()).isEqualTo(
|
||||
@@ -236,7 +236,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
ChangeEdit edit = editUtil.byChange(change).get();
|
||||
PatchSet current = getCurrentPatchSet(changeId);
|
||||
assertThat(edit.getBasePatchSet().getPatchSetId()).isEqualTo(
|
||||
@@ -260,7 +260,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change2, current)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change2).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
ChangeEdit edit = editUtil.byChange(change2).get();
|
||||
assertThat(edit.getBasePatchSet().getPatchSetId()).isEqualTo(
|
||||
current.getPatchSetId());
|
||||
@@ -275,7 +275,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void updateExistingFile() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -392,7 +392,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
adminSession.get(urlEdit()).assertNoContent();
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
EditInfo info = toEditInfo(false);
|
||||
@@ -409,7 +409,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void retrieveFilesInEdit() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
|
||||
EditInfo info = toEditInfo(true);
|
||||
@@ -493,7 +493,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_OLD);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change2).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change2);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_NEW);
|
||||
@@ -534,12 +534,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void amendExistingFile() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_NEW);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW2)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW2)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -549,12 +549,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void createAndChangeEditInOneRequestRest() throws Exception {
|
||||
Put.Input in = new Put.Input();
|
||||
in.content = RestSession.newRawInput(CONTENT_NEW);
|
||||
in.content = RawInputUtil.create(CONTENT_NEW);
|
||||
adminSession.putRaw(urlEditFile(), in.content).assertNoContent();
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_NEW);
|
||||
in.content = RestSession.newRawInput(CONTENT_NEW2);
|
||||
in.content = RawInputUtil.create(CONTENT_NEW2);
|
||||
adminSession.putRaw(urlEditFile(), in.content).assertNoContent();
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -565,7 +565,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void changeEditRest() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Put.Input in = new Put.Input();
|
||||
in.content = RestSession.newRawInput(CONTENT_NEW);
|
||||
in.content = RawInputUtil.create(CONTENT_NEW);
|
||||
adminSession.putRaw(urlEditFile(), in.content).assertNoContent();
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -592,10 +592,10 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void getFileContentRest() throws Exception {
|
||||
Put.Input in = new Put.Input();
|
||||
in.content = RestSession.newRawInput(CONTENT_NEW);
|
||||
in.content = RawInputUtil.create(CONTENT_NEW);
|
||||
adminSession.putRaw(urlEditFile(), in.content).assertNoContent();
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RestSession.newRawInput(CONTENT_NEW2)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME, RawInputUtil.create(CONTENT_NEW2)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
RestResponse r = adminSession.getJsonAccept(urlEditFile());
|
||||
@@ -619,7 +619,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void addNewFile() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -630,12 +630,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void addNewFileAndAmend() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
Optional<ChangeEdit> edit = editUtil.byChange(change);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME2), CONTENT_NEW);
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RestSession.newRawInput(CONTENT_NEW2)))
|
||||
assertThat(modifier.modifyFile(edit.get(), FILE_NAME2, RawInputUtil.create(CONTENT_NEW2)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change);
|
||||
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
|
||||
@@ -650,7 +650,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
modifier.modifyFile(
|
||||
editUtil.byChange(change).get(),
|
||||
FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_OLD));
|
||||
RawInputUtil.create(CONTENT_OLD));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -696,12 +696,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
assertThat(modifier.createEdit(change2, current)).isEqualTo(RefUpdate.Result.NEW);
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change2).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
assertThat(queryEdits()).hasSize(2);
|
||||
|
||||
assertThat(
|
||||
modifier.modifyFile(editUtil.byChange(change).get(), FILE_NAME,
|
||||
RestSession.newRawInput(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
RawInputUtil.create(CONTENT_NEW))).isEqualTo(RefUpdate.Result.FORCED);
|
||||
editUtil.delete(editUtil.byChange(change).get());
|
||||
assertThat(queryEdits()).hasSize(1);
|
||||
|
||||
@@ -720,7 +720,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void files() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
ChangeEdit edit = editUtil.byChange(change).get();
|
||||
assertThat(modifier.modifyFile(edit, FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit, FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change).get();
|
||||
|
||||
@@ -738,7 +738,7 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
public void diff() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
ChangeEdit edit = editUtil.byChange(change).get();
|
||||
assertThat(modifier.modifyFile(edit, FILE_NAME, RestSession.newRawInput(CONTENT_NEW)))
|
||||
assertThat(modifier.modifyFile(edit, FILE_NAME, RawInputUtil.create(CONTENT_NEW)))
|
||||
.isEqualTo(RefUpdate.Result.FORCED);
|
||||
edit = editUtil.byChange(change).get();
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.PushOneCommit;
|
||||
import com.google.gerrit.acceptance.RestSession;
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.TimeUtil;
|
||||
import com.google.gerrit.extensions.common.CommitInfo;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
@@ -579,7 +579,7 @@ public class GetRelatedIT extends AbstractDaemonTest {
|
||||
Change ch2 = getChange(c2_1).change();
|
||||
editModifier.createEdit(ch2, getPatchSet(ch2.currentPatchSetId()));
|
||||
editModifier.modifyFile(editUtil.byChange(ch2).get(), "a.txt",
|
||||
RestSession.newRawInput(new byte[] {'a'}));
|
||||
RawInputUtil.create(new byte[] {'a'}));
|
||||
ObjectId editRev =
|
||||
ObjectId.fromString(editUtil.byChange(ch2).get().getRevision().get());
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ EXCLUDES = [
|
||||
SRC + 'common/PluginData.java',
|
||||
SRC + 'common/FileUtil.java',
|
||||
SRC + 'common/IoUtil.java',
|
||||
SRC + 'common/RawInputUtil.java',
|
||||
SRC + 'common/TimeUtil.java',
|
||||
SRC + 'common/data/SubscribeSection.java',
|
||||
]
|
||||
@@ -49,6 +50,7 @@ java_library(
|
||||
'//lib/log:api',
|
||||
'@jgit//org.eclipse.jgit:jgit',
|
||||
],
|
||||
provided_deps = ['//lib:servlet-api-3_1'],
|
||||
visibility = ['PUBLIC'],
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright (C) 2016 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.common;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.gerrit.extensions.restapi.RawInput;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class RawInputUtil {
|
||||
public static RawInput create(String content) {
|
||||
return create(content.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static RawInput create(final byte[] bytes, final String contentType) {
|
||||
Preconditions.checkNotNull(bytes);
|
||||
Preconditions.checkArgument(bytes.length > 0);
|
||||
return new RawInput() {
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return bytes.length;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static RawInput create(final byte[] bytes) {
|
||||
return create (bytes, "application/octet-stream");
|
||||
}
|
||||
|
||||
public static RawInput create(final HttpServletRequest req) {
|
||||
return new RawInput() {
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return req.getContentType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return req.getContentLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return req.getInputStream();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ import com.google.common.net.HttpHeaders;
|
||||
import com.google.gerrit.audit.AuditService;
|
||||
import com.google.gerrit.audit.ExtendedHttpAuditEvent;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.TimeUtil;
|
||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||
import com.google.gerrit.extensions.registration.DynamicMap;
|
||||
@@ -113,7 +114,6 @@ import java.io.BufferedWriter;
|
||||
import java.io.EOFException;
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
@@ -620,22 +620,7 @@ public class RestApiServlet extends HttpServlet {
|
||||
for (Field f : obj.getClass().getDeclaredFields()) {
|
||||
if (f.getType() == RawInput.class) {
|
||||
f.setAccessible(true);
|
||||
f.set(obj, new RawInput() {
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return req.getContentType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return req.getContentLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return req.getInputStream();
|
||||
}
|
||||
});
|
||||
f.set(obj, RawInputUtil.create(req));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ java_library(
|
||||
],
|
||||
provided_deps = [
|
||||
'//lib/bouncycastle:bcprov',
|
||||
'//lib:servlet-api-3_1',
|
||||
],
|
||||
visibility = ['PUBLIC'],
|
||||
)
|
||||
|
||||
@@ -17,13 +17,13 @@ package com.google.gerrit.sshd.commands;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.data.GlobalCapability;
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.extensions.annotations.RequiresCapability;
|
||||
import com.google.gerrit.extensions.api.accounts.EmailInput;
|
||||
import com.google.gerrit.extensions.common.SshKeyInfo;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.RawInput;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
@@ -51,9 +51,7 @@ import org.kohsuke.args4j.Argument;
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
@@ -225,22 +223,7 @@ final class SetAccountCommand extends SshCommand {
|
||||
OrmException, IOException {
|
||||
for (final String sshKey : sshKeys) {
|
||||
AddSshKey.Input in = new AddSshKey.Input();
|
||||
in.raw = new RawInput() {
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(sshKey.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "plain/text";
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return sshKey.length();
|
||||
}
|
||||
};
|
||||
in.raw = RawInputUtil.create(sshKey.getBytes(), "plain/text");
|
||||
addSshKey.apply(rsrc, in);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user