Wrap stream passed to createParser in try-with-resources (elastic/x-pack-elasticsearch#4055)

This wraps the stream (`.streamInput()`) that is passed to many of the
`createParser` instances in the enclosing (or a new) try-with-resources block.
This ensures the `BytesReference.streamInput()` is closed.

Relates to elastic/x-pack-elasticsearch#28504

Original commit: elastic/x-pack-elasticsearch@7546e3b4d4
This commit is contained in:
Lee Hinman 2018-03-04 16:48:15 -07:00 committed by GitHub
parent 792fcccf46
commit 2147d217df
28 changed files with 193 additions and 135 deletions

View File

@ -6,6 +6,7 @@
package org.elasticsearch.license;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Base64;
@ -497,8 +498,10 @@ public class License implements ToXContentObject {
throw new ElasticsearchParseException("failed to parse license - no content-type provided");
}
// EMPTY is safe here because we don't call namedObject
final XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes.streamInput());
try (InputStream byteStream = bytes.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, byteStream))
{
License license = null;
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
if (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
@ -536,6 +539,7 @@ public class License implements ToXContentObject {
}
return license;
}
}
@Override
public boolean equals(Object o) {

View File

@ -22,6 +22,7 @@ import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
public class PreviewDatafeedAction extends Action<PreviewDatafeedAction.Request, PreviewDatafeedAction.Response,
@ -138,7 +139,9 @@ public class PreviewDatafeedAction extends Action<PreviewDatafeedAction.Request,
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.rawValue(preview.streamInput(), XContentType.JSON);
try (InputStream stream = preview.streamInput()) {
builder.rawValue(stream, XContentType.JSON);
}
return builder;
}

View File

