Merge pull request #11846 from jpountz/fix/parser_map_and_close

Remove XContentParser.map[Ordered]AndClose().
This commit is contained in:
Adrien Grand 2015-06-24 11:33:33 +02:00
commit 028f31b7d6
30 changed files with 179 additions and 101 deletions

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.cluster.repositories.put; package org.elasticsearch.action.admin.cluster.repositories.put;
import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -29,6 +28,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -231,8 +231,8 @@ public class PutRepositoryRequest extends AcknowledgedRequest<PutRepositoryReque
* @param repositoryDefinition repository definition * @param repositoryDefinition repository definition
*/ */
public PutRepositoryRequest source(String repositoryDefinition) { public PutRepositoryRequest source(String repositoryDefinition) {
try { try (XContentParser parser = XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition)) {
return source(XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse repository source [" + repositoryDefinition + "]", e); throw new IllegalArgumentException("failed to parse repository source [" + repositoryDefinition + "]", e);
} }
@ -255,8 +255,8 @@ public class PutRepositoryRequest extends AcknowledgedRequest<PutRepositoryReque
* @param repositoryDefinition repository definition * @param repositoryDefinition repository definition
*/ */
public PutRepositoryRequest source(byte[] repositoryDefinition, int offset, int length) { public PutRepositoryRequest source(byte[] repositoryDefinition, int offset, int length) {
try { try (XContentParser parser = XContentFactory.xContent(repositoryDefinition, offset, length).createParser(repositoryDefinition, offset, length)) {
return source(XContentFactory.xContent(repositoryDefinition, offset, length).createParser(repositoryDefinition, offset, length).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse repository source", e); throw new IllegalArgumentException("failed to parse repository source", e);
} }
@ -269,8 +269,8 @@ public class PutRepositoryRequest extends AcknowledgedRequest<PutRepositoryReque
* @param repositoryDefinition repository definition * @param repositoryDefinition repository definition
*/ */
public PutRepositoryRequest source(BytesReference repositoryDefinition) { public PutRepositoryRequest source(BytesReference repositoryDefinition) {
try { try (XContentParser parser = XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition)) {
return source(XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse template source", e); throw new IllegalArgumentException("failed to parse template source", e);
} }

View File

@ -31,6 +31,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -402,8 +403,8 @@ public class CreateSnapshotRequest extends MasterNodeRequest<CreateSnapshotReque
*/ */
public CreateSnapshotRequest source(String source) { public CreateSnapshotRequest source(String source) {
if (hasLength(source)) { if (hasLength(source)) {
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e); throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e);
} }
@ -431,8 +432,8 @@ public class CreateSnapshotRequest extends MasterNodeRequest<CreateSnapshotReque
*/ */
public CreateSnapshotRequest source(byte[] source, int offset, int length) { public CreateSnapshotRequest source(byte[] source, int offset, int length) {
if (length > 0) { if (length > 0) {
try { try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) {
return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse repository source", e); throw new IllegalArgumentException("failed to parse repository source", e);
} }
@ -447,8 +448,8 @@ public class CreateSnapshotRequest extends MasterNodeRequest<CreateSnapshotReque
* @return this request * @return this request
*/ */
public CreateSnapshotRequest source(BytesReference source) { public CreateSnapshotRequest source(BytesReference source) {
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse snapshot source", e); throw new IllegalArgumentException("failed to parse snapshot source", e);
} }

View File

@ -30,6 +30,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -553,8 +554,8 @@ public class RestoreSnapshotRequest extends MasterNodeRequest<RestoreSnapshotReq
*/ */
public RestoreSnapshotRequest source(String source) { public RestoreSnapshotRequest source(String source) {
if (hasLength(source)) { if (hasLength(source)) {
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e); throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e);
} }
@ -586,8 +587,8 @@ public class RestoreSnapshotRequest extends MasterNodeRequest<RestoreSnapshotReq
*/ */
public RestoreSnapshotRequest source(byte[] source, int offset, int length) { public RestoreSnapshotRequest source(byte[] source, int offset, int length) {
if (length > 0) { if (length > 0) {
try { try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) {
return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse repository source", e); throw new IllegalArgumentException("failed to parse repository source", e);
} }
@ -604,8 +605,8 @@ public class RestoreSnapshotRequest extends MasterNodeRequest<RestoreSnapshotReq
* @return this request * @return this request
*/ */
public RestoreSnapshotRequest source(BytesReference source) { public RestoreSnapshotRequest source(BytesReference source) {
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse template source", e); throw new IllegalArgumentException("failed to parse template source", e);
} }

View File

@ -362,8 +362,8 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
public CreateIndexRequest source(BytesReference source) { public CreateIndexRequest source(BytesReference source) {
XContentType xContentType = XContentFactory.xContentType(source); XContentType xContentType = XContentFactory.xContentType(source);
if (xContentType != null) { if (xContentType != null) {
try { try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) {
source(XContentFactory.xContent(xContentType).createParser(source).mapAndClose()); source(parser.map());
} catch (IOException e) { } catch (IOException e) {
throw new ElasticsearchParseException("failed to parse source for create index", e); throw new ElasticsearchParseException("failed to parse source for create index", e);
} }

View File

@ -308,8 +308,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* The template source definition. * The template source definition.
*/ */
public PutIndexTemplateRequest source(String templateSource) { public PutIndexTemplateRequest source(String templateSource) {
try { try (XContentParser parser = XContentFactory.xContent(templateSource).createParser(templateSource)) {
return source(XContentFactory.xContent(templateSource).createParser(templateSource).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException("failed to parse template source [" + templateSource + "]", e); throw new IllegalArgumentException("failed to parse template source [" + templateSource + "]", e);
} }
@ -326,8 +326,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* The template source definition. * The template source definition.
*/ */
public PutIndexTemplateRequest source(byte[] source, int offset, int length) { public PutIndexTemplateRequest source(byte[] source, int offset, int length) {
try { try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) {
return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse template source", e); throw new IllegalArgumentException("failed to parse template source", e);
} }
@ -337,8 +337,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* The template source definition. * The template source definition.
*/ */
public PutIndexTemplateRequest source(BytesReference source) { public PutIndexTemplateRequest source(BytesReference source) {
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose()); return source(parser.mapOrdered());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("failed to parse template source", e); throw new IllegalArgumentException("failed to parse template source", e);
} }

View File

@ -81,9 +81,8 @@ public class AliasValidator extends AbstractComponent {
public void validateAliasStandalone(Alias alias) { public void validateAliasStandalone(Alias alias) {
validateAliasStandalone(alias.name(), alias.indexRouting()); validateAliasStandalone(alias.name(), alias.indexRouting());
if (Strings.hasLength(alias.filter())) { if (Strings.hasLength(alias.filter())) {
try { try (XContentParser parser = XContentFactory.xContent(alias.filter()).createParser(alias.filter())) {
XContentParser parser = XContentFactory.xContent(alias.filter()).createParser(alias.filter()); parser.map();
parser.mapAndClose();
} catch (Throwable e) { } catch (Throwable e) {
throw new IllegalArgumentException("failed to parse filter for alias [" + alias.name() + "]", e); throw new IllegalArgumentException("failed to parse filter for alias [" + alias.name() + "]", e);
} }

View File

@ -343,9 +343,10 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
builder.startArray("mappings"); builder.startArray("mappings");
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) { for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetaData.mappings()) {
byte[] data = cursor.value.uncompressed(); byte[] data = cursor.value.uncompressed();
XContentParser parser = XContentFactory.xContent(data).createParser(data); try (XContentParser parser = XContentFactory.xContent(data).createParser(data)) {
Map<String, Object> mapping = parser.mapOrderedAndClose(); Map<String, Object> mapping = parser.mapOrdered();
builder.map(mapping); builder.map(mapping);
}
} }
builder.endArray(); builder.endArray();
} }

View File

@ -288,7 +288,10 @@ public class MappingMetaData extends AbstractDiffable<MappingMetaData> {
public MappingMetaData(CompressedXContent mapping) throws IOException { public MappingMetaData(CompressedXContent mapping) throws IOException {
this.source = mapping; this.source = mapping;
Map<String, Object> mappingMap = XContentHelper.createParser(mapping.compressedReference()).mapOrderedAndClose(); Map<String, Object> mappingMap;
try (XContentParser parser = XContentHelper.createParser(mapping.compressedReference())) {
mappingMap = parser.mapOrdered();
}
if (mappingMap.size() != 1) { if (mappingMap.size() != 1) {
throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string()); throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
} }

View File

@ -24,6 +24,7 @@ import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.apache.lucene.util.CollectionUtil; import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version; import org.elasticsearch.Version;
@ -53,6 +54,7 @@ import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
@ -451,7 +453,9 @@ public class MetaDataCreateIndexService extends AbstractComponent {
} }
private Map<String, Object> parseMapping(String mappingSource) throws Exception { private Map<String, Object> parseMapping(String mappingSource) throws Exception {
return XContentFactory.xContent(mappingSource).createParser(mappingSource).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource)) {
return parser.map();
}
} }
private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException { private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException {

View File

@ -64,8 +64,8 @@ public class XContentHelper {
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered) throws ElasticsearchParseException { public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered) throws ElasticsearchParseException {
try { try {
XContentParser parser;
XContentType contentType; XContentType contentType;
InputStream input;
Compressor compressor = CompressorFactory.compressor(bytes); Compressor compressor = CompressorFactory.compressor(bytes);
if (compressor != null) { if (compressor != null) {
InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput()); InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput());
@ -73,15 +73,17 @@ public class XContentHelper {
compressedStreamInput = new BufferedInputStream(compressedStreamInput); compressedStreamInput = new BufferedInputStream(compressedStreamInput);
} }
contentType = XContentFactory.xContentType(compressedStreamInput); contentType = XContentFactory.xContentType(compressedStreamInput);
parser = XContentFactory.xContent(contentType).createParser(compressedStreamInput); input = compressedStreamInput;
} else { } else {
contentType = XContentFactory.xContentType(bytes); contentType = XContentFactory.xContentType(bytes);
parser = XContentFactory.xContent(contentType).createParser(bytes.streamInput()); input = bytes.streamInput();
} }
if (ordered) { try (XContentParser parser = XContentFactory.xContent(contentType).createParser(input)) {
return Tuple.tuple(contentType, parser.mapOrderedAndClose()); if (ordered) {
} else { return Tuple.tuple(contentType, parser.mapOrdered());
return Tuple.tuple(contentType, parser.mapAndClose()); } else {
return Tuple.tuple(contentType, parser.map());
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e); throw new ElasticsearchParseException("Failed to parse content to map", e);

View File

@ -130,10 +130,6 @@ public interface XContentParser extends Releasable {
Map<String, Object> mapOrdered() throws IOException; Map<String, Object> mapOrdered() throws IOException;
Map<String, Object> mapAndClose() throws IOException;
Map<String, Object> mapOrderedAndClose() throws IOException;
String text() throws IOException; String text() throws IOException;
String textOrNull() throws IOException; String textOrNull() throws IOException;

View File

@ -213,25 +213,6 @@ public abstract class AbstractXContentParser implements XContentParser {
return readOrderedMap(this); return readOrderedMap(this);
} }
@Override
public Map<String, Object> mapAndClose() throws IOException {
try {
return map();
} finally {
close();
}
}
@Override
public Map<String, Object> mapOrderedAndClose() throws IOException {
try {
return mapOrdered();
} finally {
close();
}
}
static interface MapFactory { static interface MapFactory {
Map<String, Object> newMap(); Map<String, Object> newMap();
} }

View File

@ -39,6 +39,7 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.discovery.zen.ping.PingContextProvider; import org.elasticsearch.discovery.zen.ping.PingContextProvider;
import org.elasticsearch.discovery.zen.ping.ZenPing; import org.elasticsearch.discovery.zen.ping.ZenPing;
@ -408,9 +409,9 @@ public class MulticastZenPing extends AbstractLifecycleComponent<ZenPing> implem
xContentType = XContentFactory.xContentType(data); xContentType = XContentFactory.xContentType(data);
if (xContentType != null) { if (xContentType != null) {
// an external ping // an external ping
externalPingData = XContentFactory.xContent(xContentType) try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(data)) {
.createParser(data) externalPingData = parser.map();
.mapAndClose(); }
} else { } else {
throw new IllegalStateException("failed multicast message, probably message from previous version"); throw new IllegalStateException("failed multicast message, probably message from previous version");
} }

View File

@ -34,6 +34,7 @@ import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.analysis.AnalysisService;
@ -304,8 +305,8 @@ public class DocumentMapperParser extends AbstractIndexComponent {
private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException { private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException {
Map<String, Object> root; Map<String, Object> root;
try { try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
root = XContentFactory.xContent(source).createParser(source).mapOrderedAndClose(); root = parser.mapOrdered();
} catch (Exception e) { } catch (Exception e) {
throw new MapperParsingException("failed to parse mapping definition", e); throw new MapperParsingException("failed to parse mapping definition", e);
} }

View File

@ -679,7 +679,10 @@ class DocumentParser implements Closeable {
} }
private static XContentParser transform(Mapping mapping, XContentParser parser) throws IOException { private static XContentParser transform(Mapping mapping, XContentParser parser) throws IOException {
Map<String, Object> transformed = transformSourceAsMap(mapping, parser.mapOrderedAndClose()); Map<String, Object> transformed;
try (XContentParser _ = parser) {
transformed = transformSourceAsMap(mapping, parser.mapOrdered());
}
XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()).value(transformed); XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()).value(transformed);
return parser.contentType().xContent().createParser(builder.bytes()); return parser.contentType().xContent().createParser(builder.bytes());
} }

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.AcknowledgedRestListener; import org.elasticsearch.rest.action.support.AcknowledgedRestListener;
@ -48,7 +49,10 @@ public class RestClusterUpdateSettingsAction extends BaseRestHandler {
final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest(); final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest();
clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout())); clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout()));
clusterUpdateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout())); clusterUpdateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout()));
Map<String, Object> source = XContentFactory.xContent(request.content()).createParser(request.content()).mapAndClose(); Map<String, Object> source;
try (XContentParser parser = XContentFactory.xContent(request.content()).createParser(request.content())) {
source = parser.map();
}
if (source.containsKey("transient")) { if (source.containsKey("transient")) {
clusterUpdateSettingsRequest.transientSettings((Map) source.get("transient")); clusterUpdateSettingsRequest.transientSettings((Map) source.get("transient"));
} }

View File

@ -248,7 +248,10 @@ public class IndexWarmersMetaData extends AbstractDiffable<IndexMetaData.Custom>
if (binary) { if (binary) {
builder.value(entry.source()); builder.value(entry.source());
} else { } else {
Map<String, Object> mapping = XContentFactory.xContent(entry.source()).createParser(entry.source()).mapOrderedAndClose(); Map<String, Object> mapping;
try (XContentParser parser = XContentFactory.xContent(entry.source()).createParser(entry.source())) {
mapping = parser.mapOrdered();
}
builder.map(mapping); builder.map(mapping);
} }
builder.endObject(); builder.endObject();

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
@ -49,7 +50,10 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.field("something_else", "value3") .field("something_else", "value3")
.endObject(); .endObject();
Map<String, Object> source = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); Map<String, Object> source;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
source = parser.map();
}
Map<String, Object> filter = XContentMapValues.filter(source, new String[]{"test1"}, Strings.EMPTY_ARRAY); Map<String, Object> filter = XContentMapValues.filter(source, new String[]{"test1"}, Strings.EMPTY_ARRAY);
assertThat(filter.size(), equalTo(1)); assertThat(filter.size(), equalTo(1));
assertThat(filter.get("test1").toString(), equalTo("value1")); assertThat(filter.get("test1").toString(), equalTo("value1"));
@ -75,7 +79,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.field("test1", "value1") .field("test1", "value1")
.endObject(); .endObject();
source = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
source = parser.map();
}
filter = XContentMapValues.filter(source, new String[]{"path1"}, Strings.EMPTY_ARRAY); filter = XContentMapValues.filter(source, new String[]{"path1"}, Strings.EMPTY_ARRAY);
assertThat(filter.size(), equalTo(1)); assertThat(filter.size(), equalTo(1));
@ -99,7 +105,10 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.field("test", "value") .field("test", "value")
.endObject(); .endObject();
Map<String, Object> map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); Map<String, Object> map;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("test", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("test", map).toString(), equalTo("value"));
assertThat(XContentMapValues.extractValue("test.me", map), nullValue()); assertThat(XContentMapValues.extractValue("test.me", map), nullValue());
assertThat(XContentMapValues.extractValue("something.else.2", map), nullValue()); assertThat(XContentMapValues.extractValue("something.else.2", map), nullValue());
@ -108,7 +117,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .startObject("path1").startObject("path2").field("test", "value").endObject().endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("path1.path2.test", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("path1.path2.test", map).toString(), equalTo("value"));
assertThat(XContentMapValues.extractValue("path1.path2.test_me", map), nullValue()); assertThat(XContentMapValues.extractValue("path1.path2.test_me", map), nullValue());
assertThat(XContentMapValues.extractValue("path1.non_path2.test", map), nullValue()); assertThat(XContentMapValues.extractValue("path1.non_path2.test", map), nullValue());
@ -128,7 +139,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.startObject("path1").field("test", "value1", "value2").endObject() .startObject("path1").field("test", "value1", "value2").endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
extValue = XContentMapValues.extractValue("path1.test", map); extValue = XContentMapValues.extractValue("path1.test", map);
assertThat(extValue, instanceOf(List.class)); assertThat(extValue, instanceOf(List.class));
@ -145,7 +158,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.endObject() .endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
extValue = XContentMapValues.extractValue("path1.path2.test", map); extValue = XContentMapValues.extractValue("path1.path2.test", map);
assertThat(extValue, instanceOf(List.class)); assertThat(extValue, instanceOf(List.class));
@ -159,14 +174,18 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
builder = XContentFactory.jsonBuilder().startObject() builder = XContentFactory.jsonBuilder().startObject()
.field("xxx.yyy", "value") .field("xxx.yyy", "value")
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value"));
builder = XContentFactory.jsonBuilder().startObject() builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value"));
} }
@ -177,28 +196,37 @@ public class XContentMapValuesTests extends ElasticsearchTestCase {
.field("test", "value") .field("test", "value")
.endObject(); .endObject();
Map<String, Object> map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); Map<String, Object> map;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value")); assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value"));
builder = XContentFactory.jsonBuilder().startObject() builder = XContentFactory.jsonBuilder().startObject()
.field("test.me", "value") .field("test.me", "value")
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value")); assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value"));
builder = XContentFactory.jsonBuilder().startObject() builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .startObject("path1").startObject("path2").field("test", "value").endObject().endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value")); assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value"));
builder = XContentFactory.jsonBuilder().startObject() builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject(); .endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value")); assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value"));
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.mapper.completion; package org.elasticsearch.index.mapper.completion;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
@ -76,7 +77,10 @@ public class CompletionFieldMapperTests extends ElasticsearchSingleNodeTest {
XContentBuilder builder = jsonBuilder().startObject(); XContentBuilder builder = jsonBuilder().startObject();
completionFieldMapper.toXContent(builder, null).endObject(); completionFieldMapper.toXContent(builder, null).endObject();
builder.close(); builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> serializedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
serializedMap = parser.map();
}
Map<String, Object> configMap = (Map<String, Object>) serializedMap.get("completion"); Map<String, Object> configMap = (Map<String, Object>) serializedMap.get("completion");
assertThat(configMap.get("analyzer").toString(), is("simple")); assertThat(configMap.get("analyzer").toString(), is("simple"));
assertThat(configMap.get("search_analyzer").toString(), is("standard")); assertThat(configMap.get("search_analyzer").toString(), is("standard"));
@ -105,7 +109,10 @@ public class CompletionFieldMapperTests extends ElasticsearchSingleNodeTest {
XContentBuilder builder = jsonBuilder().startObject(); XContentBuilder builder = jsonBuilder().startObject();
completionFieldMapper.toXContent(builder, null).endObject(); completionFieldMapper.toXContent(builder, null).endObject();
builder.close(); builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> serializedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
serializedMap = parser.map();
}
Map<String, Object> configMap = (Map<String, Object>) serializedMap.get("completion"); Map<String, Object> configMap = (Map<String, Object>) serializedMap.get("completion");
assertThat(configMap.get("analyzer").toString(), is("simple")); assertThat(configMap.get("analyzer").toString(), is("simple"));
} }

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
@ -91,7 +92,10 @@ public class CopyToMapperTests extends ElasticsearchSingleNodeTest {
XContentBuilder builder = jsonBuilder().startObject(); XContentBuilder builder = jsonBuilder().startObject();
stringFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); stringFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
builder.close(); builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> serializedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
serializedMap = parser.map();
}
Map<String, Object> copyTestMap = (Map<String, Object>) serializedMap.get("copy_test"); Map<String, Object> copyTestMap = (Map<String, Object>) serializedMap.get("copy_test");
assertThat(copyTestMap.get("type").toString(), is("string")); assertThat(copyTestMap.get("type").toString(), is("string"));
List<String> copyToList = (List<String>) copyTestMap.get("copy_to"); List<String> copyToList = (List<String>) copyTestMap.get("copy_to");

