Skip to content

Commit 2f0ec23

Browse files
committed
add writeRawField mediaType method
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
1 parent ded5906 commit 2f0ec23

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,12 +995,23 @@ public XContentBuilder rawField(String name, InputStream value) throws IOExcepti
995995

996996
/**
997997
* Writes a raw field with the value taken from the bytes in the stream
998+
*
999+
* @deprecated use {@link #rawField(String, InputStream, MediaType)} instead
9981000
*/
1001+
@Deprecated
9991002
public XContentBuilder rawField(String name, InputStream value, XContentType contentType) throws IOException {
10001003
generator.writeRawField(name, value, contentType);
10011004
return this;
10021005
}
10031006

1007+
/**
1008+
* Writes a raw field with the value taken from the bytes in the stream
1009+
*/
1010+
public XContentBuilder rawField(String name, InputStream value, MediaType mediaType) throws IOException {
1011+
generator.writeRawField(name, value, mediaType);
1012+
return this;
1013+
}
1014+
10041015
/**
10051016
* Writes a value with the source coming directly from the bytes in the stream
10061017
*

libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ public interface XContentGenerator extends Closeable, Flushable {
116116

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

124+
/**
125+
* Writes a raw field with the value taken from the bytes in the stream
126+
*/
127+
void writeRawField(String name, InputStream value, MediaType mediaType) throws IOException;
128+
122129
/**
123130
* Writes a raw value taken from the bytes in the stream
124131
* @deprecated use {@link #writeRawValue(InputStream, MediaType)} instead

libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ public void writeRawField(String name, InputStream content) throws IOException {
346346
writeRawField(name, content, contentType);
347347
}
348348

349+
/**
350+
* Writes a raw field with the value taken from the bytes in the stream
351+
* @deprecated use {@link #writeRawField(String, InputStream, MediaType)} instead
352+
*/
353+
@Deprecated
349354
@Override
350355
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
351356
if (mayWriteRawData(contentType) == false) {
@@ -369,6 +374,32 @@ public void writeRawField(String name, InputStream content, XContentType content
369374
}
370375
}
371376

377+
/**
378+
* Writes a raw field with the value taken from the bytes in the stream
379+
*/
380+
@Override
381+
public void writeRawField(String name, InputStream content, MediaType mediaType) throws IOException {
382+
if (mayWriteRawData(mediaType) == false) {
383+
// EMPTY is safe here because we never call namedObject when writing raw data
384+
try (
385+
XContentParser parser = XContentFactory.xContent(mediaType)
386+
// It's okay to pass the throwing deprecation handler
387+
// because we should not be writing raw fields when
388+
// generating JSON
389+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)
390+
) {
391+
parser.nextToken();
392+
writeFieldName(name);
393+
copyCurrentStructure(parser);
394+
}
395+
} else {
396+
writeStartRaw(name);
397+
flush();
398+
Streams.copy(content, os);
399+
writeEndRaw();
400+
}
401+
}
402+
372403
/**
373404
* Writes the raw value to the stream
374405
*

server/src/main/java/org/opensearch/index/reindex/ReindexRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.opensearch.common.xcontent.XContentBuilder;
5050
import org.opensearch.common.xcontent.XContentFactory;
5151
import org.opensearch.common.xcontent.XContentParser;
52-
import org.opensearch.common.xcontent.XContentType;
5352
import org.opensearch.index.VersionType;
5453
import org.opensearch.index.query.QueryBuilder;
5554
import org.opensearch.script.Script;
@@ -308,7 +307,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
308307
builder.startObject("source");
309308
if (remoteInfo != null) {
310309
builder.field("remote", remoteInfo);
311-
builder.rawField("query", remoteInfo.getQuery().streamInput(), (XContentType) RemoteInfo.QUERY_CONTENT_TYPE.mediaType());
310+
builder.rawField("query", remoteInfo.getQuery().streamInput(), RemoteInfo.QUERY_CONTENT_TYPE.mediaType());
312311
}
313312
builder.array("index", getSearchRequest().indices());
314313
getSearchRequest().source().innerToXContent(builder, params);

0 commit comments

Comments
 (0)