Cleanup BytesRefrence interface (elastic/elasticsearch#2670)

This is a followup of elastic/elasticsearchelastic/elasticsearch#19196

Original commit: elastic/x-pack-elasticsearch@1d0398e89a
This commit is contained in:
Simon Willnauer 2016-07-01 16:09:53 +02:00 committed by GitHub
parent 6527683e48
commit 158a6b5588
45 changed files with 120 additions and 101 deletions

View File

@ -5,6 +5,8 @@
*/
package org.elasticsearch.license.core;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefIterator;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -51,7 +53,11 @@ public class LicenseVerifier {
license.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
Signature rsa = Signature.getInstance("SHA512withRSA");
rsa.initVerify(CryptUtils.readEncryptedPublicKey(encryptedPublicKeyData));
rsa.update(contentBuilder.bytes().toBytes());
BytesRefIterator iterator = contentBuilder.bytes().iterator();
BytesRef ref;
while((ref = iterator.next()) != null) {
rsa.update(ref.bytes, ref.offset, ref.length);
}
return rsa.verify(signedContent)
&& Arrays.equals(Base64.getEncoder().encode(encryptedPublicKeyData), signatureHash);
} catch (IOException | NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {

View File

@ -5,6 +5,9 @@
*/
package org.elasticsearch.license.licensor;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefIterator;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -55,7 +58,11 @@ public class LicenseSigner {
try {
final Signature rsa = Signature.getInstance("SHA512withRSA");
rsa.initSign(CryptUtils.readEncryptedPrivateKey(Files.readAllBytes(privateKeyPath)));
rsa.update(contentBuilder.bytes().toBytes());
final BytesRefIterator iterator = contentBuilder.bytes().iterator();
BytesRef ref;
while((ref = iterator.next()) != null) {
rsa.update(ref.bytes, ref.offset, ref.length);
}
signedContent = rsa.sign();
} catch (InvalidKeyException | IOException | NoSuchAlgorithmException | SignatureException e) {
throw new IllegalStateException(e);

View File

@ -105,7 +105,7 @@ public class MigrateToolIT extends MigrateToolTestCase {
assertArrayEquals(ip.getPrivileges(), new String[]{"read", "write", "create_index", "indices:admin/refresh"});
assertArrayEquals(ip.getFields(), new String[]{"foo", "bar"});
assertNotNull(ip.getQuery());
assertThat(ip.getQuery().toUtf8(), containsString("{\"bool\":{\"must_not\":{\"match\":{\"hidden\":true}}}}"));
assertThat(ip.getQuery().utf8ToString(), containsString("{\"bool\":{\"must_not\":{\"match\":{\"hidden\":true}}}}"));
} else {
assertArrayEquals(ip.getIndices(), new String[]{"*"});
assertArrayEquals(ip.getPrivileges(), new String[]{"read"});

View File

@ -9,6 +9,7 @@ import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
@ -186,7 +187,7 @@ public class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implemen
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(contentBuilder,
new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
streamOutput.writeString(Base64.getEncoder().encodeToString(encrypt(contentBuilder.bytes().toBytes())));
streamOutput.writeString(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
}
} else {
if (license == LICENSE_TOMBSTONE) {

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.license.plugin.core;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -32,7 +33,7 @@ public class TrialLicense {
try {
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
spec.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
byte[] encrypt = encrypt(contentBuilder.bytes().toBytes());
byte[] encrypt = encrypt(BytesReference.toBytes(contentBuilder.bytes()));
byte[] bytes = new byte[4 + 4 + encrypt.length];
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
// always generate license version -VERSION_CURRENT

View File

@ -37,7 +37,7 @@ public class RestPutLicenseAction extends BaseRestHandler {
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final NodeClient client) {
PutLicenseRequest putLicenseRequest = new PutLicenseRequest();
putLicenseRequest.license(request.content().toUtf8());
putLicenseRequest.license(request.content().utf8ToString());
putLicenseRequest.acknowledge(request.paramAsBoolean("acknowledge", false));
client.admin().cluster().execute(PutLicenseAction.INSTANCE, putLicenseRequest,
new RestBuilderListener<PutLicenseResponse>(channel) {

View File

@ -5,8 +5,8 @@
*/
package org.elasticsearch.license.plugin;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -16,7 +16,6 @@ import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -70,7 +69,7 @@ public class PutLicenseResponseTests extends ESTestCase {
// write it out
response.writeTo(output);
ByteBufferStreamInput input = new ByteBufferStreamInput(ByteBuffer.wrap(output.bytes().toBytes()));
StreamInput input = output.bytes().streamInput();
// read it back in
response.readFrom(input);

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.license.plugin;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -96,7 +97,7 @@ public class TrialLicenseTests extends ESTestCase {
try {
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
spec.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
byte[] encrypt = encrypt(contentBuilder.bytes().toBytes());
byte[] encrypt = encrypt(BytesReference.toBytes(contentBuilder.bytes()));
byte[] bytes = new byte[4 + 4 + encrypt.length];
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byteBuffer.putInt(-spec.version())

View File

@ -9,8 +9,9 @@ import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
@ -24,7 +25,6 @@ import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.license.plugin.TestUtils;
import org.elasticsearch.test.ESTestCase;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.Collections;
import java.util.UUID;
@ -42,9 +42,7 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startObject("licenses");
licensesMetaData.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
byte[] serializedBytes = builder.bytes().toBytes();
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(serializedBytes);
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(license));
}
@ -90,9 +88,7 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startObject("licenses");
licensesMetaData.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
byte[] serializedBytes = builder.bytes().toBytes();
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(serializedBytes);
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(trialLicense));
}
@ -113,13 +109,13 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startArray("trial_licenses");
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
builder.value(Base64.getEncoder().encodeToString(encrypt(contentBuilder.bytes().toBytes())));
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
builder.endArray();
builder.startArray("signed_licenses");
builder.endArray();
builder.endObject();
builder.endObject();
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes().toBytes());
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(trialLicense));
// signed license
@ -133,7 +129,7 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.endArray();
builder.endObject();
builder.endObject();
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes().toBytes());
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicense));
// trial and signed license
@ -143,14 +139,14 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startArray("trial_licenses");
contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
builder.value(Base64.getEncoder().encodeToString(encrypt(contentBuilder.bytes().toBytes())));
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
builder.endArray();
builder.startArray("signed_licenses");
signedLicense.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endArray();
builder.endObject();
builder.endObject();
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes().toBytes());
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicense));
// license with later issue date is selected
@ -162,7 +158,7 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startArray("trial_licenses");
contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
builder.value(Base64.getEncoder().encodeToString(encrypt(contentBuilder.bytes().toBytes())));
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
builder.endArray();
builder.startArray("signed_licenses");
signedLicense.toXContent(builder, ToXContent.EMPTY_PARAMS);
@ -170,7 +166,7 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.endArray();
builder.endObject();
builder.endObject();
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes().toBytes());
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicenseIssuedLater));
}
@ -190,13 +186,12 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
output.writeVInt(1);
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
output.writeString(Base64.getEncoder().encodeToString(encrypt(contentBuilder.bytes().toBytes())));
byte[] bytes = output.bytes().toBytes();
ByteBufferStreamInput input = new ByteBufferStreamInput(ByteBuffer.wrap(bytes));
input.setVersion(Version.V_2_0_0_beta1);
LicensesMetaData licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
assertThat(licensesMetaData.getLicense(), equalTo(trialLicense));
output.writeString(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
try (StreamInput input = output.bytes().streamInput()) {
input.setVersion(Version.V_2_0_0_beta1);
LicensesMetaData licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
assertThat(licensesMetaData.getLicense(), equalTo(trialLicense));
}
// signed license
License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
@ -204,11 +199,11 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
output.writeVInt(1);
signedLicense.writeTo(output);
output.writeVInt(0);
bytes = output.bytes().toBytes();
input = new ByteBufferStreamInput(ByteBuffer.wrap(bytes));
input.setVersion(Version.V_2_0_0_beta1);
licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
assertThat(licensesMetaData.getLicense(), equalTo(signedLicense));
try (StreamInput input = output.bytes().streamInput()) {
input.setVersion(Version.V_2_0_0_beta1);
LicensesMetaData licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
assertThat(licensesMetaData.getLicense(), equalTo(signedLicense));
}
}
public void testLicenseTombstoneFromXContext() throws Exception {
@ -216,11 +211,11 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
builder.startObject("licenses");
builder.nullField("license");
builder.endObject();
LicensesMetaData metaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes().toBytes());
LicensesMetaData metaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
assertThat(metaDataFromXContent.getLicense(), equalTo(LicensesMetaData.LICENSE_TOMBSTONE));
}
private static LicensesMetaData getLicensesMetaDataFromXContent(byte[] bytes) throws Exception {
private static LicensesMetaData getLicensesMetaDataFromXContent(BytesReference bytes) throws Exception {
final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(bytes);
parser.nextToken(); // consume null
parser.nextToken(); // consume "licenses"

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.monitoring.agent.exporter.http;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.SpecialPermission;
@ -216,7 +217,8 @@ public class HttpExporter extends Exporter {
out.write(CONTENT_TYPE.xContent().streamSeparator());
// Render the monitoring document
out.write(resolver.source(doc, CONTENT_TYPE).toBytes());
BytesRef bytesRef = resolver.source(doc, CONTENT_TYPE).toBytesRef();
out.write(bytesRef.bytes, bytesRef.offset, bytesRef.length);
// Adds final bulk separator
out.write(CONTENT_TYPE.xContent().streamSeparator());
@ -718,8 +720,9 @@ public class HttpExporter extends Exporter {
for (MonitoringDoc monitoringDoc : docs) {
try {
render(monitoringDoc, buffer);
BytesRef bytesRef = buffer.bytes().toBytesRef();
// write the result to the connection
out.write(buffer.bytes().toBytes());
out.write(bytesRef.bytes, bytesRef.offset, bytesRef.length);
} finally {
buffer.reset();
}

View File

@ -56,7 +56,7 @@ public class MonitoringIndexTests extends ESTestCase {
index.writeTo(out);
final StreamInput in = StreamInput.wrap(out.bytes().toBytes());
final StreamInput in = out.bytes().streamInput();
assertSame(index, MonitoringIndex.readFrom(in));

View File

@ -139,7 +139,7 @@ public class HttpExporterTemplateTests extends AbstractExporterTemplateTestCase
boolean templateExist = templates.containsKey(templateName);
if ("GET".equals(request.getMethod())) {
return templateExist ? newResponse(200, templates.get(templateName).toUtf8()) : NOT_FOUND;
return templateExist ? newResponse(200, templates.get(templateName).utf8ToString()) : NOT_FOUND;
}
if ("PUT".equals(request.getMethod())) {
templates.put(templateName, new BytesArray(request.getBody().readByteArray()));

View File

@ -467,7 +467,8 @@ public class HttpExporterTests extends MonitoringIntegTestCase {
private void enqueueGetClusterVersionResponse(MockWebServer mockWebServer, Version v) throws IOException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(
jsonBuilder().startObject().startObject("version").field("number", v.toString()).endObject().endObject().bytes().toUtf8()));
jsonBuilder().startObject().startObject("version").field("number", v.toString()).endObject().endObject().bytes()
.utf8ToString()));
}
private void enqueueResponse(int responseCode, String body) throws IOException {

View File

@ -10,6 +10,7 @@ import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.security.authc.support.CharArrays;
@ -80,7 +81,7 @@ public class ChangePasswordRequest extends ActionRequest<ChangePasswordRequest>
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
username = in.readString();
passwordHash = CharArrays.utf8BytesToChars(in.readBytesReference().array());
passwordHash = CharArrays.utf8BytesToChars(BytesReference.toBytes(in.readBytesReference()));
refreshPolicy = RefreshPolicy.readFrom(in);
}

View File

@ -127,7 +127,7 @@ public class PutUserRequest extends ActionRequest<PutUserRequest> implements Use
if (passwordHashRef == BytesArray.EMPTY) {
passwordHash = null;
} else {
passwordHash = CharArrays.utf8BytesToChars(passwordHashRef.array());
passwordHash = CharArrays.utf8BytesToChars(BytesReference.toBytes(passwordHashRef));
}
roles = in.readStringArray();
fullName = in.readOptionalString();

View File

@ -22,7 +22,7 @@ public class AuditUtil {
try {
return XContentHelper.convertToJson(request.content(), false, false);
} catch (IOException ioe) {
return "Invalid Format: " + request.content().toUtf8();
return "Invalid Format: " + request.content().utf8ToString();
}
}
return "";

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.security.authc;
import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -132,7 +133,7 @@ public class Authentication {
BytesStreamOutput output = new BytesStreamOutput();
Version.writeVersion(Version.CURRENT, output);
writeTo(output);
return Base64.getEncoder().encodeToString(output.bytes().toBytes());
return Base64.getEncoder().encodeToString(BytesReference.toBytes(output.bytes()));
}
void writeTo(StreamOutput out) throws IOException {

View File

@ -17,7 +17,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
@ -303,7 +302,7 @@ public class RoleDescriptor implements ToXContent {
sb.append("], privileges=[").append(Strings.arrayToCommaDelimitedString(privileges));
sb.append("], fields=[").append(Strings.arrayToCommaDelimitedString(fields));
if (query != null) {
sb.append("], query=").append(query.toUtf8());
sb.append("], query=").append(query.utf8ToString());
}
sb.append("]");
return sb.toString();
@ -340,7 +339,7 @@ public class RoleDescriptor implements ToXContent {
builder.array("fields", fields);
}
if (query != null) {
builder.field("query", query.toUtf8());
builder.field("query", query.utf8ToString());
}
return builder.endObject();
}
@ -368,7 +367,7 @@ public class RoleDescriptor implements ToXContent {
out.writeStringArray(privileges);
if (query != null) {
out.writeBoolean(true);
out.writeByteArray(query.array());
out.writeByteArray(BytesReference.toBytes(query));
} else {
out.writeBoolean(false);
}

View File

@ -156,7 +156,7 @@ public final class FieldSubsetReader extends FilterLeafReader {
Tuple<XContentType, Map<String, Object>> result = XContentHelper.convertToMap(bytes, true);
Map<String, Object> transformedSource = XContentMapValues.filter(result.v2(), fieldNames, null);
XContentBuilder xContentBuilder = XContentBuilder.builder(result.v1().xContent()).map(transformedSource);
visitor.binaryField(fieldInfo, xContentBuilder.bytes().toBytes());
visitor.binaryField(fieldInfo, BytesReference.toBytes(xContentBuilder.bytes()));
} else {
visitor.binaryField(fieldInfo, value);
}

View File

@ -65,7 +65,7 @@ public class AnonymousUserTests extends ESTestCase {
BytesStreamOutput output = new BytesStreamOutput();
User.writeTo(user, output);
User anonymousSerialized = User.readFrom(new ByteBufferStreamInput(ByteBuffer.wrap(output.bytes().toBytes())));
User anonymousSerialized = User.readFrom(output.bytes().streamInput());
assertThat(AnonymousUser.is(anonymousSerialized), is(true));
// test with null anonymous

View File

@ -150,7 +150,7 @@ public class HttpResponse implements ToXContent {
sb.append("]");
}
if (hasContent()) {
sb.append(", body=[").append(body.toUtf8()).append("]");
sb.append(", body=[").append(body.utf8ToString()).append("]");
}
return sb.toString();
}
@ -168,7 +168,7 @@ public class HttpResponse implements ToXContent {
builder.endObject();
}
if (hasContent()) {
builder = builder.field(Field.BODY.getPreferredName(), body.toUtf8());
builder = builder.field(Field.BODY.getPreferredName(), body.utf8ToString());
}
builder.endObject();
return builder;

View File

@ -43,7 +43,7 @@ public class DefaultTextTemplateEngine extends AbstractComponent implements Text
ExecutableScript executable = service.executable(compiledScript, model);
Object result = executable.run();
if (result instanceof BytesReference) {
return ((BytesReference) result).toUtf8();
return ((BytesReference) result).utf8ToString();
}
return result.toString();
}

View File

@ -134,7 +134,7 @@ public class TextTemplate implements ToXContent {
} else {
contentType = parser.contentType();
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
template = builder.copyCurrentStructure(parser).bytes().toUtf8();
template = builder.copyCurrentStructure(parser).bytes().utf8ToString();
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.FILE)) {
type = ScriptType.FILE;
@ -171,7 +171,7 @@ public class TextTemplate implements ToXContent {
}
public static Builder inline(XContentBuilder template) {
return new Builder.Inline(template.bytes().toUtf8()).contentType(template.contentType());
return new Builder.Inline(template.bytes().utf8ToString()).contentType(template.contentType());
}
public static Builder<Builder.Inline> inline(String text) {

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.notification.email;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Provider;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -233,7 +234,7 @@ public abstract class Attachment extends BodyPartSource {
try {
XContentBuilder builder = XContentBuilder.builder(type.xContent()).prettyPrint();
content.toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes().toBytes();
return BytesReference.toBytes(builder.bytes());
} catch (IOException ioe) {
throw new ElasticsearchException("could not create an xcontent attachment [" + name + "]", ioe);
}

View File

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
@ -98,7 +99,8 @@ public class HttpEmailAttachementParser implements EmailAttachmentParser<HttpReq
if (response.hasContent()) {
String contentType = attachment.getContentType();
String attachmentContentType = Strings.hasLength(contentType) ? contentType : response.contentType();
return new Attachment.Bytes(attachment.id(), response.body().toBytes(), attachmentContentType, attachment.inline());
return new Attachment.Bytes(attachment.id(), BytesReference.toBytes(response.body()), attachmentContentType,
attachment.inline());
} else {
logger.error("Empty response body: [host[{}], port[{}], method[{}], path[{}]: response status [{}]", httpRequest.host(),
httpRequest.port(), httpRequest.method(), httpRequest.path(), response.status());

View File

@ -72,7 +72,7 @@ public class TemplateUtils {
*/
public static String filter(BytesReference source, String version, String versionProperty) {
return Pattern.compile(versionProperty)
.matcher(source.toUtf8())
.matcher(source.utf8ToString())
.replaceAll(version);
}

View File

@ -96,7 +96,7 @@ public class HttpClientTests extends ESTestCase {
assertThat(response.status(), equalTo(responseCode));
assertThat(response.body().toUtf8(), equalTo(body));
assertThat(response.body().utf8ToString(), equalTo(body));
assertThat(webServer.getRequestCount(), equalTo(1));
assertThat(recordedRequest.getBody().readString(StandardCharsets.UTF_8), equalTo(request.body()));
assertThat(recordedRequest.getPath().split("\\?")[0], equalTo(request.path()));
@ -112,7 +112,7 @@ public class HttpClientTests extends ESTestCase {
HttpResponse response = httpClient.execute(requestBuilder.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("body"));
assertThat(response.body().utf8ToString(), equalTo("body"));
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/test"));
@ -128,7 +128,7 @@ public class HttpClientTests extends ESTestCase {
HttpResponse response = httpClient.execute(requestBuilder.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("body"));
assertThat(response.body().utf8ToString(), equalTo("body"));
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/test?key=value+123%3A123"));
@ -144,7 +144,7 @@ public class HttpClientTests extends ESTestCase {
.body("body");
HttpResponse response = httpClient.execute(request.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("body"));
assertThat(response.body().utf8ToString(), equalTo("body"));
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/test"));
assertThat(recordedRequest.getHeader("Authorization"), equalTo("Basic dXNlcjpwYXNz"));
@ -190,7 +190,7 @@ public class HttpClientTests extends ESTestCase {
.body("body");
HttpResponse response = httpClient.execute(request.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("body"));
assertThat(response.body().utf8ToString(), equalTo("body"));
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/test"));
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("body"));
@ -221,7 +221,7 @@ public class HttpClientTests extends ESTestCase {
.body("body");
HttpResponse response = httpClient.execute(request.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("body"));
assertThat(response.body().utf8ToString(), equalTo("body"));
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/test"));
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("body"));
@ -287,7 +287,7 @@ public class HttpClientTests extends ESTestCase {
assertThat(response.status(), equalTo(statusCode));
assertThat(response.hasContent(), is(hasBody));
if (hasBody) {
assertThat(response.body().toUtf8(), is(body));
assertThat(response.body().utf8ToString(), is(body));
}
}
@ -341,7 +341,7 @@ public class HttpClientTests extends ESTestCase {
HttpResponse response = httpClient.execute(requestBuilder.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("fullProxiedContent"));
assertThat(response.body().utf8ToString(), equalTo("fullProxiedContent"));
// ensure we hit the proxyServer and not the webserver
assertThat(webServer.getRequestCount(), equalTo(0));
@ -370,7 +370,7 @@ public class HttpClientTests extends ESTestCase {
HttpResponse response = httpClient.execute(requestBuilder.build());
assertThat(response.status(), equalTo(200));
assertThat(response.body().toUtf8(), equalTo("fullProxiedContent"));
assertThat(response.body().utf8ToString(), equalTo("fullProxiedContent"));
// ensure we hit the proxyServer and not the webserver
assertThat(webServer.getRequestCount(), equalTo(0));

View File

@ -84,7 +84,7 @@ public class HttpResponseTests extends ESTestCase {
if (body == null) {
assertThat(parsedResponse.body(), nullValue());
} else {
assertThat(parsedResponse.body().toUtf8(), is(body));
assertThat(parsedResponse.body().utf8ToString(), is(body));
}
for (Map.Entry<String, String[]> headerEntry : headers.entrySet()) {
assertThat(headerEntry.getValue(), arrayContaining(parsedResponse.header(headerEntry.getKey())));

View File

@ -153,7 +153,7 @@ public class UserAccountTests extends ESTestCase {
}))
.build();
logger.info("expected (r1): {}", jsonBuilder().value(reqR1).bytes().toUtf8());
logger.info("expected (r1): {}", jsonBuilder().value(reqR1).bytes().utf8ToString());
HttpResponse resR1 = mock(HttpResponse.class);
when(resR1.status()).thenReturn(200);
@ -183,7 +183,7 @@ public class UserAccountTests extends ESTestCase {
}))
.build();
logger.info("expected (r2): {}", jsonBuilder().value(reqR1).bytes().toUtf8());
logger.info("expected (r2): {}", jsonBuilder().value(reqR1).bytes().utf8ToString());
HttpResponse resR2 = mock(HttpResponse.class);
when(resR2.status()).thenReturn(200);
@ -210,7 +210,7 @@ public class UserAccountTests extends ESTestCase {
}))
.build();
logger.info("expected (u1): {}", jsonBuilder().value(reqU1).bytes().toUtf8());
logger.info("expected (u1): {}", jsonBuilder().value(reqU1).bytes().utf8ToString());
HttpResponse resU1 = mock(HttpResponse.class);
when(resU1.status()).thenReturn(200);
@ -237,7 +237,7 @@ public class UserAccountTests extends ESTestCase {
}))
.build();
logger.info("expected (u2): {}", jsonBuilder().value(reqU2).bytes().toUtf8());
logger.info("expected (u2): {}", jsonBuilder().value(reqU2).bytes().utf8ToString());
HttpResponse resU2 = mock(HttpResponse.class);
when(resU2.status()).thenReturn(200);

View File

@ -132,7 +132,7 @@ public class V1AccountTests extends ESTestCase {
.toString())
.build();
logger.info("expected (r1): {}", jsonBuilder().value(req1).bytes().toUtf8());
logger.info("expected (r1): {}", jsonBuilder().value(req1).bytes().utf8ToString());
HttpResponse res1 = mock(HttpResponse.class);
when(res1.status()).thenReturn(200);
@ -155,7 +155,7 @@ public class V1AccountTests extends ESTestCase {
.toString())
.build();
logger.info("expected (r2): {}", jsonBuilder().value(req2).bytes().toUtf8());
logger.info("expected (r2): {}", jsonBuilder().value(req2).bytes().utf8ToString());
HttpResponse res2 = mock(HttpResponse.class);
when(res2.status()).thenReturn(200);

View File

@ -81,7 +81,7 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
parser = contentType.xContent().createParser(response.body());
} catch (Exception e) {
throw new ElasticsearchParseException("could not parse response body [{}] it does not appear to be [{}]", type(), ctx.id(),
response.body().toUtf8(), contentType.shortName());
response.body().utf8ToString(), contentType.shortName());
}
}
@ -92,7 +92,7 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
if (parser != null) {
payloadMap.putAll(parser.mapOrdered());
} else {
payloadMap.put("_value", response.body().toUtf8());
payloadMap.put("_value", response.body().utf8ToString());
}
}
if (headers.size() > 0) {

View File

@ -7,6 +7,8 @@ package org.elasticsearch.xpack.watcher.support;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import java.util.Arrays;
@ -35,10 +37,10 @@ public final class SearchRequestEquivalence {
try {
BytesStreamOutput output1 = new BytesStreamOutput();
r1.writeTo(output1);
byte[] bytes1 = output1.bytes().toBytes();
byte[] bytes1 = BytesReference.toBytes(output1.bytes());
output1.reset();
r2.writeTo(output1);
byte[] bytes2 = output1.bytes().toBytes();
byte[] bytes2 = BytesReference.toBytes(output1.bytes());
return Arrays.equals(bytes1, bytes2);
} catch (Throwable t) {
throw illegalState("could not compare search requests", t);

View File

@ -265,7 +265,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
private Watch parse(String id, boolean includeStatus, boolean withSecrets, BytesReference source, DateTime now) throws IOException {
if (logger.isTraceEnabled()) {
logger.trace("parsing watch [{}] ", source.toUtf8());
logger.trace("parsing watch [{}] ", source.utf8ToString());
}
XContentParser parser = null;
try {

View File

@ -210,7 +210,7 @@ public class WatchStore extends AbstractComponent {
IndexRequest createIndexRequest(String id, BytesReference source, long version) {
IndexRequest indexRequest = new IndexRequest(INDEX, DOC_TYPE, id);
indexRequest.source(source.toBytes());
indexRequest.source(BytesReference.toBytes(source));
indexRequest.version(version);
return indexRequest;
}

View File

@ -297,7 +297,7 @@ public class EmailActionTests extends ESTestCase {
builder.endObject();
BytesReference bytes = builder.bytes();
logger.info("email action json [{}]", bytes.toUtf8());
logger.info("email action json [{}]", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();
@ -392,7 +392,7 @@ public class EmailActionTests extends ESTestCase {
XContentBuilder builder = jsonBuilder();
executable.toXContent(builder, params);
BytesReference bytes = builder.bytes();
logger.info("{}", bytes.toUtf8());
logger.info("{}", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();

View File

@ -190,7 +190,7 @@ public class EmailAttachmentTests extends AbstractWatcherIntegrationTestCase {
.condition(compareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L))
.addAction("_email", emailAction(emailBuilder).setAuthentication(USERNAME, PASSWORD.toCharArray())
.setAttachments(emailAttachments));
logger.info("TMP WATCHSOURCE {}", watchSourceBuilder.build().getBytes().toUtf8());
logger.info("TMP WATCHSOURCE {}", watchSourceBuilder.build().getBytes().utf8ToString());
watcherClient.preparePutWatch("_test_id")
.setSource(watchSourceBuilder)

View File

@ -121,7 +121,7 @@ public class HipChatActionFactoryTests extends ESTestCase {
builder.endObject();
BytesReference bytes = builder.bytes();
logger.info("hipchat action json [{}]", bytes.toUtf8());
logger.info("hipchat action json [{}]", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();
@ -186,7 +186,7 @@ public class HipChatActionFactoryTests extends ESTestCase {
XContentBuilder jsonBuilder = jsonBuilder();
action.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
BytesReference bytes = builder.bytes();
logger.info("{}", bytes.toUtf8());
logger.info("{}", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();

View File

@ -171,7 +171,7 @@ public class HipChatActionTests extends ESTestCase {
builder.endObject();
BytesReference bytes = builder.bytes();
logger.info("hipchat action json [{}]", bytes.toUtf8());
logger.info("hipchat action json [{}]", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();
@ -236,7 +236,7 @@ public class HipChatActionTests extends ESTestCase {
XContentBuilder jsonBuilder = jsonBuilder();
action.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
BytesReference bytes = builder.bytes();
logger.info("{}", bytes.toUtf8());
logger.info("{}", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();

View File

@ -179,7 +179,7 @@ public class PagerDutyActionTests extends ESTestCase {
builder.endObject();
BytesReference bytes = builder.bytes();
logger.info("pagerduty action json [{}]", bytes.toUtf8());
logger.info("pagerduty action json [{}]", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();

View File

@ -156,7 +156,7 @@ public class SlackActionTests extends ESTestCase {
builder.endObject();
BytesReference bytes = builder.bytes();
logger.info("slack action json [{}]", bytes.toUtf8());
logger.info("slack action json [{}]", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();
@ -177,7 +177,7 @@ public class SlackActionTests extends ESTestCase {
XContentBuilder builder = jsonBuilder();
action.toXContent(builder, ToXContent.EMPTY_PARAMS);
BytesReference bytes = builder.bytes();
logger.info("{}", bytes.toUtf8());
logger.info("{}", bytes.utf8ToString());
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
parser.nextToken();

View File

@ -34,7 +34,7 @@ public class TriggeredWatchTests extends AbstractWatcherIntegrationTestCase {
XContentBuilder jsonBuilder2 = XContentFactory.jsonBuilder();
parsedTriggeredWatch.toXContent(jsonBuilder2, ToXContent.EMPTY_PARAMS);
assertThat(jsonBuilder.bytes().toUtf8(), equalTo(jsonBuilder2.bytes().toUtf8()));
assertThat(jsonBuilder.bytes().utf8ToString(), equalTo(jsonBuilder2.bytes().utf8ToString()));
}
private TriggeredWatch.Parser triggeredWatchParser() {

View File

@ -115,7 +115,7 @@ public class ChainInputTests extends ESTestCase {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
chainedInput.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertThat(builder.bytes().toUtf8(),
assertThat(builder.bytes().utf8ToString(),
is("{\"inputs\":[{\"first\":{\"simple\":{\"foo\":\"bar\"}}},{\"second\":{\"simple\":{\"spam\":\"eggs\"}}}]}"));
// parsing it back as well!

View File

@ -15,7 +15,6 @@ import org.elasticsearch.test.ESTestCase;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.smileBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.yamlBuilder;
import static org.hamcrest.Matchers.is;
/**
*
@ -34,6 +33,6 @@ public class XContentSourceTests extends ESTestCase {
XContentSource source = new XContentSource(bytes, builder.contentType());
XContentBuilder builder2 = XContentFactory.contentBuilder(builder.contentType());
BytesReference bytes2 = source.toXContent(builder2, ToXContent.EMPTY_PARAMS).bytes();
assertThat(bytes.array(), is(bytes2.array()));
assertEquals(bytes.toBytesRef(), bytes2.toBytesRef());
}
}

View File

@ -148,7 +148,7 @@ public class HttpSecretsIntegrationTests extends AbstractWatcherIntegrationTestC
// now lets execute the watch manually
webServer.enqueue(new MockResponse().setResponseCode(200).setBody(
jsonBuilder().startObject().field("key", "value").endObject().bytes().toUtf8()));
jsonBuilder().startObject().field("key", "value").endObject().bytes().utf8ToString()));
TriggerEvent triggerEvent = new ScheduleTriggerEvent(new DateTime(UTC), new DateTime(UTC));
ExecuteWatchResponse executeResponse = watcherClient.prepareExecuteWatch("_id")
@ -220,7 +220,7 @@ public class HttpSecretsIntegrationTests extends AbstractWatcherIntegrationTestC
// now lets execute the watch manually
webServer.enqueue(new MockResponse().setResponseCode(200).setBody(
jsonBuilder().startObject().field("key", "value").endObject().bytes().toUtf8()));
jsonBuilder().startObject().field("key", "value").endObject().bytes().utf8ToString()));
TriggerEvent triggerEvent = new ScheduleTriggerEvent(new DateTime(UTC), new DateTime(UTC));
ExecuteWatchResponse executeResponse = watcherClient.prepareExecuteWatch("_id")

View File

@ -207,7 +207,7 @@ public class WatchTests extends ESTestCase {
Watch watch = new Watch("_name", trigger, input, condition, transform, throttlePeriod, actions, metadata, watchStatus);
BytesReference bytes = XContentFactory.jsonBuilder().value(watch).bytes();
logger.info("{}", bytes.toUtf8());
logger.info("{}", bytes.utf8ToString());
Watch.Parser watchParser = new Watch.Parser(settings, conditionRegistry, triggerService, transformRegistry, actionRegistry,
inputRegistry, secretService, clock);