View File

@ -32,6 +32,7 @@ import org.elasticsearch.common.util.LocaleUtils;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.*;
@ -380,7 +381,10 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest {
private Map<String, String> getConfigurationViaXContent(DateFieldMapper dateFieldMapper) throws IOException { private Map<String, String> getConfigurationViaXContent(DateFieldMapper dateFieldMapper) throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder().startObject(); XContentBuilder builder = JsonXContent.contentBuilder().startObject();
dateFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); dateFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
Map<String, Object> dateFieldMapperMap = JsonXContent.jsonXContent.createParser(builder.string()).mapAndClose(); Map<String, Object> dateFieldMapperMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
dateFieldMapperMap = parser.map();
}
assertThat(dateFieldMapperMap, hasKey("field")); assertThat(dateFieldMapperMap, hasKey("field"));
assertThat(dateFieldMapperMap.get("field"), is(instanceOf(Map.class))); assertThat(dateFieldMapperMap.get("field"), is(instanceOf(Map.class)));
return (Map<String, String>) dateFieldMapperMap.get("field"); return (Map<String, String>) dateFieldMapperMap.get("field");

View File

@ -29,6 +29,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.ParsedDocument;
@ -79,7 +80,10 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest {
XContentBuilder builder = JsonXContent.contentBuilder().startObject(); XContentBuilder builder = JsonXContent.contentBuilder().startObject();
enabledMapper.routingFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); enabledMapper.routingFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
builder.close(); builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> serializedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
serializedMap = parser.map();
}
assertThat(serializedMap, hasKey("_routing")); assertThat(serializedMap, hasKey("_routing"));
assertThat(serializedMap.get("_routing"), instanceOf(Map.class)); assertThat(serializedMap.get("_routing"), instanceOf(Map.class));
Map<String, Object> routingConfiguration = (Map<String, Object>) serializedMap.get("_routing"); Map<String, Object> routingConfiguration = (Map<String, Object>) serializedMap.get("_routing");

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.*;
import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.ElasticsearchSingleNodeTest;
@ -122,7 +123,10 @@ public class DefaultSourceMappingTests extends ElasticsearchSingleNodeTest {
.endObject().bytes()); .endObject().bytes());
IndexableField sourceField = doc.rootDoc().getField("_source"); IndexableField sourceField = doc.rootDoc().getField("_source");
Map<String, Object> sourceAsMap = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue())).mapAndClose(); Map<String, Object> sourceAsMap;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) {
sourceAsMap = parser.map();
}
assertThat(sourceAsMap.containsKey("path1"), equalTo(true)); assertThat(sourceAsMap.containsKey("path1"), equalTo(true));
assertThat(sourceAsMap.containsKey("path2"), equalTo(false)); assertThat(sourceAsMap.containsKey("path2"), equalTo(false));
} }
@ -140,7 +144,10 @@ public class DefaultSourceMappingTests extends ElasticsearchSingleNodeTest {
.endObject().bytes()); .endObject().bytes());
IndexableField sourceField = doc.rootDoc().getField("_source"); IndexableField sourceField = doc.rootDoc().getField("_source");
Map<String, Object> sourceAsMap = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue())).mapAndClose(); Map<String, Object> sourceAsMap;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) {
sourceAsMap = parser.map();
}
assertThat(sourceAsMap.containsKey("path1"), equalTo(false)); assertThat(sourceAsMap.containsKey("path1"), equalTo(false));
assertThat(sourceAsMap.containsKey("path2"), equalTo(true)); assertThat(sourceAsMap.containsKey("path2"), equalTo(true));
} }

