Skip to content

Commit 97d01be

Browse files
nknizeshiv0408
authored andcommitted
[Refactor] MediaTypeParser to MediaTypeParserRegistry (opensearch-project#8636)
Refactors the static MediaTypeParser utility to a dynamic registry to support registration of new MediaType definitions. This is a next step to decoupling the x-content library from core classes in the server module to support modularity and cloud or serverless implementations. Signed-off-by: Nicholas Walter Knize <[email protected]> Signed-off-by: Shivansh Arora <[email protected]>
1 parent 9bdcc41 commit 97d01be

File tree

87 files changed

+712
-753
lines changed

Some content is hidden

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

87 files changed

+712
-753
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
9292
- Make Span exporter configurable ([#8620](https://github.com/opensearch-project/OpenSearch/issues/8620))
9393
- Change InternalSignificantTerms to sum shard-level superset counts only in final reduce ([#8735](https://github.com/opensearch-project/OpenSearch/pull/8735))
9494
- Exclude 'benchmarks' from codecov report ([#8805](https://github.com/opensearch-project/OpenSearch/pull/8805))
95+
- [Refactor] MediaTypeParser to MediaTypeParserRegistry ([#8636](https://github.com/opensearch-project/OpenSearch/pull/8636))
9596

9697
### Deprecated
9798

client/client-benchmark-noop-api-plugin/src/main/java/org/opensearch/plugin/noop/action/bulk/RestNoopBulkAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
9999
defaultPipeline,
100100
defaultRequireAlias,
101101
true,
102-
request.getXContentType()
102+
request.getMediaType()
103103
);
104104

105105
// short circuit the call to the transport layer

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

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
157157
// Bulk API only supports newline delimited JSON or Smile. Before executing
158158
// the bulk, we need to check that all requests have the same content-type
159159
// and this content-type is supported by the Bulk API.
160-
XContentType bulkContentType = null;
160+
MediaType bulkContentType = null;
161161
for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
162162
DocWriteRequest<?> action = bulkRequest.requests().get(i);
163163

@@ -245,7 +245,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
245245
if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
246246
IndexRequest indexRequest = (IndexRequest) action;
247247
BytesReference indexSource = indexRequest.source();
248-
XContentType indexXContentType = indexRequest.getContentType();
248+
MediaType mediaType = indexRequest.getContentType();
249249

250250
try (
251251
XContentParser parser = XContentHelper.createParser(
@@ -257,7 +257,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
257257
NamedXContentRegistry.EMPTY,
258258
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
259259
indexSource,
260-
indexXContentType
260+
mediaType
261261
)
262262
) {
263263
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
@@ -266,7 +266,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
266266
}
267267
}
268268
} else if (opType == DocWriteRequest.OpType.UPDATE) {
269-
source = XContentHelper.toXContent((UpdateRequest) action, bulkContentType, false).toBytesRef();
269+
source = XContentHelper.toXContent((UpdateRequest) action, bulkContentType, ToXContent.EMPTY_PARAMS, false).toBytesRef();
270270
}
271271

272272
if (source != null) {
@@ -392,30 +392,30 @@ static Request update(UpdateRequest updateRequest) throws IOException {
392392
// set for the partial document and the upsert document. This client
393393
// only accepts update requests that have the same content types set
394394
// for both doc and upsert.
395-
XContentType xContentType = null;
395+
MediaType mediaType = null;
396396
if (updateRequest.doc() != null) {
397-
xContentType = updateRequest.doc().getContentType();
397+
mediaType = updateRequest.doc().getContentType();
398398
}
399399
if (updateRequest.upsertRequest() != null) {
400-
XContentType upsertContentType = updateRequest.upsertRequest().getContentType();
401-
if ((xContentType != null) && (xContentType != upsertContentType)) {
400+
MediaType upsertContentType = updateRequest.upsertRequest().getContentType();
401+
if ((mediaType != null) && (mediaType != upsertContentType)) {
402402
throw new IllegalStateException(
403403
"Update request cannot have different content types for doc ["
404-
+ xContentType
404+
+ mediaType
405405
+ "]"
406406
+ " and upsert ["
407407
+ upsertContentType
408408
+ "] documents"
409409
);
410410
} else {
411-
xContentType = upsertContentType;
411+
mediaType = upsertContentType;
412412
}
413413
}
414-
if (xContentType == null) {
415-
xContentType = Requests.INDEX_CONTENT_TYPE;
414+
if (mediaType == null) {
415+
mediaType = Requests.INDEX_CONTENT_TYPE;
416416
}
417417
request.addParameters(parameters.asMap());
418-
request.setEntity(createEntity(updateRequest, xContentType));
418+
request.setEntity(createEntity(updateRequest, mediaType));
419419
return request;
420420
}
421421

@@ -816,14 +816,13 @@ static Request deleteScript(DeleteStoredScriptRequest deleteStoredScriptRequest)
816816
return request;
817817
}
818818

819-
static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
820-
return createEntity(toXContent, xContentType, ToXContent.EMPTY_PARAMS);
819+
static HttpEntity createEntity(ToXContent toXContent, MediaType mediaType) throws IOException {
820+
return createEntity(toXContent, mediaType, ToXContent.EMPTY_PARAMS);
821821
}
822822

823-
static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType, ToXContent.Params toXContentParams)
824-
throws IOException {
825-
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, toXContentParams, false).toBytesRef();
826-
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));
823+
static HttpEntity createEntity(ToXContent toXContent, MediaType mediaType, ToXContent.Params toXContentParams) throws IOException {
824+
BytesRef source = XContentHelper.toXContent(toXContent, mediaType, toXContentParams, false).toBytesRef();
825+
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(mediaType));
827826
}
828827

829828
static String endpoint(String index, String id) {
@@ -868,20 +867,6 @@ static String endpoint(String[] indices, String endpoint, String type) {
868867
return new EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs(endpoint).addPathPart(type).build();
869868
}
870869

871-
/**
872-
* Returns a {@link ContentType} from a given {@link XContentType}.
873-
*
874-
* @param xContentType the {@link XContentType}
875-
* @return the {@link ContentType}
876-
*
877-
* @deprecated use {@link #createContentType(MediaType)} instead
878-
*/
879-
@Deprecated
880-
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
881-
public static ContentType createContentType(final XContentType xContentType) {
882-
return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null);
883-
}
884-
885870
/**
886871
* Returns a {@link ContentType} from a given {@link XContentType}.
887872
*
@@ -1265,28 +1250,28 @@ Params withWaitForEvents(Priority waitForEvents) {
12651250
*
12661251
* @return the {@link IndexRequest}'s content type
12671252
*/
1268-
static XContentType enforceSameContentType(IndexRequest indexRequest, @Nullable XContentType xContentType) {
1269-
XContentType requestContentType = indexRequest.getContentType();
1253+
static MediaType enforceSameContentType(IndexRequest indexRequest, @Nullable MediaType mediaType) {
1254+
MediaType requestContentType = indexRequest.getContentType();
12701255
if (requestContentType != XContentType.JSON && requestContentType != XContentType.SMILE) {
12711256
throw new IllegalArgumentException(
12721257
"Unsupported content-type found for request with content-type ["
12731258
+ requestContentType
12741259
+ "], only JSON and SMILE are supported"
12751260
);
12761261
}
1277-
if (xContentType == null) {
1262+
if (mediaType == null) {
12781263
return requestContentType;
12791264
}
1280-
if (requestContentType != xContentType) {
1265+
if (requestContentType != mediaType) {
12811266
throw new IllegalArgumentException(
12821267
"Mismatching content-type found for request with content-type ["
12831268
+ requestContentType
12841269
+ "], previous requests have content-type ["
1285-
+ xContentType
1270+
+ mediaType
12861271
+ "]"
12871272
);
12881273
}
1289-
return xContentType;
1274+
return mediaType;
12901275
}
12911276

12921277
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@
8888
import org.opensearch.core.ParseField;
8989
import org.opensearch.core.xcontent.ContextParser;
9090
import org.opensearch.core.xcontent.DeprecationHandler;
91+
import org.opensearch.core.xcontent.MediaType;
9192
import org.opensearch.core.xcontent.NamedXContentRegistry;
9293
import org.opensearch.core.xcontent.XContentParser;
93-
import org.opensearch.common.xcontent.XContentType;
9494
import org.opensearch.index.rankeval.RankEvalRequest;
9595
import org.opensearch.index.rankeval.RankEvalResponse;
9696
import org.opensearch.index.reindex.BulkByScrollResponse;
@@ -2227,11 +2227,11 @@ protected final <Resp> Resp parseEntity(final HttpEntity entity, final CheckedFu
22272227
if (entity.getContentType() == null) {
22282228
throw new IllegalStateException("OpenSearch didn't return the [Content-Type] header, unable to parse response body");
22292229
}
2230-
XContentType xContentType = XContentType.fromMediaType(entity.getContentType());
2231-
if (xContentType == null) {
2230+
MediaType medaiType = MediaType.fromMediaType(entity.getContentType());
2231+
if (medaiType == null) {
22322232
throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType());
22332233
}
2234-
try (XContentParser parser = xContentType.xContent().createParser(registry, DEPRECATION_HANDLER, entity.getContent())) {
2234+
try (XContentParser parser = medaiType.xContent().createParser(registry, DEPRECATION_HANDLER, entity.getContent())) {
22352235
return entityParser.apply(parser);
22362236
}
22372237
}

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

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
import org.opensearch.common.settings.Settings;
4545
import org.opensearch.common.xcontent.XContentFactory;
4646
import org.opensearch.common.xcontent.XContentHelper;
47-
import org.opensearch.common.xcontent.XContentType;
4847
import org.opensearch.core.ParseField;
4948
import org.opensearch.core.xcontent.DeprecationHandler;
5049
import org.opensearch.core.xcontent.MediaType;
50+
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
5151
import org.opensearch.core.xcontent.NamedXContentRegistry;
5252
import org.opensearch.core.xcontent.ToXContentObject;
5353
import org.opensearch.core.xcontent.XContentBuilder;
@@ -77,7 +77,7 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, ToX
7777
private Settings settings = EMPTY_SETTINGS;
7878

7979
private BytesReference mappings;
80-
private XContentType mappingsXContentType;
80+
private MediaType mappingsMediaType;
8181

8282
private final Set<Alias> aliases = new HashSet<>();
8383

@@ -123,17 +123,6 @@ public CreateIndexRequest settings(Settings settings) {
123123
return this;
124124
}
125125

126-
/**
127-
* The settings to create the index with (either json or yaml format)
128-
*
129-
* @deprecated use {@link #settings(String source, MediaType mediaType)} instead
130-
*/
131-
@Deprecated
132-
public CreateIndexRequest settings(String source, XContentType xContentType) {
133-
this.settings = Settings.builder().loadFromSource(source, xContentType).build();
134-
return this;
135-
}
136-
137126
/**
138127
* The settings to create the index with (either json or yaml format)
139128
*/
@@ -162,23 +151,8 @@ public BytesReference mappings() {
162151
return mappings;
163152
}
164153

165-
public XContentType mappingsXContentType() {
166-
return mappingsXContentType;
167-
}
168-
169-
/**
170-
* Adds mapping that will be added when the index gets created.
171-
*
172-
* Note that the definition should *not* be nested under a type name.
173-
*
174-
* @param source The mapping source
175-
* @param xContentType The content type of the source
176-
*
177-
* @deprecated use {@link #mapping(String source, MediaType mediaType)} instead
178-
*/
179-
@Deprecated
180-
public CreateIndexRequest mapping(String source, XContentType xContentType) {
181-
return mapping(new BytesArray(source), xContentType);
154+
public MediaType mappingsMediaType() {
155+
return mappingsMediaType;
182156
}
183157

184158
/**
@@ -213,32 +187,14 @@ public CreateIndexRequest mapping(XContentBuilder source) {
213187
*/
214188
public CreateIndexRequest mapping(Map<String, ?> source) {
215189
try {
216-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
190+
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType());
217191
builder.map(source);
218192
return mapping(BytesReference.bytes(builder), builder.contentType());
219193
} catch (IOException e) {
220194
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
221195
}
222196
}
223197

224-
/**
225-
* Adds mapping that will be added when the index gets created.
226-
*
227-
* Note that the definition should *not* be nested under a type name.
228-
*
229-
* @param source The mapping source
230-
* @param xContentType the content type of the mapping source
231-
*
232-
* @deprecated use {@link #mapping(BytesReference source, MediaType mediaType)} instead
233-
*/
234-
@Deprecated
235-
public CreateIndexRequest mapping(BytesReference source, XContentType xContentType) {
236-
Objects.requireNonNull(xContentType);
237-
mappings = source;
238-
mappingsXContentType = xContentType;
239-
return this;
240-
}
241-
242198
/**
243199
* Adds mapping that will be added when the index gets created.
244200
*
@@ -250,7 +206,7 @@ public CreateIndexRequest mapping(BytesReference source, XContentType xContentTy
250206
public CreateIndexRequest mapping(BytesReference source, MediaType mediaType) {
251207
Objects.requireNonNull(mediaType);
252208
mappings = source;
253-
mappingsXContentType = XContentType.fromMediaType(mediaType);
209+
mappingsMediaType = mediaType;
254210
return this;
255211
}
256212

@@ -278,33 +234,13 @@ public CreateIndexRequest aliases(XContentBuilder source) {
278234
return aliases(BytesReference.bytes(source), source.contentType());
279235
}
280236

281-
/**
282-
* Sets the aliases that will be associated with the index when it gets created
283-
*
284-
* @deprecated use {@link #aliases(String, MediaType)} instead
285-
*/
286-
@Deprecated
287-
public CreateIndexRequest aliases(String source, XContentType contentType) {
288-
return aliases(new BytesArray(source), contentType);
289-
}
290-
291237
/**
292238
* Sets the aliases that will be associated with the index when it gets created
293239
*/
294240
public CreateIndexRequest aliases(String source, MediaType mediaType) {
295241
return aliases(new BytesArray(source), mediaType);
296242
}
297243

298-
/**
299-
* Sets the aliases that will be associated with the index when it gets created
300-
*
301-
* @deprecated use {@link #aliases(BytesReference source, MediaType contentType)} instead
302-
*/
303-
@Deprecated
304-
public CreateIndexRequest aliases(BytesReference source, XContentType contentType) {
305-
return aliases(source, (MediaType) contentType);
306-
}
307-
308244
/**
309245
* Sets the aliases that will be associated with the index when it gets created
310246
*/
@@ -345,18 +281,6 @@ public CreateIndexRequest aliases(Collection<Alias> aliases) {
345281
return this;
346282
}
347283

348-
/**
349-
* Sets the settings and mappings as a single source.
350-
*
351-
* Note that the mapping definition should *not* be nested under a type name.
352-
*
353-
* @deprecated use {@link #source(String, MediaType)} instead
354-
*/
355-
@Deprecated
356-
public CreateIndexRequest source(String source, XContentType xContentType) {
357-
return source(new BytesArray(source), xContentType);
358-
}
359-
360284
/**
361285
* Sets the settings and mappings as a single source.
362286
*
@@ -375,20 +299,6 @@ public CreateIndexRequest source(XContentBuilder source) {
375299
return source(BytesReference.bytes(source), source.contentType());
376300
}
377301

378-
/**
379-
* Sets the settings and mappings as a single source.
380-
*
381-
* Note that the mapping definition should *not* be nested under a type name.
382-
*
383-
* @deprecated use {@link #source(BytesReference, MediaType)} instead
384-
*/
385-
@Deprecated
386-
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
387-
Objects.requireNonNull(xContentType);
388-
source(XContentHelper.convertToMap(source, false, xContentType).v2());
389-
return this;
390-
}
391-
392302
/**
393303
* Sets the settings and mappings as a single source.
394304
*
@@ -458,7 +368,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
458368

459369
if (mappings != null) {
460370
try (InputStream stream = mappings.streamInput()) {
461-
builder.rawField(MAPPINGS.getPreferredName(), stream, mappingsXContentType);
371+
builder.rawField(MAPPINGS.getPreferredName(), stream, mappingsMediaType);
462372
}
463373
}
464374

0 commit comments

Comments
 (0)