From 739d524a865901a902d1a347a77fcb587269c661 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Tue, 7 Feb 2023 09:15:25 -0600 Subject: [PATCH] update deprecation methods Signed-off-by: Nicholas Walter Knize --- .../opensearch/client/RequestConverters.java | 20 +++++++++ .../opensearch/common/xcontent/MediaType.java | 6 +++ .../opensearch/common/xcontent/XContent.java | 2 +- .../common/xcontent/XContentBuilder.java | 11 +++++ .../common/xcontent/XContentGenerator.java | 9 +++- .../common/xcontent/XContentType.java | 6 --- .../common/xcontent/cbor/CborXContent.java | 3 +- .../common/xcontent/json/JsonXContent.java | 3 +- .../xcontent/json/JsonXContentGenerator.java | 43 ++++++++++++++++++- .../common/xcontent/smile/SmileXContent.java | 3 +- .../common/xcontent/yaml/YamlXContent.java | 3 +- .../common/xcontent/XContentHelper.java | 10 ++--- .../index/reindex/ReindexRequest.java | 3 +- 13 files changed, 103 insertions(+), 19 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java index 88e3a3a904830..13c4d6ef99dfe 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java @@ -79,6 +79,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.common.util.CollectionUtils; import org.opensearch.common.xcontent.DeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.XContent; @@ -112,6 +113,11 @@ import java.util.Map; import java.util.StringJoiner; +/** + * Converts OpenSearch writeable requests to an HTTP Request + * + * @opensearch.api + */ final class RequestConverters { static final XContentType REQUEST_BODY_CONTENT_TYPE = XContentType.JSON; @@ -867,12 +873,26 @@ static String endpoint(String[] indices, String endpoint, String type) { * * @param xContentType the {@link XContentType} * @return the {@link ContentType} + * + * @deprecated use {@link #createContentType(MediaType)} instead */ + @Deprecated @SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType") public static ContentType createContentType(final XContentType xContentType) { return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null); } + /** + * Returns a {@link ContentType} from a given {@link XContentType}. + * + * @param mediaType the {@link MediaType} + * @return the {@link ContentType} + */ + @SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType") + public static ContentType createContentType(final MediaType mediaType) { + return ContentType.create(mediaType.mediaTypeWithoutParameters(), (Charset) null); + } + /** * Utility class to help with common parameter names and patterns. Wraps * a {@link Request} and adds the parameters to it directly. diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/MediaType.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/MediaType.java index 3fad89152cc45..f38457b5ffd4c 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/MediaType.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/MediaType.java @@ -63,5 +63,11 @@ default String typeWithSubtype() { return type() + "/" + subtype(); } + default String mediaType() { + return mediaTypeWithoutParameters(); + } + + String mediaTypeWithoutParameters(); + XContent xContent(); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContent.java index c8a8aa1a0cfa3..d4fec1095930e 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContent.java @@ -46,7 +46,7 @@ public interface XContent { /** * The type this content handles and produces. */ - XContentType type(); + MediaType type(); byte streamSeparator(); 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 47e89e65a5e83..66194b543fdfc 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 @@ -1001,6 +1001,17 @@ public XContentBuilder rawField(String name, InputStream value, XContentType con return this; } + /** + * Writes a value with the source coming directly from the bytes in the stream + * + * @deprecated use {@link #rawValue(InputStream, MediaType)} instead + */ + @Deprecated + public XContentBuilder rawValue(InputStream stream, XContentType contentType) throws IOException { + generator.writeRawValue(stream, contentType); + return this; + } + /** * Writes a value with the source coming directly from the bytes in the stream */ 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 4b996e558d263..f3c2b2ba9c772 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 @@ -121,8 +121,15 @@ public interface XContentGenerator extends Closeable, Flushable { /** * Writes a raw value taken from the bytes in the stream + * @deprecated use {@link #writeRawValue(InputStream, MediaType)} instead */ - void writeRawValue(InputStream value, MediaType xContentType) throws IOException; + @Deprecated + void writeRawValue(InputStream value, XContentType xContentType) throws IOException; + + /** + * Writes a raw value taken from the bytes in the stream + */ + void writeRawValue(InputStream value, MediaType mediaType) throws IOException; void copyCurrentStructure(XContentParser parser) throws IOException; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java index 32283698beaf8..d93df5b05be4a 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java @@ -186,12 +186,6 @@ public int index() { return index; } - public String mediaType() { - return mediaTypeWithoutParameters(); - } - - public abstract String mediaTypeWithoutParameters(); - @Override public String type() { return "application"; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java index 674b6566bdec0..3eb3baf61caf4 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java @@ -37,6 +37,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; 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.XContentBuilder; @@ -75,7 +76,7 @@ public static XContentBuilder contentBuilder() throws IOException { private CborXContent() {} @Override - public XContentType type() { + public MediaType type() { return XContentType.CBOR; } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java index a7872904b1126..a5f534f93211d 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java @@ -37,6 +37,7 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; 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.XContentBuilder; @@ -77,7 +78,7 @@ public static XContentBuilder contentBuilder() throws IOException { private JsonXContent() {} @Override - public XContentType type() { + public MediaType type() { return XContentType.JSON; } 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 5be3c5c55c12a..8e11546b48bb5 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 @@ -369,8 +369,14 @@ public void writeRawField(String name, InputStream content, XContentType content } } + /** + * Writes the raw value to the stream + * + * @deprecated use {@link #writeRawValue(InputStream, MediaType)} instead + */ + @Deprecated @Override - public void writeRawValue(InputStream stream, MediaType xContentType) throws IOException { + public void writeRawValue(InputStream stream, XContentType xContentType) throws IOException { if (mayWriteRawData(xContentType) == false) { copyRawValue(stream, xContentType.xContent()); } else { @@ -384,6 +390,41 @@ public void writeRawValue(InputStream stream, MediaType xContentType) throws IOE } } + /** + * Writes the raw value to the stream + */ + @Override + public void writeRawValue(InputStream stream, MediaType mediaType) throws IOException { + if (mayWriteRawData(mediaType) == false) { + copyRawValue(stream, mediaType.xContent()); + } else { + if (generator.getOutputContext().getCurrentName() != null) { + // If we've just started a field we'll need to add the separator + generator.writeRaw(':'); + } + flush(); + Streams.copy(stream, os, false); + writeEndRaw(); + } + } + + /** + * possibly copy the whole structure to correctly filter + * + * @deprecated use {@link #mayWriteRawData(MediaType)} instead + */ + @Deprecated + private boolean mayWriteRawData(XContentType 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 + // filtered or converted + return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; + } + + /** + * possibly copy the whole structure to correctly filter + */ 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, diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java index 82925c5f84f9c..a780f9358870a 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java @@ -38,6 +38,7 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileGenerator; 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.XContentBuilder; @@ -77,7 +78,7 @@ public static XContentBuilder contentBuilder() throws IOException { private SmileXContent() {} @Override - public XContentType type() { + public MediaType type() { return XContentType.SMILE; } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java index 8ebdeca71c0ce..a1f1f1703a8f6 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java @@ -36,6 +36,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; 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.XContentBuilder; @@ -70,7 +71,7 @@ public static XContentBuilder contentBuilder() throws IOException { private YamlXContent() {} @Override - public XContentType type() { + public MediaType type() { return XContentType.YAML; } 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 2a780606f3956..5319ce222c08d 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java @@ -96,9 +96,9 @@ public static XContentParser createParser( NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, BytesReference bytes, - MediaType xContentType + MediaType mediaType ) throws IOException { - Objects.requireNonNull(xContentType); + Objects.requireNonNull(mediaType); Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { InputStream compressedInput = null; @@ -107,7 +107,7 @@ public static XContentParser createParser( if (compressedInput.markSupported() == false) { compressedInput = new BufferedInputStream(compressedInput); } - return XContentFactory.xContent(xContentType).createParser(xContentRegistry, deprecationHandler, compressedInput); + return XContentFactory.xContent(mediaType).createParser(xContentRegistry, deprecationHandler, compressedInput); } catch (Exception e) { if (compressedInput != null) compressedInput.close(); throw e; @@ -115,10 +115,10 @@ public static XContentParser createParser( } else { if (bytes instanceof BytesArray) { final BytesArray array = (BytesArray) bytes; - return xContentType.xContent() + return mediaType.xContent() .createParser(xContentRegistry, deprecationHandler, array.array(), array.offset(), array.length()); } - return xContentType.xContent().createParser(xContentRegistry, deprecationHandler, bytes.streamInput()); + return mediaType.xContent().createParser(xContentRegistry, deprecationHandler, bytes.streamInput()); } } diff --git a/server/src/main/java/org/opensearch/index/reindex/ReindexRequest.java b/server/src/main/java/org/opensearch/index/reindex/ReindexRequest.java index bd8398eab91c1..c0d1f353b3b7e 100644 --- a/server/src/main/java/org/opensearch/index/reindex/ReindexRequest.java +++ b/server/src/main/java/org/opensearch/index/reindex/ReindexRequest.java @@ -49,6 +49,7 @@ import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentParser; +import org.opensearch.common.xcontent.XContentType; import org.opensearch.index.VersionType; import org.opensearch.index.query.QueryBuilder; import org.opensearch.script.Script; @@ -307,7 +308,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject("source"); if (remoteInfo != null) { builder.field("remote", remoteInfo); - builder.rawField("query", remoteInfo.getQuery().streamInput(), RemoteInfo.QUERY_CONTENT_TYPE.type()); + builder.rawField("query", remoteInfo.getQuery().streamInput(), (XContentType) RemoteInfo.QUERY_CONTENT_TYPE.type()); } builder.array("index", getSearchRequest().indices()); getSearchRequest().source().innerToXContent(builder, params);