View File

@ -34,6 +34,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.FieldDataType;
@ -286,7 +287,10 @@ public class SimpleStringMappingTests extends ElasticsearchSingleNodeTest {
fieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); fieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
builder.close(); builder.close();
Map<String, Object> fieldMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> fieldMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
fieldMap = parser.map();
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) fieldMap.get(fieldName); Map<String, Object> result = (Map<String, Object>) fieldMap.get(fieldName);
return result; return result;

View File

@ -35,6 +35,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
@ -168,7 +169,10 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest {
XContentBuilder builder = JsonXContent.contentBuilder().startObject(); XContentBuilder builder = JsonXContent.contentBuilder().startObject();
enabledMapper.timestampFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); enabledMapper.timestampFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
builder.close(); builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); Map<String, Object> serializedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
serializedMap = parser.map();
}
assertThat(serializedMap, hasKey("_timestamp")); assertThat(serializedMap, hasKey("_timestamp"));
assertThat(serializedMap.get("_timestamp"), instanceOf(Map.class)); assertThat(serializedMap.get("_timestamp"), instanceOf(Map.class));
Map<String, Object> timestampConfiguration = (Map<String, Object>) serializedMap.get("_timestamp"); Map<String, Object> timestampConfiguration = (Map<String, Object>) serializedMap.get("_timestamp");

