diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java b/client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java index 5bf65a6ea1989..83e148f098f12 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java @@ -44,6 +44,7 @@ import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.DeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.ToXContentObject; import org.opensearch.common.xcontent.XContentBuilder; @@ -64,6 +65,8 @@ /** * A request to create an index. + * + * @opensearch.api */ public class CreateIndexRequest extends TimedRequest implements Validatable, ToXContentObject { static final ParseField MAPPINGS = new ParseField("mappings"); @@ -122,12 +125,23 @@ public CreateIndexRequest settings(Settings settings) { /** * The settings to create the index with (either json or yaml format) + * + * @deprecated use {@link #settings(String source, MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest settings(String source, XContentType xContentType) { this.settings = Settings.builder().loadFromSource(source, xContentType).build(); return this; } + /** + * The settings to create the index with (either json or yaml format) + */ + public CreateIndexRequest settings(String source, MediaType mediaType) { + this.settings = Settings.builder().loadFromSource(source, mediaType).build(); + return this; + } + /** * Allows to set the settings using a json builder. */ @@ -159,11 +173,26 @@ public XContentType mappingsXContentType() { * * @param source The mapping source * @param xContentType The content type of the source + * + * @deprecated use {@link #mapping(String source, MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest mapping(String source, XContentType xContentType) { return mapping(new BytesArray(source), xContentType); } + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param mediaType The media type of the source + */ + public CreateIndexRequest mapping(String source, MediaType mediaType) { + return mapping(new BytesArray(source), mediaType); + } + /** * Adds mapping that will be added when the index gets created. * @@ -199,7 +228,10 @@ public CreateIndexRequest mapping(Map source) { * * @param source The mapping source * @param xContentType the content type of the mapping source + * + * @deprecated use {@link #mapping(BytesReference source, MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest mapping(BytesReference source, XContentType xContentType) { Objects.requireNonNull(xContentType); mappings = source; @@ -207,6 +239,21 @@ public CreateIndexRequest mapping(BytesReference source, XContentType xContentTy return this; } + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param mediaType the content type of the mapping source + */ + public CreateIndexRequest mapping(BytesReference source, MediaType mediaType) { + Objects.requireNonNull(mediaType); + mappings = source; + mappingsXContentType = (XContentType) mediaType; + return this; + } + public Set aliases() { return this.aliases; } @@ -233,7 +280,10 @@ public CreateIndexRequest aliases(XContentBuilder source) { /** * Sets the aliases that will be associated with the index when it gets created + * + * @deprecated use {@link #aliases(String, MediaType)} instead */ + @Deprecated public CreateIndexRequest aliases(String source, XContentType contentType) { return aliases(new BytesArray(source), contentType); } @@ -241,7 +291,24 @@ public CreateIndexRequest aliases(String source, XContentType contentType) { /** * Sets the aliases that will be associated with the index when it gets created */ + public CreateIndexRequest aliases(String source, MediaType mediaType) { + return aliases(new BytesArray(source), mediaType); + } + + /** + * Sets the aliases that will be associated with the index when it gets created + * + * @deprecated use {@link #aliases(BytesReference source, MediaType contentType)} instead + */ + @Deprecated public CreateIndexRequest aliases(BytesReference source, XContentType contentType) { + return aliases(source, (MediaType) contentType); + } + + /** + * Sets the aliases that will be associated with the index when it gets created + */ + public CreateIndexRequest aliases(BytesReference source, MediaType contentType) { // EMPTY is safe here because we never call namedObject try ( XContentParser parser = XContentHelper.createParser( @@ -282,11 +349,23 @@ public CreateIndexRequest aliases(Collection aliases) { * Sets the settings and mappings as a single source. * * Note that the mapping definition should *not* be nested under a type name. + * + * @deprecated use {@link #source(String, MediaType)} instead */ + @Deprecated public CreateIndexRequest source(String source, XContentType xContentType) { return source(new BytesArray(source), xContentType); } + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(String source, MediaType mediaType) { + return source(new BytesArray(source), mediaType); + } + /** * Sets the settings and mappings as a single source. * @@ -300,13 +379,27 @@ public CreateIndexRequest source(XContentBuilder source) { * Sets the settings and mappings as a single source. * * Note that the mapping definition should *not* be nested under a type name. + * + * @deprecated use {@link #source(BytesReference, MediaType)} instead */ + @Deprecated public CreateIndexRequest source(BytesReference source, XContentType xContentType) { Objects.requireNonNull(xContentType); source(XContentHelper.convertToMap(source, false, xContentType).v2()); return this; } + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(BytesReference source, MediaType mediaType) { + Objects.requireNonNull(mediaType); + source(XContentHelper.convertToMap(source, false, mediaType).v2()); + return this; + } + /** * Sets the settings and mappings as a single source. * diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java index cd0eb8881ab0c..d7ce845761357 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java @@ -43,6 +43,7 @@ import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.DeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.ToXContentFragment; import org.opensearch.common.xcontent.XContentBuilder; @@ -268,9 +269,10 @@ private PutIndexTemplateRequest internalMapping(Map source) { try { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.map(source); - Objects.requireNonNull(builder.contentType()); + MediaType mediaType = builder.contentType(); + Objects.requireNonNull(mediaType); try { - mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType())); + mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType)); return this; } catch (IOException e) { throw new UncheckedIOException("failed to convert source to json", e); @@ -342,7 +344,10 @@ public PutIndexTemplateRequest source(Map templateSource) { /** * The template source definition. + * + * @deprecated use {@link #source(String, MediaType)} instead */ + @Deprecated public PutIndexTemplateRequest source(String templateSource, XContentType xContentType) { return source(XContentHelper.convertToMap(xContentType.xContent(), templateSource, true)); } @@ -350,6 +355,16 @@ public PutIndexTemplateRequest source(String templateSource, XContentType xConte /** * The template source definition. */ + public PutIndexTemplateRequest source(String templateSource, MediaType mediaType) { + return source(XContentHelper.convertToMap(mediaType.xContent(), templateSource, true)); + } + + /** + * The template source definition. + * + * @deprecated use {@link #source(byte[], MediaType)} instead + */ + @Deprecated public PutIndexTemplateRequest source(byte[] source, XContentType xContentType) { return source(source, 0, source.length, xContentType); } @@ -357,6 +372,16 @@ public PutIndexTemplateRequest source(byte[] source, XContentType xContentType) /** * The template source definition. */ + public PutIndexTemplateRequest source(byte[] source, MediaType mediaType) { + return source(source, 0, source.length, mediaType); + } + + /** + * The template source definition. + * + * @deprecated use {@link #source(byte[], int, int, MediaType)} instead + */ + @Deprecated public PutIndexTemplateRequest source(byte[] source, int offset, int length, XContentType xContentType) { return source(new BytesArray(source, offset, length), xContentType); } @@ -364,10 +389,27 @@ public PutIndexTemplateRequest source(byte[] source, int offset, int length, XCo /** * The template source definition. */ + public PutIndexTemplateRequest source(byte[] source, int offset, int length, MediaType mediaType) { + return source(new BytesArray(source, offset, length), mediaType); + } + + /** + * The template source definition. + * + * @deprecated use {@link #source(BytesReference, MediaType)} instead + */ + @Deprecated public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType) { return source(XContentHelper.convertToMap(source, true, xContentType).v2()); } + /** + * The template source definition. + */ + public PutIndexTemplateRequest source(BytesReference source, MediaType mediaType) { + return source(XContentHelper.convertToMap(source, true, mediaType).v2()); + } + public Set aliases() { return this.aliases; } diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java b/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java index c8d5e4c95782a..9c0d850fe7288 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java @@ -38,6 +38,7 @@ import org.opensearch.client.TimedRequest; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.ToXContentObject; import org.opensearch.common.xcontent.XContentBuilder; @@ -52,6 +53,8 @@ * Put a mapping definition into one or more indices. If an index already contains mappings, * the new mappings will be merged with the existing one. If there are elements that cannot * be merged, the request will be rejected. + * + * @opensearch.api */ public class PutMappingRequest extends TimedRequest implements IndicesRequest, ToXContentObject { @@ -59,7 +62,7 @@ public class PutMappingRequest extends TimedRequest implements IndicesRequest, T private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true); private BytesReference source; - private XContentType xContentType; + private MediaType mediaType; /** * Constructs a new put mapping request against one or more indices. If no indices @@ -96,9 +99,19 @@ public BytesReference source() { /** * The {@link XContentType} of the mapping source. + * + * @deprecated use {@link #mediaType()} instead */ + @Deprecated public XContentType xContentType() { - return xContentType; + return (XContentType) mediaType; + } + + /** + * The {@link XContentType} of the mapping source. + */ + public MediaType mediaType() { + return mediaType; } /** @@ -120,10 +133,24 @@ public PutMappingRequest source(Map mappingSource) { * The mapping source definition. * * Note that the definition should *not* be nested under a type name. + * + * @deprecated use {@link #source(String, MediaType)} instead */ + @Deprecated public PutMappingRequest source(String mappingSource, XContentType xContentType) { this.source = new BytesArray(mappingSource); - this.xContentType = xContentType; + this.mediaType = xContentType; + return this; + } + + /** + * The mapping source definition. + * + * Note that the definition should *not* be nested under a type name. + */ + public PutMappingRequest source(String mappingSource, MediaType mediaType) { + this.source = new BytesArray(mappingSource); + this.mediaType = mediaType; return this; } @@ -134,7 +161,11 @@ public PutMappingRequest source(String mappingSource, XContentType xContentType) */ public PutMappingRequest source(XContentBuilder builder) { this.source = BytesReference.bytes(builder); - this.xContentType = builder.contentType(); + MediaType mediaType = builder.contentType(); + if (mediaType instanceof XContentType == false) { + throw new IllegalArgumentException("PutMappingRequest does not support media type [" + mediaType.getClass().getName() + "]"); + } + this.mediaType = builder.contentType(); return this; } @@ -142,10 +173,24 @@ public PutMappingRequest source(XContentBuilder builder) { * The mapping source definition. * * Note that the definition should *not* be nested under a type name. + * + * @deprecated use {@link #source(BytesReference, MediaType)} instead */ + @Deprecated public PutMappingRequest source(BytesReference source, XContentType xContentType) { this.source = source; - this.xContentType = xContentType; + this.mediaType = xContentType; + return this; + } + + /** + * The mapping source definition. + * + * Note that the definition should *not* be nested under a type name. + */ + public PutMappingRequest source(BytesReference source, MediaType mediaType) { + this.source = source; + this.mediaType = mediaType; return this; } @@ -153,7 +198,7 @@ public PutMappingRequest source(BytesReference source, XContentType xContentType public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { if (source != null) { try (InputStream stream = source.streamInput()) { - builder.rawValue(stream, xContentType); + builder.rawValue(stream, mediaType); } } else { builder.startObject().endObject(); diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/indices/PutMappingRequestTests.java b/client/rest-high-level/src/test/java/org/opensearch/client/indices/PutMappingRequestTests.java index 6065be82ddb22..877d3cd77687d 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/indices/PutMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/indices/PutMappingRequestTests.java @@ -74,8 +74,8 @@ protected boolean supportsUnknownFields() { protected void assertEqualInstances(PutMappingRequest expected, PutMappingRequest actual) { if (actual.source() != null) { try ( - XContentParser expectedJson = createParser(expected.xContentType().xContent(), expected.source()); - XContentParser actualJson = createParser(actual.xContentType().xContent(), actual.source()) + XContentParser expectedJson = createParser(expected.mediaType().xContent(), expected.source()); + XContentParser actualJson = createParser(actual.mediaType().xContent(), actual.source()) ) { assertEquals(expectedJson.mapOrdered(), actualJson.mapOrdered()); } catch (IOException e) { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentBuilder.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentBuilder.java index 7086cdb0fda83..47e89e65a5e83 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentBuilder.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentBuilder.java @@ -1004,7 +1004,7 @@ public XContentBuilder rawField(String name, InputStream value, XContentType con /** * Writes a value with the source coming directly from the bytes in the stream */ - public XContentBuilder rawValue(InputStream stream, XContentType contentType) throws IOException { + public XContentBuilder rawValue(InputStream stream, MediaType contentType) throws IOException { generator.writeRawValue(stream, contentType); return this; } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java index 63b1ef6af752d..676c9618ba9f5 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java @@ -141,7 +141,7 @@ public static XContentBuilder contentBuilder(XContentType type) throws IOExcepti /** * Returns the {@link org.opensearch.common.xcontent.XContent} for the provided content type. */ - public static XContent xContent(XContentType type) { + public static XContent xContent(MediaType type) { if (type == null) { throw new IllegalArgumentException("Cannot get xcontent for unknown type"); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentGenerator.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentGenerator.java index b0dacb3826be3..4b996e558d263 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentGenerator.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentGenerator.java @@ -122,7 +122,7 @@ public interface XContentGenerator extends Closeable, Flushable { /** * Writes a raw value taken from the bytes in the stream */ - void writeRawValue(InputStream value, XContentType xContentType) throws IOException; + void writeRawValue(InputStream value, MediaType xContentType) throws IOException; void copyCurrentStructure(XContentParser parser) throws IOException; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java index b972f52c57201..5be3c5c55c12a 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java @@ -42,6 +42,7 @@ import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.core.util.JsonGeneratorDelegate; import org.opensearch.common.xcontent.DeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.XContent; import org.opensearch.common.xcontent.XContentFactory; @@ -369,7 +370,7 @@ public void writeRawField(String name, InputStream content, XContentType content } @Override - public void writeRawValue(InputStream stream, XContentType xContentType) throws IOException { + public void writeRawValue(InputStream stream, MediaType xContentType) throws IOException { if (mayWriteRawData(xContentType) == false) { copyRawValue(stream, xContentType.xContent()); } else { @@ -383,7 +384,7 @@ public void writeRawValue(InputStream stream, XContentType xContentType) throws } } - private boolean mayWriteRawData(XContentType contentType) { + private boolean mayWriteRawData(MediaType contentType) { // When the current generator is filtered (ie filter != null) // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly diff --git a/server/src/main/java/org/opensearch/action/admin/indices/create/CreateIndexRequest.java b/server/src/main/java/org/opensearch/action/admin/indices/create/CreateIndexRequest.java index 302c2aad64bb4..fb3d1ef531966 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/create/CreateIndexRequest.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/create/CreateIndexRequest.java @@ -51,6 +51,7 @@ import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.DeprecationHandler; import org.opensearch.common.xcontent.LoggingDeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; @@ -208,12 +209,23 @@ public CreateIndexRequest settings(Settings settings) { /** * The settings to create the index with (either json or yaml format) + * + * @deprecated use {@link #settings(String source, MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest settings(String source, XContentType xContentType) { this.settings = Settings.builder().loadFromSource(source, xContentType).build(); return this; } + /** + * The settings to create the index with (either json or yaml format) + */ + public CreateIndexRequest settings(String source, MediaType mediaType) { + this.settings = Settings.builder().loadFromSource(source, mediaType).build(); + return this; + } + /** * Allows to set the settings using a json builder. */ @@ -248,23 +260,55 @@ public CreateIndexRequest mapping(String mapping) { * * @param source The mapping source * @param xContentType The content type of the source + * + * @deprecated use {@link #mapping(String source, MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest mapping(String source, XContentType xContentType) { return mapping(new BytesArray(source), xContentType); } + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param mediaType The media type of the source + */ + public CreateIndexRequest mapping(String source, MediaType mediaType) { + return mapping(new BytesArray(source), mediaType); + } + /** * Adds mapping that will be added when the index gets created. * * @param source The mapping source * @param xContentType the content type of the mapping source + * + * @deprecated use {@link #mapping(BytesReference source, MediaType mediaType)} instead */ + @Deprecated private CreateIndexRequest mapping(BytesReference source, XContentType xContentType) { Objects.requireNonNull(xContentType); Map mappingAsMap = XContentHelper.convertToMap(source, false, xContentType).v2(); return mapping(MapperService.SINGLE_MAPPING_NAME, mappingAsMap); } + /** + * Adds mapping that will be added when the index gets created. + * + * Note that the definition should *not* be nested under a type name. + * + * @param source The mapping source + * @param mediaType The media type of the source + */ + public CreateIndexRequest mapping(BytesReference source, MediaType mediaType) { + Objects.requireNonNull(mediaType); + Map mappingAsMap = XContentHelper.convertToMap(source, false, mediaType).v2(); + return mapping(MapperService.SINGLE_MAPPING_NAME, mappingAsMap); + } + /** * Adds mapping that will be added when the index gets created. * @@ -379,11 +423,23 @@ public CreateIndexRequest alias(Alias alias) { /** * Sets the settings and mappings as a single source. + * + * @deprecated use {@link #source(String, MediaType)} instead */ + @Deprecated public CreateIndexRequest source(String source, XContentType xContentType) { return source(new BytesArray(source), xContentType); } + /** + * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(String source, MediaType mediaType) { + return source(new BytesArray(source), mediaType); + } + /** * Sets the settings and mappings as a single source. */ @@ -393,14 +449,29 @@ public CreateIndexRequest source(XContentBuilder source) { /** * Sets the settings and mappings as a single source. + * + * @deprecated use {@link #source(byte[], MediaType mediaType)} instead */ + @Deprecated public CreateIndexRequest source(byte[] source, XContentType xContentType) { return source(source, 0, source.length, xContentType); } /** * Sets the settings and mappings as a single source. + * + * Note that the mapping definition should *not* be nested under a type name. + */ + public CreateIndexRequest source(byte[] source, MediaType mediaType) { + return source(source, 0, source.length, mediaType); + } + + /** + * Sets the settings and mappings as a single source. + * + * @deprecated use {@link #source(byte[], int, int, MediaType)} instead */ + @Deprecated public CreateIndexRequest source(byte[] source, int offset, int length, XContentType xContentType) { return source(new BytesArray(source, offset, length), xContentType); } @@ -408,12 +479,31 @@ public CreateIndexRequest source(byte[] source, int offset, int length, XContent /** * Sets the settings and mappings as a single source. */ + public CreateIndexRequest source(byte[] source, int offset, int length, MediaType mediaType) { + return source(new BytesArray(source, offset, length), mediaType); + } + + /** + * Sets the settings and mappings as a single source. + * + * @deprecated use {@link #source(BytesReference, MediaType)} instead + */ + @Deprecated public CreateIndexRequest source(BytesReference source, XContentType xContentType) { Objects.requireNonNull(xContentType); source(XContentHelper.convertToMap(source, false, xContentType).v2(), LoggingDeprecationHandler.INSTANCE); return this; } + /** + * Sets the settings and mappings as a single source. + */ + public CreateIndexRequest source(BytesReference source, MediaType mediaType) { + Objects.requireNonNull(mediaType); + source(XContentHelper.convertToMap(source, false, mediaType).v2(), LoggingDeprecationHandler.INSTANCE); + return this; + } + /** * Sets the settings and mappings as a single source. */ diff --git a/server/src/main/java/org/opensearch/action/ingest/PutPipelineRequest.java b/server/src/main/java/org/opensearch/action/ingest/PutPipelineRequest.java index d5fbaa46810f7..63a9c4266d334 100644 --- a/server/src/main/java/org/opensearch/action/ingest/PutPipelineRequest.java +++ b/server/src/main/java/org/opensearch/action/ingest/PutPipelineRequest.java @@ -37,6 +37,7 @@ import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.ToXContentObject; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentType; @@ -57,13 +58,28 @@ public class PutPipelineRequest extends AcknowledgedRequest /** * Create a new pipeline request with the id and source along with the content type of the source + * + * @deprecated use {@link #PutPipelineRequest(String, BytesReference, MediaType)} instead */ + @Deprecated public PutPipelineRequest(String id, BytesReference source, XContentType xContentType) { this.id = Objects.requireNonNull(id); this.source = Objects.requireNonNull(source); this.xContentType = Objects.requireNonNull(xContentType); } + /** + * Create a new pipeline request with the id and source along with the content type of the source + */ + public PutPipelineRequest(String id, BytesReference source, MediaType mediaType) { + this.id = Objects.requireNonNull(id); + this.source = Objects.requireNonNull(source); + if (mediaType instanceof XContentType == false) { + throw new IllegalArgumentException("PutPipelineRequest found unsupported media type [" + mediaType.getClass().getName() + "]"); + } + this.xContentType = (XContentType) Objects.requireNonNull(mediaType); + } + public PutPipelineRequest(StreamInput in) throws IOException { super(in); id = in.readString(); diff --git a/server/src/main/java/org/opensearch/common/settings/Settings.java b/server/src/main/java/org/opensearch/common/settings/Settings.java index d5ec0e514ac67..b4bfa3581047d 100644 --- a/server/src/main/java/org/opensearch/common/settings/Settings.java +++ b/server/src/main/java/org/opensearch/common/settings/Settings.java @@ -49,6 +49,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.common.xcontent.DeprecationHandler; import org.opensearch.common.xcontent.LoggingDeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.ToXContentFragment; import org.opensearch.common.xcontent.XContentBuilder; @@ -1090,7 +1091,7 @@ public Builder loadFromMap(Map map) { /** * Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)} */ - public Builder loadFromSource(String source, XContentType xContentType) { + public Builder loadFromSource(String source, MediaType xContentType) { try ( XContentParser parser = XContentFactory.xContent(xContentType) .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source) diff --git a/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java index e4b35d6ee2753..2a780606f3956 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java @@ -61,7 +61,7 @@ public class XContentHelper { /** * Creates a parser based on the bytes provided - * @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, BytesReference, XContentType)} + * @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, BytesReference, MediaType)} * to avoid content type auto-detection */ @Deprecated @@ -96,7 +96,7 @@ public static XContentParser createParser( NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, BytesReference bytes, - XContentType xContentType + MediaType xContentType ) throws IOException { Objects.requireNonNull(xContentType); Compressor compressor = CompressorFactory.compressor(bytes); @@ -124,7 +124,7 @@ public static XContentParser createParser( /** * Converts the given bytes into a map that is optionally ordered. - * @deprecated this method relies on auto-detection of content type. Use {@link #convertToMap(BytesReference, boolean, XContentType)} + * @deprecated this method relies on auto-detection of content type. Use {@link #convertToMap(BytesReference, boolean, MediaType)} * instead with the proper {@link XContentType} */ @Deprecated @@ -135,11 +135,29 @@ public static Tuple> convertToMap(BytesReferen /** * Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null. + * + * @deprecated use {@link #convertToMap(BytesReference, boolean, MediaType)} instead */ - public static Tuple> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType) - throws OpenSearchParseException { + @Deprecated + public static Tuple> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType) { + if (Objects.isNull(xContentType) == false && xContentType instanceof XContentType == false) { + throw new IllegalArgumentException( + "XContentHelper.convertToMap does not support media type [" + xContentType.getClass().getName() + "]" + ); + } + return (Tuple>) convertToMap(bytes, ordered, (MediaType) xContentType); + } + + /** + * Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null. + */ + public static Tuple> convertToMap( + BytesReference bytes, + boolean ordered, + MediaType xContentType + ) throws OpenSearchParseException { try { - final XContentType contentType; + final MediaType contentType; InputStream input; Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { @@ -243,7 +261,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b return convertToJson(bytes, reformatJson, prettyPrint, XContentFactory.xContentType(bytes.toBytesRef().bytes)); } - public static String convertToJson(BytesReference bytes, boolean reformatJson, XContentType xContentType) throws IOException { + public static String convertToJson(BytesReference bytes, boolean reformatJson, MediaType xContentType) throws IOException { return convertToJson(bytes, reformatJson, false, xContentType); } @@ -260,10 +278,24 @@ public static String stripWhitespace(String json) throws IOException { return convertToJson(new BytesArray(json), true, XContentType.JSON); } + /** + * Converts the XContentType to a json string + * + * @deprecated use {@link #convertToJson(BytesReference, boolean, boolean, MediaType)} instead + */ + @Deprecated public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, XContentType xContentType) throws IOException { - Objects.requireNonNull(xContentType); - if (xContentType == XContentType.JSON && !reformatJson) { + return convertToJson(bytes, reformatJson, prettyPrint, (MediaType) xContentType); + } + + /** + * Converts the given {@link MediaType} to a json string + */ + public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, MediaType mediaType) + throws IOException { + Objects.requireNonNull(mediaType); + if (mediaType == XContentType.JSON && !reformatJson) { return bytes.utf8ToString(); } @@ -271,7 +303,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b if (bytes instanceof BytesArray) { final BytesArray array = (BytesArray) bytes; try ( - XContentParser parser = XContentFactory.xContent(xContentType) + XContentParser parser = XContentFactory.xContent(mediaType) .createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, @@ -285,7 +317,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b } else { try ( InputStream stream = bytes.streamInput(); - XContentParser parser = XContentFactory.xContent(xContentType) + XContentParser parser = XContentFactory.xContent(mediaType) .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream) ) { return toJsonString(prettyPrint, parser);