Skip to content

Commit

Permalink
add writeRawField mediaType method
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 8, 2023
1 parent ded5906 commit 2f0ec23
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -995,12 +995,23 @@ public XContentBuilder rawField(String name, InputStream value) throws IOExcepti

/**
* Writes a raw field with the value taken from the bytes in the stream
*
* @deprecated use {@link #rawField(String, InputStream, MediaType)} instead
*/
@Deprecated
public XContentBuilder rawField(String name, InputStream value, XContentType contentType) throws IOException {
generator.writeRawField(name, value, contentType);
return this;
}

/**
* Writes a raw field with the value taken from the bytes in the stream
*/
public XContentBuilder rawField(String name, InputStream value, MediaType mediaType) throws IOException {
generator.writeRawField(name, value, mediaType);
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 @@ -116,9 +116,16 @@ public interface XContentGenerator extends Closeable, Flushable {

/**
* Writes a raw field with the value taken from the bytes in the stream
* @deprecated use {@link #writeRawField(String, InputStream, MediaType)} instead
*/
@Deprecated
void writeRawField(String name, InputStream value, XContentType xContentType) throws IOException;

/**
* Writes a raw field with the value taken from the bytes in the stream
*/
void writeRawField(String name, InputStream value, MediaType mediaType) throws IOException;

/**
* Writes a raw value taken from the bytes in the stream
* @deprecated use {@link #writeRawValue(InputStream, MediaType)} instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ public void writeRawField(String name, InputStream content) throws IOException {
writeRawField(name, content, contentType);
}

/**
* Writes a raw field with the value taken from the bytes in the stream
* @deprecated use {@link #writeRawField(String, InputStream, MediaType)} instead
*/
@Deprecated
@Override
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
if (mayWriteRawData(contentType) == false) {
Expand All @@ -369,6 +374,32 @@ public void writeRawField(String name, InputStream content, XContentType content
}
}

/**
* Writes a raw field with the value taken from the bytes in the stream
*/
@Override
public void writeRawField(String name, InputStream content, MediaType mediaType) throws IOException {
if (mayWriteRawData(mediaType) == false) {
// EMPTY is safe here because we never call namedObject when writing raw data
try (
XContentParser parser = XContentFactory.xContent(mediaType)
// It's okay to pass the throwing deprecation handler
// because we should not be writing raw fields when
// generating JSON
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)
) {
parser.nextToken();
writeFieldName(name);
copyCurrentStructure(parser);
}
} else {
writeStartRaw(name);
flush();
Streams.copy(content, os);
writeEndRaw();
}
}

/**
* Writes the raw value to the stream
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
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 @@ -308,7 +307,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(), (XContentType) RemoteInfo.QUERY_CONTENT_TYPE.mediaType());
builder.rawField("query", remoteInfo.getQuery().streamInput(), RemoteInfo.QUERY_CONTENT_TYPE.mediaType());
}
builder.array("index", getSearchRequest().indices());
getSearchRequest().source().innerToXContent(builder, params);
Expand Down

0 comments on commit 2f0ec23

Please sign in to comment.