View File

@ -18,6 +18,7 @@
*/ */
package org.elasticsearch.search.builder; package org.elasticsearch.search.builder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test; import org.junit.Test;
@ -78,7 +79,10 @@ public class SearchSourceBuilderTest extends ElasticsearchTestCase {
} }
private Map<String, Object> getSourceMap(SearchSourceBuilder builder) throws IOException { private Map<String, Object> getSourceMap(SearchSourceBuilder builder) throws IOException {
Map<String, Object> data = JsonXContent.jsonXContent.createParser(builder.toString()).mapAndClose(); Map<String, Object> data;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.toString())) {
data = parser.map();
}
assertThat(data, hasKey("_source")); assertThat(data, hasKey("_source"));
return (Map<String, Object>) data.get("_source"); return (Map<String, Object>) data.get("_source");
} }

View File

@ -19,6 +19,8 @@
package org.elasticsearch.test.rest.json; package org.elasticsearch.test.rest.json;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.rest.Stash; import org.elasticsearch.test.rest.Stash;
@ -40,7 +42,9 @@ public class JsonPath {
} }
private static Map<String, Object> convertToMap(String json) throws IOException { private static Map<String, Object> convertToMap(String json) throws IOException {
return JsonXContent.jsonXContent.createParser(json).mapOrderedAndClose(); try (XContentParser parser = JsonXContent.jsonXContent.createParser(json)) {
return parser.mapOrdered();
}
} }
/** /**

View File

@ -18,6 +18,7 @@
*/ */
package org.elasticsearch.test.rest.test; package org.elasticsearch.test.rest.test;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import org.elasticsearch.test.rest.parser.DoSectionParser; import org.elasticsearch.test.rest.parser.DoSectionParser;
@ -387,7 +388,10 @@ public class DoSectionParserTests extends AbstractParserTests {
} }
private static void assertJsonEquals(Map<String, Object> actual, String expected) throws IOException { private static void assertJsonEquals(Map<String, Object> actual, String expected) throws IOException {
Map<String,Object> expectedMap = JsonXContent.jsonXContent.createParser(expected).mapOrderedAndClose(); Map<String,Object> expectedMap;
try (XContentParser parser = JsonXContent.jsonXContent.createParser(expected)) {
expectedMap = parser.mapOrdered();
}
MatcherAssert.assertThat(actual, equalTo(expectedMap)); MatcherAssert.assertThat(actual, equalTo(expectedMap));
} }
} }

