Merge pull request elastic/elasticsearch#1114 from jpountz/fix/safe_write_raw

Simplify xcontent usage.

Original commit: elastic/x-pack-elasticsearch@5e7eca08b9
This commit is contained in:
Adrien Grand 2015-12-11 12:17:15 +01:00
commit 295d250a9f
3 changed files with 15 additions and 18 deletions

View File

@ -49,7 +49,7 @@ public class LicenseSerializationTests extends ESTestCase {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(License.REST_VIEW_MODE, "true")));
builder.flush();
Map<String, Object> map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
Map<String, Object> map = XContentHelper.convertToMap(builder.bytes(), false).v2();
// should have an extra status field, human readable issue_data and expiry_date
assertThat(map.get("status"), notNullValue());
@ -59,14 +59,14 @@ public class LicenseSerializationTests extends ESTestCase {
builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.flush();
map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
map = XContentHelper.convertToMap(builder.bytes(), false).v2();
assertThat(map.get("status"), nullValue());
license = TestUtils.generateLicenses(new TestUtils.LicenseSpec(validLicenseIssueDate, validLicenseExpiryDate));
builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(License.REST_VIEW_MODE, "true")));
builder.flush();
map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
map = XContentHelper.convertToMap(builder.bytes(), false).v2();
// should have an extra status field, human readable issue_data and expiry_date
assertThat(map.get("status"), notNullValue());
@ -76,14 +76,14 @@ public class LicenseSerializationTests extends ESTestCase {
builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.flush();
map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
map = XContentHelper.convertToMap(builder.bytes(), false).v2();
assertThat(map.get("status"), nullValue());
license = TestUtils.generateLicenses(new TestUtils.LicenseSpec(invalidLicenseIssueDate, validLicenseExpiryDate));
builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(License.REST_VIEW_MODE, "true")));
builder.flush();
map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
map = XContentHelper.convertToMap(builder.bytes(), false).v2();
// should have an extra status field, human readable issue_data and expiry_date
assertThat(map.get("status"), notNullValue());
@ -93,7 +93,7 @@ public class LicenseSerializationTests extends ESTestCase {
builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.flush();
map = XContentHelper.convertToMap(builder.bytesStream().bytes(), false).v2();
map = XContentHelper.convertToMap(builder.bytes(), false).v2();
assertThat(map.get("status"), nullValue());
}
}

View File

@ -42,7 +42,7 @@ public class PutLicenseResponseTests extends ESTestCase {
response.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
Map<String, Object> map = XContentHelper.convertToMap(contentBuilder.bytesStream().bytes(), false).v2();
Map<String, Object> map = XContentHelper.convertToMap(contentBuilder.bytes(), false).v2();
assertThat(map.containsKey("acknowledged"), equalTo(true));
boolean actualAcknowledged = (boolean) map.get("acknowledged");
assertThat(actualAcknowledged, equalTo(acknowledged));

View File

@ -199,18 +199,15 @@ public class HttpExporter extends Exporter {
}
builder.endObject();
builder.endObject();
// Adds action metadata line bulk separator
builder.flush(); // Flush is needed here because the separator is written directly in the builder's stream
builder.stream().write(builder.contentType().xContent().streamSeparator());
// Render the MarvelDoc
renderer.render(marvelDoc, xContentType, out);
// Adds final bulk separator
builder.flush();
builder.stream().write(builder.contentType().xContent().streamSeparator());
}
// Adds action metadata line bulk separator
out.write(xContentType.xContent().streamSeparator());
// Render the MarvelDoc
renderer.render(marvelDoc, xContentType, out);
// Adds final bulk separator
out.write(xContentType.xContent().streamSeparator());
}
@SuppressWarnings("unchecked")