From 158a6b5588d0f8c62fd2ee5c243ce52bcf5ec86c Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 1 Jul 2016 16:09:53 +0200 Subject: [PATCH] Cleanup BytesRefrence interface (elastic/elasticsearch#2670) This is a followup of elastic/elasticsearchelastic/elasticsearch#19196 Original commit: elastic/x-pack-elasticsearch@1d0398e89a4d76df7e3b8b08330fd9c71f22a37c --- .../license/core/LicenseVerifier.java | 8 ++- .../license/licensor/LicenseSigner.java | 9 +++- .../xpack/security/MigrateToolIT.java | 2 +- .../license/plugin/core/LicensesMetaData.java | 3 +- .../license/plugin/core/TrialLicense.java | 3 +- .../plugin/rest/RestPutLicenseAction.java | 2 +- .../plugin/PutLicenseResponseTests.java | 5 +- .../license/plugin/TrialLicenseTests.java | 3 +- .../LicensesMetaDataSerializationTests.java | 53 +++++++++---------- .../agent/exporter/http/HttpExporter.java | 7 ++- .../action/MonitoringIndexTests.java | 2 +- .../http/HttpExporterTemplateTests.java | 2 +- .../exporter/http/HttpExporterTests.java | 3 +- .../action/user/ChangePasswordRequest.java | 3 +- .../security/action/user/PutUserRequest.java | 2 +- .../xpack/security/audit/AuditUtil.java | 2 +- .../xpack/security/authc/Authentication.java | 3 +- .../xpack/security/authz/RoleDescriptor.java | 7 ++- .../accesscontrol/FieldSubsetReader.java | 2 +- .../security/user/AnonymousUserTests.java | 2 +- .../xpack/common/http/HttpResponse.java | 4 +- .../text/DefaultTextTemplateEngine.java | 2 +- .../xpack/common/text/TextTemplate.java | 4 +- .../xpack/notification/email/Attachment.java | 3 +- .../HttpEmailAttachementParser.java | 4 +- .../xpack/template/TemplateUtils.java | 2 +- .../xpack/common/http/HttpClientTests.java | 18 +++---- .../xpack/common/http/HttpResponseTests.java | 2 +- .../hipchat/UserAccountTests.java | 8 +-- .../notification/hipchat/V1AccountTests.java | 4 +- .../input/http/ExecutableHttpInput.java | 4 +- .../support/SearchRequestEquivalence.java | 6 ++- .../xpack/watcher/watch/Watch.java | 2 +- .../xpack/watcher/watch/WatchStore.java | 2 +- .../actions/email/EmailActionTests.java | 4 +- .../actions/email/EmailAttachmentTests.java | 2 +- .../hipchat/HipChatActionFactoryTests.java | 4 +- .../actions/hipchat/HipChatActionTests.java | 4 +- .../pagerduty/PagerDutyActionTests.java | 2 +- .../actions/slack/SlackActionTests.java | 4 +- .../execution/TriggeredWatchTests.java | 2 +- .../watcher/input/chain/ChainInputTests.java | 2 +- .../support/xcontent/XContentSourceTests.java | 3 +- .../HttpSecretsIntegrationTests.java | 4 +- .../xpack/watcher/watch/WatchTests.java | 2 +- 45 files changed, 120 insertions(+), 101 deletions(-) diff --git a/elasticsearch/license/base/src/main/java/org/elasticsearch/license/core/LicenseVerifier.java b/elasticsearch/license/base/src/main/java/org/elasticsearch/license/core/LicenseVerifier.java index 1bef3dc58e7..13f34945542 100644 --- a/elasticsearch/license/base/src/main/java/org/elasticsearch/license/core/LicenseVerifier.java +++ b/elasticsearch/license/base/src/main/java/org/elasticsearch/license/core/LicenseVerifier.java @@ -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) { diff --git a/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java b/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java index 915c9ba86dc..34c6f2c4393 100644 --- a/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java +++ b/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java @@ -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); diff --git a/elasticsearch/qa/security-migrate-tests/src/test/java/org/elasticsearch/xpack/security/MigrateToolIT.java b/elasticsearch/qa/security-migrate-tests/src/test/java/org/elasticsearch/xpack/security/MigrateToolIT.java index 6052236c431..12cad15d5e6 100644 --- a/elasticsearch/qa/security-migrate-tests/src/test/java/org/elasticsearch/xpack/security/MigrateToolIT.java +++ b/elasticsearch/qa/security-migrate-tests/src/test/java/org/elasticsearch/xpack/security/MigrateToolIT.java @@ -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"}); diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java index de8dc139750..cc1dabb24bb 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java @@ -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 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) { diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/TrialLicense.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/TrialLicense.java index 814e220cd7b..7f71d81d885 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/TrialLicense.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/TrialLicense.java @@ -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 diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestPutLicenseAction.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestPutLicenseAction.java index d8fde68aad8..80156b71497 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestPutLicenseAction.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestPutLicenseAction.java @@ -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(channel) { diff --git a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/PutLicenseResponseTests.java b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/PutLicenseResponseTests.java index 16a180e8ae8..13d6d8b04fc 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/PutLicenseResponseTests.java +++ b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/PutLicenseResponseTests.java @@ -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); diff --git a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/TrialLicenseTests.java b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/TrialLicenseTests.java index d520ae0e3d7..26c34df6483 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/TrialLicenseTests.java +++ b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/TrialLicenseTests.java @@ -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()) diff --git a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java index 5ca36015575..13337e5e29a 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java +++ b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java @@ -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" diff --git a/elasticsearch/x-pack/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporter.java b/elasticsearch/x-pack/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporter.java index 38e359ea66c..3237ef5f960 100644 --- a/elasticsearch/x-pack/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporter.java +++ b/elasticsearch/x-pack/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporter.java @@ -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(); } diff --git a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringIndexTests.java b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringIndexTests.java index edd88e477dc..c88c4a8252c 100644 --- a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringIndexTests.java +++ b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringIndexTests.java @@ -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)); diff --git a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTemplateTests.java b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTemplateTests.java index 1264f243021..fc44071fbd8 100644 --- a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTemplateTests.java +++ b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTemplateTests.java @@ -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())); diff --git a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTests.java b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTests.java index 38d9ee1d54a..b9ae02f14a8 100644 --- a/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTests.java +++ b/elasticsearch/x-pack/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/agent/exporter/http/HttpExporterTests.java @@ -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 { diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequest.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequest.java index dcf3e30ec83..8ad5cf72ff6 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequest.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequest.java @@ -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 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); } diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/PutUserRequest.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/PutUserRequest.java index bd7bd2f11c6..74394882963 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/PutUserRequest.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/action/user/PutUserRequest.java @@ -127,7 +127,7 @@ public class PutUserRequest extends ActionRequest 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(); diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java index f19416f8001..7993927b758 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java @@ -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 ""; diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authc/Authentication.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authc/Authentication.java index b486b6ac191..fe83bff0092 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authc/Authentication.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authc/Authentication.java @@ -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 { diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/RoleDescriptor.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/RoleDescriptor.java index 46cfa97c6aa..4d287e61204 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/RoleDescriptor.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/RoleDescriptor.java @@ -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); } diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java index 145f1365201..ea454c5b0ef 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java @@ -156,7 +156,7 @@ public final class FieldSubsetReader extends FilterLeafReader { Tuple> result = XContentHelper.convertToMap(bytes, true); Map 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); } diff --git a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java index 837e195e6f8..f801ed792f3 100644 --- a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java +++ b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java @@ -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 diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpResponse.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpResponse.java index ca469cba782..ba6bffc06ed 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpResponse.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpResponse.java @@ -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; diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/DefaultTextTemplateEngine.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/DefaultTextTemplateEngine.java index 1fc734c9c57..a9acedd2938 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/DefaultTextTemplateEngine.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/DefaultTextTemplateEngine.java @@ -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(); } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/TextTemplate.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/TextTemplate.java index 92077527552..9324efda640 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/TextTemplate.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/text/TextTemplate.java @@ -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 inline(String text) { diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/Attachment.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/Attachment.java index 34b848bc55d..d5dfa7f4469 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/Attachment.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/Attachment.java @@ -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); } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/attachment/HttpEmailAttachementParser.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/attachment/HttpEmailAttachementParser.java index d98ade2cf4e..e0edeaefd93 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/attachment/HttpEmailAttachementParser.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/notification/email/attachment/HttpEmailAttachementParser.java @@ -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 headerEntry : headers.entrySet()) { assertThat(headerEntry.getValue(), arrayContaining(parsedResponse.header(headerEntry.getKey()))); diff --git a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java index 8f1038f376a..0b1272a9172 100644 --- a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java +++ b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java @@ -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); diff --git a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/V1AccountTests.java b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/V1AccountTests.java index 2b6630a9cd7..58bcb7a85bd 100644 --- a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/V1AccountTests.java +++ b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/notification/hipchat/V1AccountTests.java @@ -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); diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java index 6c53c8c472e..e905fc2f4ee 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java @@ -81,7 +81,7 @@ public class ExecutableHttpInput extends ExecutableInput 0) { diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/SearchRequestEquivalence.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/SearchRequestEquivalence.java index 9b7a76a3356..fec055628f1 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/SearchRequestEquivalence.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/SearchRequestEquivalence.java @@ -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); diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/Watch.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/Watch.java index c6a7073e34e..2da2cb98143 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/Watch.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/Watch.java @@ -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 { diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchStore.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchStore.java index bae3c4e93eb..eb3a53a8bcd 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchStore.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchStore.java @@ -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; } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java index 538fdfb00f0..06446895ee8 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java @@ -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(); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java index 107c71b1842..a190ba965a3 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java @@ -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) diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java index f6cae159f14..01158e8ebf2 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java @@ -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(); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionTests.java index 6cbc0d932e7..2437045e550 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionTests.java @@ -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(); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java index e848c29212c..f85cc709030 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java @@ -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(); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java index 39f3796e1bb..7ad499dbc47 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java @@ -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(); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchTests.java index 1185e3def78..a280c376c05 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchTests.java @@ -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() { diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java index 0e582856127..0735ce5806e 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java @@ -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! diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java index 0009a8007be..e9bf9b71236 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java @@ -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()); } } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java index cd244e7a890..3cf46f318f3 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java @@ -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") diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java index ea4a09715d2..0b10ed8dcb8 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java @@ -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);