View File

@ -215,8 +215,10 @@ public class SimpleThreadPoolTests extends ElasticsearchIntegrationTest {
info.toXContent(builder, ToXContent.EMPTY_PARAMS); info.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject(); builder.endObject();
builder.close(); builder.close();
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.string()); Map<String, Object> poolsMap;
Map<String, Object> poolsMap = parser.mapAndClose(); try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.string())) {
poolsMap = parser.map();
}
return (Map<String, Object>) ((Map<String, Object>) poolsMap.get("thread_pool")).get(poolName); return (Map<String, Object>) ((Map<String, Object>) poolsMap.get("thread_pool")).get(poolName);
} }

View File

@ -82,8 +82,10 @@ public class ThreadPoolSerializationTests extends ElasticsearchTestCase {
builder.endObject(); builder.endObject();
BytesReference bytesReference = builder.bytes(); BytesReference bytesReference = builder.bytes();
XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference); Map<String, Object> map;
Map<String, Object> map = parser.mapAndClose(); try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference)) {
map = parser.map();
}
assertThat(map, hasKey("foo")); assertThat(map, hasKey("foo"));
map = (Map<String, Object>) map.get("foo"); map = (Map<String, Object>) map.get("foo");
assertThat(map, hasKey("queue_size")); assertThat(map, hasKey("queue_size"));