Skip to content

Commit

Permalink
[Refactor] XContentFactory contentType introspection to MediaTypePars…
Browse files Browse the repository at this point in the history
…erRegistry

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>
  • Loading branch information
nknize committed Jul 28, 2023
1 parent 723cab6 commit e6cf81f
Show file tree
Hide file tree
Showing 118 changed files with 711 additions and 667 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public CreateIndexRequest mapping(XContentBuilder source) {
*/
public CreateIndexRequest mapping(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
builder.map(source);
return mapping(BytesReference.bytes(builder), builder.contentType());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -267,7 +268,7 @@ public PutIndexTemplateRequest mapping(Map<String, Object> source) {

private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
builder.map(source);
MediaType mediaType = builder.contentType();
Objects.requireNonNull(mediaType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.opensearch.client.TimedRequest;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
Expand Down Expand Up @@ -111,7 +110,7 @@ public MediaType mediaType() {
*/
public PutMappingRequest source(Map<String, ?> mappingSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
builder.map(mappingSource);
return source(builder);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContent;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.test.OpenSearchTestCase;
Expand All @@ -60,7 +59,7 @@ public final void testFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());

final XContent xContent = XContentFactory.xContent(xContentType);
final XContent xContent = xContentType.xContent();
final XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, bytes.streamInput());
final S serverInstance = doParseToServerInstance(parser);
assertInstances(serverInstance, clientTestInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContent;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.test.OpenSearchTestCase;
Expand All @@ -59,7 +58,7 @@ public final void testFromXContent() throws IOException {
final S serverTestInstance = createServerTestInstance(xContentType);
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, getParams(), randomBoolean());

final XContent xContent = XContentFactory.xContent(xContentType);
final XContent xContent = xContentType.xContent();
final XContentParser parser = xContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.opensearch.client.AbstractResponseTestCase;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;

Expand All @@ -61,7 +61,7 @@ static class SourceOnlyResponse implements ToXContentObject {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// this implementation copied from RestGetSourceAction.RestGetSourceResponseListener::buildResponse
try (InputStream stream = source.streamInput()) {
builder.rawValue(stream, XContentHelper.xContentType(source));
builder.rawValue(stream, MediaTypeRegistry.xContentType(source));
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContent;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.index.Index;
Expand Down Expand Up @@ -194,7 +193,7 @@ public final void testBwcFromXContent() throws IOException {

final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes = toShuffledXContent(expected, xContentType, getParams(), randomBoolean());
final XContent xContent = XContentFactory.xContent(xContentType);
final XContent xContent = xContentType.xContent();
final XContentParser parser = xContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
Expand All @@ -215,7 +214,7 @@ public final void testBwcFromXContent() throws IOException {

final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes = toShuffledXContent(expected, xContentType, getParams(), randomBoolean());
final XContent xContent = XContentFactory.xContent(xContentType);
final XContent xContent = xContentType.xContent();
final XContentParser parser = xContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -262,7 +262,7 @@ private static AliasMetadata randomAliasMetadata(String name) {
}

static XContentBuilder randomMapping(String type, XContentType xContentType) throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
XContentBuilder builder = MediaTypeRegistry.contentBuilder(xContentType);
builder.startObject().startObject(type);

randomMappingFields(builder, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

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

import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;

/**
Expand Down Expand Up @@ -69,12 +71,20 @@ default String typeWithSubtype() {

XContent xContent();

boolean detectedXContent(final byte[] bytes, int offset, int length);

boolean detectedXContent(final CharSequence content, final int length);

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

String mediaTypeWithoutParameters();

XContentBuilder contentBuilder() throws IOException;

XContentBuilder contentBuilder(final OutputStream os) throws IOException;

/**
* Accepts a format string, which is most of the time is equivalent to {@link MediaType#subtype()}
* and attempts to match the value to an {@link MediaType}.
Expand Down
Loading

0 comments on commit e6cf81f

Please sign in to comment.