@ -24,6 +24,7 @@ import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.utils.time.TimeUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -308,8 +309,9 @@ public class ModelSnapshot implements ToXContentObject, Writeable {
}
public static ModelSnapshot fromJson(BytesReference bytesReference) {
try (XContentParser parser = XContentFactory.xContent(bytesReference)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytesReference.streamInput())) {
try (InputStream stream = bytesReference.streamInput();
XContentParser parser = XContentFactory.xContent(bytesReference)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return PARSER.apply(parser, null).build();
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse modelSnapshot", e);

View File

@ -22,6 +22,7 @@ import org.elasticsearch.xpack.core.security.user.User;
import org.elasticsearch.xpack.core.security.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.CharBuffer;
/**
@ -66,8 +67,9 @@ public class ChangePasswordRequestBuilder
*/
public ChangePasswordRequestBuilder source(BytesReference source, XContentType xContentType) throws IOException {
// EMPTY is ok here because we never call namedObject
try (XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
XContentUtils.verifyObject(parser);
XContentParser.Token token;
String currentFieldName = null;

View File

@ -25,6 +25,7 @@ import org.elasticsearch.xpack.core.security.user.User;
import org.elasticsearch.xpack.core.security.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
@ -99,8 +100,9 @@ public class PutUserRequestBuilder extends ActionRequestBuilder<PutUserRequest,
Objects.requireNonNull(xContentType);
username(username);
// EMPTY is ok here because we never call namedObject
try (XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
XContentUtils.verifyObject(parser);
XContentParser.Token token;
String currentFieldName = null;

View File

@ -24,6 +24,7 @@ import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -144,8 +145,9 @@ public class ExpressionRoleMapping implements ToXContentObject, Writeable {
*/
public static ExpressionRoleMapping parse(String name, BytesReference source, XContentType xContentType) throws IOException {
final NamedXContentRegistry registry = NamedXContentRegistry.EMPTY;
try (XContentParser parser = xContentType.xContent()
.createParser(registry, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) {
return parse(name, parser);
}
}

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -54,7 +55,9 @@ public final class ExpressionParser {
* @param content The XContent (typically JSON) DSL representation of the expression
*/
public RoleMapperExpression parse(String name, XContentSource content) throws IOException {
return parse(name, content.parser(NamedXContentRegistry.EMPTY));
try (InputStream stream = content.getBytes().streamInput()) {
return parse(name, content.parser(NamedXContentRegistry.EMPTY, stream));
}
}
/**

View File

@ -29,6 +29,7 @@ import org.elasticsearch.xpack.core.security.support.Validation;
import org.elasticsearch.xpack.core.security.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -219,8 +220,9 @@ public class RoleDescriptor implements ToXContentObject {
throws IOException {
assert name != null;
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return parse(name, parser, allow2xFormat);
}
}
@ -288,8 +290,9 @@ public class RoleDescriptor implements ToXContentObject {
public static RoleDescriptor parsePrivilegesCheck(String description, BytesReference source, XContentType xContentType)
throws IOException {
try (XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
// advance to the START_OBJECT token
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {

View File

@ -20,6 +20,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.watcher.common.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@ -98,15 +99,16 @@ public class XContentSource implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = parser(NamedXContentRegistry.EMPTY)) {
try (InputStream stream = bytes.streamInput();
XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) {
parser.nextToken();
XContentHelper.copyCurrentStructure(builder.generator(), parser);
return builder;
}
}
public XContentParser parser(NamedXContentRegistry xContentRegistry) throws IOException {
return contentType.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, bytes.streamInput());
public XContentParser parser(NamedXContentRegistry xContentRegistry, InputStream stream) throws IOException {
return contentType.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream);
}
public static XContentSource readFrom(StreamInput in) throws IOException {
@ -121,7 +123,8 @@ public class XContentSource implements ToXContent {
private Object data() {
if (data == null) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = parser(NamedXContentRegistry.EMPTY)) {
try (InputStream stream = bytes.streamInput();
XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) {
data = XContentUtils.readValue(parser, parser.nextToken());
} catch (IOException ex) {
throw new ElasticsearchException("failed to read value", ex);

View File

@ -36,6 +36,7 @@ import org.elasticsearch.xpack.core.ml.job.config.MlFilter;
import org.elasticsearch.xpack.core.ml.utils.MlIndicesUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -81,15 +82,16 @@ public class TransportGetFiltersAction extends HandledTransportAction<GetFilters
QueryPage<MlFilter> responseBody;
if (getDocResponse.isExists()) {
BytesReference docSource = getDocResponse.getSourceAsBytesRef();
try (InputStream stream = docSource.streamInput();
XContentParser parser =
XContentFactory.xContent(docSource)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
docSource.streamInput());
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
MlFilter filter = MlFilter.PARSER.apply(parser, null).build();
responseBody = new QueryPage<>(Collections.singletonList(filter), 1, MlFilter.RESULTS_FIELD);
GetFiltersAction.Response filterResponse = new GetFiltersAction.Response(responseBody);
listener.onResponse(filterResponse);
}
} else {
this.onFailure(QueryPage.emptyQueryPage(MlFilter.RESULTS_FIELD));
}
@ -122,8 +124,9 @@ public class TransportGetFiltersAction extends HandledTransportAction<GetFilters
List<MlFilter> docs = new ArrayList<>();
for (SearchHit hit : response.getHits().getHits()) {
BytesReference docSource = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(docSource).createParser(
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, docSource.streamInput())) {
try (InputStream stream = docSource.streamInput();
XContentParser parser = XContentFactory.xContent(docSource).createParser(
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
docs.add(MlFilter.PARSER.apply(parser, null).build());
} catch (IOException e) {
this.onFailure(e);

View File

@ -17,6 +17,7 @@ import org.elasticsearch.xpack.core.ml.job.results.Bucket;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import java.io.IOException;
import java.io.InputStream;
class BatchedBucketsIterator extends BatchedResultsIterator<Bucket> {
@ -27,14 +28,13 @@ class BatchedBucketsIterator extends BatchedResultsIterator<Bucket> {
@Override
protected Result<Bucket> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
XContentParser parser;
try {
parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, source.streamInput());
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)) {
Bucket bucket = Bucket.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), bucket);
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse bucket", e);
}
Bucket bucket = Bucket.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), bucket);
}
}

View File

@ -17,6 +17,7 @@ import org.elasticsearch.xpack.core.ml.job.results.Influencer;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import java.io.IOException;
import java.io.InputStream;
class BatchedInfluencersIterator extends BatchedResultsIterator<Influencer> {
BatchedInfluencersIterator(Client client, String jobId) {
@ -26,15 +27,13 @@ class BatchedInfluencersIterator extends BatchedResultsIterator<Influencer> {
@Override
protected Result<Influencer> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
XContentParser parser;
try {
parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, source.streamInput());
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)) {
Influencer influencer = Influencer.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), influencer);
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parser influencer", e);
}
Influencer influencer = Influencer.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), influencer);
}
}

View File

@ -17,6 +17,7 @@ import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import java.io.IOException;
import java.io.InputStream;
class BatchedRecordsIterator extends BatchedResultsIterator<AnomalyRecord> {
@ -27,14 +28,13 @@ class BatchedRecordsIterator extends BatchedResultsIterator<AnomalyRecord> {
@Override
protected Result<AnomalyRecord> map(SearchHit hit) {
BytesReference source = hit.getSourceRef();
XContentParser parser;
try {
parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, source.streamInput());
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream)){
AnomalyRecord record = AnomalyRecord.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), record);
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse record", e);
}
AnomalyRecord record = AnomalyRecord.PARSER.apply(parser, null);
return new Result<>(hit.getIndex(), record);
}
}

View File

@ -101,6 +101,7 @@ import org.elasticsearch.xpack.core.ml.utils.MlIndicesUtils;
import org.elasticsearch.xpack.core.security.support.Exceptions;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -474,8 +475,9 @@ public class JobProvider {
private <T, U> T parseSearchHit(SearchHit hit, BiFunction<XContentParser, U, T> objectParser,
Consumer<Exception> errorHandler) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return objectParser.apply(parser, null);
} catch (IOException e) {
errorHandler.accept(new ElasticsearchParseException("failed to parse " + hit.getType(), e));
@ -487,8 +489,9 @@ public class JobProvider {
Consumer<Exception> errorHandler) {
BytesReference source = getResponse.getSourceAsBytesRef();
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return objectParser.apply(parser, null);
} catch (IOException e) {
errorHandler.accept(new ElasticsearchParseException("failed to parse " + getResponse.getType(), e));
@ -523,8 +526,9 @@ public class JobProvider {
List<Bucket> results = new ArrayList<>();
for (SearchHit hit : hits.getHits()) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
Bucket bucket = Bucket.PARSER.apply(parser, null);
results.add(bucket);
} catch (IOException e) {
@ -654,8 +658,9 @@ public class JobProvider {
List<CategoryDefinition> results = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
CategoryDefinition categoryDefinition = CategoryDefinition.PARSER.apply(parser, null);
results.add(categoryDefinition);
} catch (IOException e) {
@ -688,8 +693,9 @@ public class JobProvider {
List<AnomalyRecord> results = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
results.add(AnomalyRecord.PARSER.apply(parser, null));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse records", e);
@ -736,8 +742,9 @@ public class JobProvider {
List<Influencer> influencers = new ArrayList<>();
for (SearchHit hit : response.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
influencers.add(Influencer.PARSER.apply(parser, null));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse influencer", e);
@ -880,8 +887,9 @@ public class JobProvider {
for (SearchHit hit : searchResponse.getHits().getHits()) {
BytesReference source = hit.getSourceRef();
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
ModelPlot modelPlot = ModelPlot.PARSER.apply(parser, null);
results.add(modelPlot);
} catch (IOException e) {
@ -1214,10 +1222,11 @@ public class JobProvider {
if (getDocResponse.isExists()) {
BytesReference docSource = getDocResponse.getSourceAsBytesRef();
try (XContentParser parser =
try (InputStream stream = docSource.streamInput();
XContentParser parser =
XContentFactory.xContent(docSource)
.createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, docSource.streamInput())) {
LoggingDeprecationHandler.INSTANCE, stream)) {
Calendar calendar = Calendar.PARSER.apply(parser, null).build();
listener.onResponse(calendar);
}

View File

@ -220,9 +220,9 @@ public class CppLogMessageHandler implements Closeable {
}
private void parseMessage(XContent xContent, BytesReference bytesRef) {
try {
try (InputStream stream = bytesRef.streamInput();
XContentParser parser = xContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytesRef.streamInput());
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
CppLogMessage msg = CppLogMessage.PARSER.apply(parser, null);
Level level = Level.getLevel(msg.getLevel());
if (level == null) {

View File

@ -80,11 +80,13 @@ public class NormalizerResultHandler extends AbstractComponent {
}
private void parseResult(XContent xContent, BytesReference bytesRef) throws IOException {
try (InputStream stream = bytesRef.streamInput();
XContentParser parser = xContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytesRef.streamInput());
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
NormalizerResult result = NormalizerResult.PARSER.apply(parser, null);
normalizedResults.add(result);
}
}
private static int findNextMarker(byte marker, BytesReference bytesRef, int from) {
for (int i = from; i < bytesRef.length(); ++i) {

View File

@ -36,6 +36,7 @@ import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -117,13 +118,15 @@ public class ExpiredForecastsRemover implements MlDataRemover {
}
for (SearchHit hit : hits.getHits()) {
try (InputStream stream = hit.getSourceRef().streamInput();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, hit.getSourceRef().streamInput());
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
ForecastRequestStats forecastRequestStats = ForecastRequestStats.PARSER.apply(parser, null);
if (forecastRequestStats.getExpiryTime().toEpochMilli() < cutoffEpochMs) {
forecastsToDelete.add(forecastRequestStats);
}
}
}
return forecastsToDelete;
}

View File

@ -16,6 +16,7 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import static org.elasticsearch.common.xcontent.NamedXContentRegistry.EMPTY;
@ -60,7 +61,8 @@ public abstract class FilteredMonitoringDoc extends MonitoringDoc {
try (XContentBuilder filteredBuilder = new XContentBuilder(xContent, out, filters)) {
super.toXContent(filteredBuilder, params);
}
try (XContentParser parser = xContent.createParser(EMPTY, LoggingDeprecationHandler.INSTANCE, out.bytes().streamInput())) {
try (InputStream stream = out.bytes().streamInput();
XContentParser parser = xContent.createParser(EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return builder.copyCurrentStructure(parser);
}
}

View File

@ -9,6 +9,7 @@ package org.elasticsearch.xpack.rollup;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
@ -588,8 +589,9 @@ public class RollupRequestTranslator {
try {
output.writeString(metric.getType());
metric.writeTo(output);
try (NamedWriteableAwareStreamInput in =
new NamedWriteableAwareStreamInput(output.bytes().streamInput(), registry)) {
try (StreamInput stream = output.bytes().streamInput();
NamedWriteableAwareStreamInput in =
new NamedWriteableAwareStreamInput(stream, registry)) {
ValuesSourceAggregationBuilder serialized
= ((ValuesSourceAggregationBuilder)in.readNamedWriteable(AggregationBuilder.class))

View File

@ -40,6 +40,7 @@ import org.elasticsearch.xpack.security.authc.support.CachingUsernamePasswordRea
import org.elasticsearch.xpack.security.authc.support.UserRoleMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -149,7 +150,9 @@ public class NativeRoleMappingStore extends AbstractComponent implements UserRol
}
private ExpressionRoleMapping buildMapping(String id, BytesReference source) {
try (XContentParser parser = getParser(source)) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return ExpressionRoleMapping.parse(id, parser);
} catch (Exception e) {
logger.warn(new ParameterizedMessage("Role mapping [{}] cannot be parsed and will be skipped", id), e);
@ -157,11 +160,6 @@ public class NativeRoleMappingStore extends AbstractComponent implements UserRol
}
}
private static XContentParser getParser(BytesReference source) throws IOException {
return XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source.streamInput());
}
/**
* Stores (create or update) a single mapping in the index
*/

View File

@ -157,8 +157,9 @@ public class HttpClient {
private <Response> Response fromXContent(XContentType xContentType, BytesReference bytesReference,
CheckedFunction<XContentParser, Response, IOException> responseParser) {
try (XContentParser parser = xContentType.xContent().createParser(registry,
LoggingDeprecationHandler.INSTANCE, bytesReference.streamInput())) {
try (InputStream stream = bytesReference.streamInput();
XContentParser parser = xContentType.xContent().createParser(registry,
LoggingDeprecationHandler.INSTANCE, stream)) {
return responseParser.apply(parser);
} catch (IOException ex) {
throw new ClientException("Cannot parse response", ex);

View File

@ -23,6 +23,7 @@ import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.support.Variables;
import org.elasticsearch.xpack.watcher.support.XContentFilterKeysUtils;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -80,8 +81,9 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
if (contentType != null) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = contentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.body().streamInput())) {
try (InputStream stream = response.body().streamInput();
XContentParser parser = contentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
if (input.getExtractKeys() != null) {
payloadMap.putAll(XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser));
} else {

View File

@ -36,6 +36,7 @@ import org.elasticsearch.xpack.watcher.notification.email.Attachment;
import org.elasticsearch.xpack.watcher.support.Variables;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Map;
@ -201,8 +202,9 @@ public class ReportingAttachmentParser implements EmailAttachmentParser<Reportin
*/
private String extractIdFromJson(String watchId, String attachmentId, BytesReference body) throws IOException {
// EMPTY is safe here becaus we never call namedObject
try (XContentParser parser = JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, body.streamInput())) {
try (InputStream stream = body.streamInput();
XContentParser parser = JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
KibanaReportingPayload payload = new KibanaReportingPayload();
PAYLOAD_PARSER.parse(parser, payload, null);
String path = payload.getPath();

View File

@ -14,6 +14,7 @@ import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.watcher.common.http.HttpClient;
import org.elasticsearch.xpack.watcher.common.http.HttpMethod;
@ -24,6 +25,7 @@ import org.elasticsearch.xpack.watcher.common.http.Scheme;
import org.elasticsearch.xpack.watcher.common.http.auth.basic.BasicAuth;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -79,9 +81,11 @@ public class JiraAccount {
builder.startObject();
settings.getAsSettings(ISSUE_DEFAULTS_SETTING).toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
this.issueDefaults = Collections.unmodifiableMap(XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(Collections.emptyList()),
LoggingDeprecationHandler.INSTANCE, builder.bytes().streamInput()).map());
try (InputStream stream = builder.bytes().streamInput();
XContentParser parser = XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(Collections.emptyList()), LoggingDeprecationHandler.INSTANCE, stream)) {
this.issueDefaults = Collections.unmodifiableMap(parser.map());
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}

View File

@ -20,6 +20,7 @@ import org.elasticsearch.xpack.watcher.common.http.HttpResponse;
import org.elasticsearch.xpack.watcher.actions.jira.JiraAction;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -148,8 +149,9 @@ public class JiraIssue implements ToXContentObject {
if (response.hasContent()) {
final List<String> errors = new ArrayList<>();
// EMPTY is safe here because we never call namedObject
try (XContentParser parser = JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.body().streamInput())) {
try (InputStream stream = response.body().streamInput();
XContentParser parser = JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
XContentParser.Token token = parser.currentToken();
if (token == null) {
token = parser.nextToken();

View File

@ -19,6 +19,7 @@ import org.elasticsearch.xpack.watcher.common.http.HttpResponse;
import org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyAction;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -102,10 +103,10 @@ public class SentEvent implements ToXContentObject {
// lets first try to parse the error response in the body
// based on https://developer.pagerduty.com/documentation/rest/errors
try {
// EMPTY is safe here because we never call namedObject
try (InputStream stream = response.body().streamInput();
XContentParser parser = JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.body().streamInput());
// EMPTY is safe here because we never call namedObject
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
parser.nextToken();
String message = null;

View File

@ -24,6 +24,7 @@ import org.elasticsearch.xpack.watcher.Watcher;
import org.elasticsearch.xpack.watcher.support.Variables;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
/**
@ -64,8 +65,9 @@ public class WatcherSearchTemplateService extends AbstractComponent {
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
BytesReference source = request.getSearchSource();
if (source != null && source.length() > 0) {
try (XContentParser parser = XContentFactory.xContent(source)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source.streamInput())) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(source)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) {
sourceBuilder.parseXContent(parser);
searchRequest.source(sourceBuilder);
}

View File

@ -37,6 +37,7 @@ import org.elasticsearch.xpack.watcher.trigger.TriggerService;
import org.joda.time.DateTime;
import java.io.IOException;
import java.io.InputStream;
import java.time.Clock;
import java.util.Collections;
import java.util.HashMap;
@ -108,8 +109,9 @@ public class WatchParser extends AbstractComponent {
logger.trace("parsing watch [{}] ", source.utf8ToString());
}
// EMPTY is safe here because we never use namedObject
try (WatcherXContentParser parser = new WatcherXContentParser(xContentType.xContent().createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, source.streamInput()),
try (InputStream stream = source.streamInput();
WatcherXContentParser parser = new WatcherXContentParser(xContentType.xContent().createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, stream),
now, withSecrets ? cryptoService : null, allowRedactedPasswords)) {
parser.nextToken();
return parse(id, includeStatus, parser);