Remove BytesArray and BytesReference usage from XContentFactory (elastic/x-pack-elasticsearch#4158)

This is the x-pack side of https://github.com/elastic/elasticsearch/pull/29151

Original commit: elastic/x-pack-elasticsearch@be71338057
This commit is contained in:
Lee Hinman 2018-03-20 11:52:31 -06:00 committed by GitHub
parent 0a1e09c644
commit 418eefbcf1
10 changed files with 27 additions and 18 deletions

View File

@ -19,6 +19,7 @@ import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.xpack.core.ml.job.config.Job;
@ -337,7 +338,7 @@ public class ModelSnapshot implements ToXContentObject, Writeable {
public static ModelSnapshot fromJson(BytesReference bytesReference) {
try (InputStream stream = bytesReference.streamInput();
XContentParser parser = XContentFactory.xContent(bytesReference)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(bytesReference))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return PARSER.apply(parser, null).build();
} catch (IOException e) {

View File

@ -22,6 +22,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
@ -84,7 +85,7 @@ public class TransportGetFiltersAction extends HandledTransportAction<GetFilters
BytesReference docSource = getDocResponse.getSourceAsBytesRef();
try (InputStream stream = docSource.streamInput();
XContentParser parser =
XContentFactory.xContent(docSource)
XContentFactory.xContent(getDocResponse.getSourceAsBytes())
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
MlFilter filter = MlFilter.PARSER.apply(parser, null).build();
responseBody = new QueryPage<>(Collections.singletonList(filter), 1, MlFilter.RESULTS_FIELD);
@ -125,7 +126,7 @@ public class TransportGetFiltersAction extends HandledTransportAction<GetFilters
for (SearchHit hit : response.getHits().getHits()) {
BytesReference docSource = hit.getSourceRef();
try (InputStream stream = docSource.streamInput();
XContentParser parser = XContentFactory.xContent(docSource).createParser(
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(docSource)).createParser(
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
docs.add(MlFilter.PARSER.apply(parser, null).build());
} catch (IOException e) {

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.core.ml.job.results.Bucket;
@ -29,7 +30,7 @@ class BatchedBucketsIterator extends BatchedResultsIterator<Bucket> {
protected Result<Bucket> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source)).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)) {
Bucket bucket = Bucket.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), bucket);

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.core.ml.job.results.Influencer;
@ -28,7 +29,7 @@ class BatchedInfluencersIterator extends BatchedResultsIterator<Influencer> {
protected Result<Influencer> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source)).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)) {
Influencer influencer = Influencer.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), influencer);

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord;
@ -29,7 +30,7 @@ class BatchedRecordsIterator extends BatchedResultsIterator<AnomalyRecord> {
protected Result<AnomalyRecord> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source)).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)){
AnomalyRecord record = AnomalyRecord.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), record);

View File

@ -50,6 +50,7 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexNotFoundException;
@ -476,7 +477,7 @@ public class JobProvider {
Consumer<Exception> errorHandler) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return objectParser.apply(parser, null);
} catch (IOException e) {
@ -527,7 +528,7 @@ public class JobProvider {
for (SearchHit hit : hits.getHits()) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
Bucket bucket = Bucket.PARSER.apply(parser, null);
results.add(bucket);
@ -659,7 +660,7 @@ public class JobProvider {
for (SearchHit hit : hits) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
CategoryDefinition categoryDefinition = CategoryDefinition.PARSER.apply(parser, null);
results.add(categoryDefinition);
@ -694,7 +695,7 @@ public class JobProvider {
for (SearchHit hit : searchResponse.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
results.add(AnomalyRecord.PARSER.apply(parser, null));
} catch (IOException e) {
@ -743,7 +744,7 @@ public class JobProvider {
for (SearchHit hit : response.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
influencers.add(Influencer.PARSER.apply(parser, null));
} catch (IOException e) {
@ -888,7 +889,7 @@ public class JobProvider {
for (SearchHit hit : searchResponse.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
ModelPlot modelPlot = ModelPlot.PARSER.apply(parser, null);
results.add(modelPlot);
@ -1224,7 +1225,7 @@ public class JobProvider {
try (InputStream stream = docSource.streamInput();
XContentParser parser =
XContentFactory.xContent(docSource)
XContentFactory.xContent(XContentHelper.xContentType(docSource))
.createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)) {
Calendar calendar = Calendar.PARSER.apply(parser, null).build();

View File

@ -16,6 +16,7 @@ import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.ThreadPool;
@ -96,7 +97,7 @@ public class AuditorTests extends ESTestCase {
}
private AuditMessage parseAuditMessage(BytesReference msg) throws IOException {
XContentParser parser = XContentFactory.xContent(msg)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(msg))
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, msg.streamInput());
return AuditMessage.PARSER.apply(parser, null);
}

View File

@ -10,7 +10,7 @@ import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
@ -70,7 +70,7 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
XContentType responseContentType = response.xContentType();
if (input.getExpectedResponseXContentType() == null) {
//Attempt to auto detect content type, if not set in response
contentType = responseContentType != null ? responseContentType : XContentFactory.xContentType(response.body());
contentType = responseContentType != null ? responseContentType : XContentHelper.xContentType(response.body());
} else {
contentType = input.getExpectedResponseXContentType().contentType();
if (responseContentType != contentType) {

View File

@ -12,6 +12,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
@ -66,7 +67,7 @@ public class WatcherSearchTemplateService extends AbstractComponent {
BytesReference source = request.getSearchSource();
if (source != null && source.length() > 0) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source))
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) {
sourceBuilder.parseXContent(parser);
searchRequest.source(sourceBuilder);

View File

@ -14,6 +14,7 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xpack.core.watcher.actions.ActionStatus;
import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper;
@ -75,7 +76,7 @@ public final class WatcherTestUtils {
}
public static XContentSource xContentSource(BytesReference bytes) {
XContent xContent = XContentFactory.xContent(bytes);
XContent xContent = XContentFactory.xContent(XContentHelper.xContentType(bytes));
return new XContentSource(bytes, xContent.type());
}