diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java index 1aed59227e8bd..4f93a5dd5d5e1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java @@ -25,8 +25,6 @@ import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -207,13 +205,7 @@ static class NameOrDefinition implements ToXContentFragment { NameOrDefinition(Map definition) { this.name = null; Objects.requireNonNull(definition); - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { - throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); - } + this.definition = Settings.builder().loadFromMap(definition).build(); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java index 1a018591dc770..290fca008fece 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java @@ -127,13 +127,7 @@ public CreateIndexRequest settings(XContentBuilder builder) { * The settings to create the index with (either json/yaml/properties format) */ public CreateIndexRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java index c5be421ed6fb4..d90db9d13cbf4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java @@ -22,7 +22,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.client.TimedRequest; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; @@ -170,13 +169,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType * The settings to create the index template with (either json or yaml format). */ public PutIndexTemplateRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java index 3a2243d6548fd..362b1705efd58 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java @@ -19,13 +19,10 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import java.io.IOException; @@ -160,10 +157,8 @@ public static final class NameOrDefinition implements ToXContentFragment { this.name = null; Objects.requireNonNull(definition); try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { + this.definition = Settings.builder().loadFromMap(definition).build(); + } catch (Exception e) { throw new IllegalArgumentException("Failed to parse [" + definition + "] in [" + field.getPreferredName() + "]", e); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java index c9a2c6b751734..7b25b8237d8c4 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java @@ -19,16 +19,13 @@ package org.elasticsearch.action.admin.cluster.repositories.put; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -168,13 +165,7 @@ public PutRepositoryRequest settings(String source, XContentType xContentType) { * @return this request */ public PutRepositoryRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java index 41ef1e230d744..eb9c16f8eb0f4 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java @@ -19,18 +19,15 @@ package org.elasticsearch.action.admin.cluster.settings; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; @@ -115,13 +112,7 @@ public ClusterUpdateSettingsRequest transientSettings(String source, XContentTyp * Sets the transient settings to be updated. They will not survive a full cluster restart */ public ClusterUpdateSettingsRequest transientSettings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - transientSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.transientSettings = Settings.builder().loadFromMap(source).build(); return this; } @@ -153,13 +144,7 @@ public ClusterUpdateSettingsRequest persistentSettings(String source, XContentTy * Sets the persistent settings to be updated. They will get applied cross restarts */ public ClusterUpdateSettingsRequest persistentSettings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - persistentSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.persistentSettings = Settings.builder().loadFromMap(source).build(); return this; } @@ -183,7 +168,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) throws IOException { + public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java index 4d12a7de01362..bd1e84cdff58b 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java @@ -19,7 +19,6 @@ package org.elasticsearch.action.admin.cluster.snapshots.restore; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.MasterNodeRequest; @@ -30,7 +29,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -417,13 +415,7 @@ public RestoreSnapshotRequest indexSettings(String source, XContentType xContent * Sets settings that should be added/changed in all restored indices */ public RestoreSnapshotRequest indexSettings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - indexSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.indexSettings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index 58b65a80b3844..633607ae84950 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -208,13 +208,7 @@ public CreateIndexRequest settings(XContentBuilder builder) { * The settings to create the index with (either json/yaml/properties format) */ public CreateIndexRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java index d5144bfbb7862..7bb6d672b6337 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java @@ -19,7 +19,6 @@ package org.elasticsearch.action.admin.indices.settings.put; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; @@ -30,7 +29,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; @@ -169,13 +167,7 @@ public UpdateSettingsRequest setPreserveExisting(boolean preserveExisting) { * Sets the settings to be updated (either json or yaml format) */ public UpdateSettingsRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java index 0be54db3b0f38..4dc47b99193a9 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java @@ -211,13 +211,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType * The settings to create the index template with (either json or yaml format). */ public PutIndexTemplateRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/common/settings/Settings.java b/server/src/main/java/org/elasticsearch/common/settings/Settings.java index 91a37513707d7..50aa57b900d8a 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -21,6 +21,7 @@ import org.apache.logging.log4j.Level; import org.apache.lucene.util.SetOnce; +import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; import org.elasticsearch.common.Booleans; @@ -1019,6 +1020,19 @@ private void processLegacyLists(Map map) { } } + /** + * Loads settings from a map. + */ + public Builder loadFromMap(Map map) { + // TODO: do this without a serialization round-trip + try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { + builder.map(map); + return loadFromSource(Strings.toString(builder), builder.contentType()); + } catch (IOException e) { + throw new ElasticsearchGenerationException("Failed to generate [" + map + "]", e); + } + } + /** * Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)} */ diff --git a/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java b/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java index 1831904de6b6f..b21b9b3d621e8 100644 --- a/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java +++ b/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java @@ -19,17 +19,14 @@ package org.elasticsearch.index.analysis; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParseException; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.Map; @@ -49,10 +46,8 @@ public NameOrDefinition(Map definition) { this.name = null; Objects.requireNonNull(definition); try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { + this.definition = Settings.builder().loadFromMap(definition).build(); + } catch (Exception e) { throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); } }