Skip to content

Commit

Permalink
update deprecation methods
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Feb 7, 2023
1 parent c5f0c89 commit 29abae8
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,11 @@ default String typeWithSubtype() {
return type() + "/" + subtype();
}

default String mediaType() {
return mediaTypeWithoutParameters();
}

String mediaTypeWithoutParameters();

XContent xContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface XContent {
/**
* The type this content handles and produces.
*/
XContentType type();
MediaType type();

byte streamSeparator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ public int index() {
return index;
}

public String mediaType() {
return mediaTypeWithoutParameters();
}

public abstract String mediaTypeWithoutParameters();

@Override
public String type() {
return "application";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static XContentBuilder contentBuilder() throws IOException {
private CborXContent() {}

@Override
public XContentType type() {
public MediaType type() {
return XContentType.CBOR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static XContentBuilder contentBuilder() throws IOException {
private JsonXContent() {}

@Override
public XContentType type() {
public MediaType type() {
return XContentType.JSON;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static XContentBuilder contentBuilder() throws IOException {
private SmileXContent() {}

@Override
public XContentType type() {
public MediaType type() {
return XContentType.SMILE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static XContentBuilder contentBuilder() throws IOException {
private YamlXContent() {}

@Override
public XContentType type() {
public MediaType type() {
return XContentType.YAML;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -107,18 +107,18 @@ 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;
}
} 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 29abae8

Please sign in to comment.