Skip to content

Commit 36c37ac

Browse files
authored
[Refactor] XContentFactory contentType introspection to MediaTypeParserRegistry (opensearch-project#8826)
XContentFactory is tightly coupled to concrete XContentType. This commit builds on the MediaType abstractions, specifically MediaTypeParserRegistry, to decouple contentType introspection (e.g., determining type from byte streams) from concrete XContentTypes. This enables downstream extensions (e.g., serverless or cloud native implementations) to register their own custom XContentType and define the serialization format for proper content introspection. This also removes the tight coupling of :libs:opensearch-x-content with abstract interface contracts further enabling modularity. Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
1 parent b282005 commit 36c37ac

File tree

118 files changed

+711
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+711
-667
lines changed

client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public CreateIndexRequest mapping(XContentBuilder source) {
187187
*/
188188
public CreateIndexRequest mapping(Map<String, ?> source) {
189189
try {
190-
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
190+
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
191191
builder.map(source);
192192
return mapping(BytesReference.bytes(builder), builder.contentType());
193193
} catch (IOException e) {

client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.opensearch.common.xcontent.json.JsonXContent;
4949
import org.opensearch.common.xcontent.support.XContentMapValues;
5050
import org.opensearch.core.xcontent.DeprecationHandler;
51+
import org.opensearch.core.xcontent.MediaTypeRegistry;
5152
import org.opensearch.core.xcontent.NamedXContentRegistry;
5253
import org.opensearch.core.xcontent.ToXContentFragment;
5354
import org.opensearch.core.xcontent.XContentBuilder;
@@ -267,7 +268,7 @@ public PutIndexTemplateRequest mapping(Map<String, Object> source) {
267268

268269
private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
269270
try {
270-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
271+
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
271272
builder.map(source);
272273
MediaType mediaType = builder.contentType();
273274
Objects.requireNonNull(mediaType);

client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.opensearch.client.TimedRequest;
3939
import org.opensearch.core.common.bytes.BytesArray;
4040
import org.opensearch.core.common.bytes.BytesReference;
41-
import org.opensearch.common.xcontent.XContentFactory;
4241
import org.opensearch.core.xcontent.MediaType;
4342
import org.opensearch.core.xcontent.MediaTypeRegistry;
4443
import org.opensearch.core.xcontent.ToXContent;
@@ -111,7 +110,7 @@ public MediaType mediaType() {
111110
*/
112111
public PutMappingRequest source(Map<String, ?> mappingSource) {
113112
try {
114-
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
113+
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
115114
builder.map(mappingSource);
116115
return source(builder);
117116
} catch (IOException e) {

client/rest-high-level/src/test/java/org/opensearch/client/AbstractRequestTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.opensearch.core.xcontent.NamedXContentRegistry;
3737
import org.opensearch.core.xcontent.ToXContent;
3838
import org.opensearch.core.xcontent.XContent;
39-
import org.opensearch.common.xcontent.XContentFactory;
4039
import org.opensearch.core.xcontent.XContentParser;
4140
import org.opensearch.common.xcontent.XContentType;
4241
import org.opensearch.test.OpenSearchTestCase;
@@ -60,7 +59,7 @@ public final void testFromXContent() throws IOException {
6059
final XContentType xContentType = randomFrom(XContentType.values());
6160
final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
6261

63-
final XContent xContent = XContentFactory.xContent(xContentType);
62+
final XContent xContent = xContentType.xContent();
6463
final XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, bytes.streamInput());
6564
final S serverInstance = doParseToServerInstance(parser);
6665
assertInstances(serverInstance, clientTestInstance);

client/rest-high-level/src/test/java/org/opensearch/client/AbstractResponseTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.opensearch.core.xcontent.NamedXContentRegistry;
3737
import org.opensearch.core.xcontent.ToXContent;
3838
import org.opensearch.core.xcontent.XContent;
39-
import org.opensearch.common.xcontent.XContentFactory;
4039
import org.opensearch.core.xcontent.XContentParser;
4140
import org.opensearch.common.xcontent.XContentType;
4241
import org.opensearch.test.OpenSearchTestCase;
@@ -59,7 +58,7 @@ public final void testFromXContent() throws IOException {
5958
final S serverTestInstance = createServerTestInstance(xContentType);
6059
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, getParams(), randomBoolean());
6160

62-
final XContent xContent = XContentFactory.xContent(xContentType);
61+
final XContent xContent = xContentType.xContent();
6362
final XContentParser parser = xContent.createParser(
6463
NamedXContentRegistry.EMPTY,
6564
LoggingDeprecationHandler.INSTANCE,

client/rest-high-level/src/test/java/org/opensearch/client/core/GetSourceResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
import org.opensearch.client.AbstractResponseTestCase;
3636
import org.opensearch.core.common.bytes.BytesArray;
3737
import org.opensearch.core.common.bytes.BytesReference;
38+
import org.opensearch.core.xcontent.MediaTypeRegistry;
3839
import org.opensearch.core.xcontent.ToXContentObject;
3940
import org.opensearch.core.xcontent.XContentBuilder;
40-
import org.opensearch.common.xcontent.XContentHelper;
4141
import org.opensearch.core.xcontent.XContentParser;
4242
import org.opensearch.common.xcontent.XContentType;
4343

@@ -61,7 +61,7 @@ static class SourceOnlyResponse implements ToXContentObject {
6161
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
6262
// this implementation copied from RestGetSourceAction.RestGetSourceResponseListener::buildResponse
6363
try (InputStream stream = source.streamInput()) {
64-
builder.rawValue(stream, XContentHelper.xContentType(source));
64+
builder.rawValue(stream, MediaTypeRegistry.xContentType(source));
6565
}
6666
return builder;
6767
}

client/rest-high-level/src/test/java/org/opensearch/client/indices/CloseIndexResponseTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
4040
import org.opensearch.core.xcontent.NamedXContentRegistry;
4141
import org.opensearch.core.xcontent.XContent;
42-
import org.opensearch.common.xcontent.XContentFactory;
4342
import org.opensearch.core.xcontent.XContentParser;
4443
import org.opensearch.common.xcontent.XContentType;
4544
import org.opensearch.core.index.Index;
@@ -194,7 +193,7 @@ public final void testBwcFromXContent() throws IOException {
194193

195194
final XContentType xContentType = randomFrom(XContentType.values());
196195
final BytesReference bytes = toShuffledXContent(expected, xContentType, getParams(), randomBoolean());
197-
final XContent xContent = XContentFactory.xContent(xContentType);
196+
final XContent xContent = xContentType.xContent();
198197
final XContentParser parser = xContent.createParser(
199198
NamedXContentRegistry.EMPTY,
200199
LoggingDeprecationHandler.INSTANCE,
@@ -215,7 +214,7 @@ public final void testBwcFromXContent() throws IOException {
215214

216215
final XContentType xContentType = randomFrom(XContentType.values());
217216
final BytesReference bytes = toShuffledXContent(expected, xContentType, getParams(), randomBoolean());
218-
final XContent xContent = XContentFactory.xContent(xContentType);
217+
final XContent xContent = xContentType.xContent();
219218
final XContentParser parser = xContent.createParser(
220219
NamedXContentRegistry.EMPTY,
221220
LoggingDeprecationHandler.INSTANCE,

client/rest-high-level/src/test/java/org/opensearch/client/indices/GetIndexTemplatesResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
import org.opensearch.common.compress.CompressedXContent;
4040
import org.opensearch.common.settings.Settings;
4141
import org.opensearch.core.xcontent.DeprecationHandler;
42+
import org.opensearch.core.xcontent.MediaTypeRegistry;
4243
import org.opensearch.core.xcontent.NamedXContentRegistry;
4344
import org.opensearch.core.xcontent.ToXContent;
4445
import org.opensearch.core.xcontent.XContentBuilder;
45-
import org.opensearch.common.xcontent.XContentFactory;
4646
import org.opensearch.common.xcontent.XContentHelper;
4747
import org.opensearch.core.xcontent.XContentParser;
4848
import org.opensearch.common.xcontent.XContentType;
@@ -262,7 +262,7 @@ private static AliasMetadata randomAliasMetadata(String name) {
262262
}
263263

264264
static XContentBuilder randomMapping(String type, XContentType xContentType) throws IOException {
265-
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
265+
XContentBuilder builder = MediaTypeRegistry.contentBuilder(xContentType);
266266
builder.startObject().startObject(type);
267267

268268
randomMappingFields(builder, true);

libs/core/src/main/java/org/opensearch/core/xcontent/MediaType.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import org.opensearch.core.common.io.stream.Writeable;
3636

37+
import java.io.IOException;
38+
import java.io.OutputStream;
3739
import java.util.Locale;
3840

3941
/**
@@ -69,12 +71,20 @@ default String typeWithSubtype() {
6971

7072
XContent xContent();
7173

74+
boolean detectedXContent(final byte[] bytes, int offset, int length);
75+
76+
boolean detectedXContent(final CharSequence content, final int length);
77+
7278
default String mediaType() {
7379
return mediaTypeWithoutParameters();
7480
}
7581

7682
String mediaTypeWithoutParameters();
7783

84+
XContentBuilder contentBuilder() throws IOException;
85+
86+
XContentBuilder contentBuilder(final OutputStream os) throws IOException;
87+
7888
/**
7989
* Accepts a format string, which is most of the time is equivalent to {@link MediaType#subtype()}
8090
* and attempts to match the value to an {@link MediaType}.

0 commit comments

Comments
 (0)