Skip to content

Commit f25e217

Browse files
[Refactor] MediaTypeParserRegistry to MediaTypeRegistry (#8940) (#9037)
This commit rote refactors MediaTypeParserRegistry to MediaTypeRegistry to make the class naming align with the intention of the logic. The MediaTypeRegistry is a mechanism for downstream extensions to register concrete MediaTypes thus having Parser in the name is unneeded. (cherry picked from commit 3c973ba) Signed-off-by: Nicholas Walter Knize <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f2b1f2b commit f25e217

File tree

9 files changed

+27
-30
lines changed

9 files changed

+27
-30
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import org.opensearch.core.ParseField;
4848
import org.opensearch.core.xcontent.DeprecationHandler;
4949
import org.opensearch.core.xcontent.MediaType;
50-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
50+
import org.opensearch.core.xcontent.MediaTypeRegistry;
5151
import org.opensearch.core.xcontent.NamedXContentRegistry;
5252
import org.opensearch.core.xcontent.ToXContentObject;
5353
import org.opensearch.core.xcontent.XContentBuilder;
@@ -187,7 +187,7 @@ public CreateIndexRequest mapping(XContentBuilder source) {
187187
*/
188188
public CreateIndexRequest mapping(Map<String, ?> source) {
189189
try {
190-
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType());
190+
XContentBuilder builder = XContentFactory.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/PutMappingRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.opensearch.core.common.bytes.BytesReference;
4141
import org.opensearch.common.xcontent.XContentFactory;
4242
import org.opensearch.core.xcontent.MediaType;
43-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
43+
import org.opensearch.core.xcontent.MediaTypeRegistry;
4444
import org.opensearch.core.xcontent.ToXContent;
4545
import org.opensearch.core.xcontent.ToXContentObject;
4646
import org.opensearch.core.xcontent.XContentBuilder;
@@ -111,7 +111,7 @@ public MediaType mediaType() {
111111
*/
112112
public PutMappingRequest source(Map<String, ?> mappingSource) {
113113
try {
114-
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType());
114+
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeRegistry.getDefaultMediaType());
115115
builder.map(mappingSource);
116116
return source(builder);
117117
} catch (IOException e) {

libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.opensearch.core.common.Strings;
5555
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
5656
import org.opensearch.core.xcontent.MediaType;
57-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
57+
import org.opensearch.core.xcontent.MediaTypeRegistry;
5858

5959
import java.io.ByteArrayInputStream;
6060
import java.io.EOFException;
@@ -347,7 +347,7 @@ public BigInteger readBigInteger() throws IOException {
347347
}
348348

349349
public MediaType readMediaType() throws IOException {
350-
return MediaTypeParserRegistry.fromMediaType(readString());
350+
return MediaTypeRegistry.fromMediaType(readString());
351351
}
352352

353353
@Nullable

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ default String mediaType() {
8282
* This method will return {@code null} if no match is found
8383
*/
8484
static MediaType fromFormat(String mediaType) {
85-
return MediaTypeParserRegistry.fromFormat(mediaType);
85+
return MediaTypeRegistry.fromFormat(mediaType);
8686
}
8787

8888
/**
@@ -93,7 +93,7 @@ static MediaType fromFormat(String mediaType) {
9393
*/
9494
static MediaType fromMediaType(String mediaTypeHeaderValue) {
9595
mediaTypeHeaderValue = removeVersionInMediaType(mediaTypeHeaderValue);
96-
return MediaTypeParserRegistry.fromMediaType(mediaTypeHeaderValue);
96+
return MediaTypeRegistry.fromMediaType(mediaTypeHeaderValue);
9797
}
9898

9999
/**

libs/core/src/main/java/org/opensearch/core/xcontent/MediaTypeParserRegistry.java renamed to libs/core/src/main/java/org/opensearch/core/xcontent/MediaTypeRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* @opensearch.internal
4343
*/
44-
public final class MediaTypeParserRegistry {
44+
public final class MediaTypeRegistry {
4545
private static Map<String, MediaType> formatToMediaType = Map.of();
4646
private static Map<String, MediaType> typeWithSubtypeToMediaType = Map.of();
4747

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.opensearch.common.xcontent.yaml.YamlXContent;
3939
import org.opensearch.core.common.io.stream.StreamOutput;
4040
import org.opensearch.core.xcontent.MediaType;
41-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
41+
import org.opensearch.core.xcontent.MediaTypeRegistry;
4242
import org.opensearch.core.xcontent.XContent;
4343

4444
import java.io.IOException;
@@ -133,7 +133,7 @@ public XContent xContent() {
133133

134134
static {
135135
/** a parser of media types */
136-
MediaTypeParserRegistry.register(XContentType.values(), Map.of("application/*", JSON, "application/x-ndjson", JSON));
136+
MediaTypeRegistry.register(XContentType.values(), Map.of("application/*", JSON, "application/x-ndjson", JSON));
137137
}
138138

139139
private int index;

libs/x-content/src/test/java/org/opensearch/common/xcontent/MediaTypeParserTests.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
package org.opensearch.common.xcontent;
3434

35-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
35+
import org.opensearch.core.xcontent.MediaTypeRegistry;
3636
import org.opensearch.test.OpenSearchTestCase;
3737

3838
import java.util.Collections;
@@ -46,40 +46,37 @@ public class MediaTypeParserTests extends OpenSearchTestCase {
4646

4747
public void testJsonWithParameters() throws Exception {
4848
String mediaType = "application/json";
49-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType).getParameters(), equalTo(Collections.emptyMap()));
50-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + ";").getParameters(), equalTo(Collections.emptyMap()));
49+
assertThat(MediaTypeRegistry.parseMediaType(mediaType).getParameters(), equalTo(Collections.emptyMap()));
50+
assertThat(MediaTypeRegistry.parseMediaType(mediaType + ";").getParameters(), equalTo(Collections.emptyMap()));
51+
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; charset=UTF-8").getParameters(), equalTo(Map.of("charset", "utf-8")));
5152
assertThat(
52-
MediaTypeParserRegistry.parseMediaType(mediaType + "; charset=UTF-8").getParameters(),
53-
equalTo(Map.of("charset", "utf-8"))
54-
);
55-
assertThat(
56-
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123;charset=UTF-8").getParameters(),
53+
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123;charset=UTF-8").getParameters(),
5754
equalTo(Map.of("charset", "utf-8", "custom", "123"))
5855
);
5956
}
6057

6158
public void testWhiteSpaceInTypeSubtype() {
6259
String mediaType = " application/json ";
63-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType).getMediaType(), equalTo(XContentType.JSON));
60+
assertThat(MediaTypeRegistry.parseMediaType(mediaType).getMediaType(), equalTo(XContentType.JSON));
6461

6562
assertThat(
66-
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123; charset=UTF-8").getParameters(),
63+
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123; charset=UTF-8").getParameters(),
6764
equalTo(Map.of("charset", "utf-8", "custom", "123"))
6865
);
6966
assertThat(
70-
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123;\n charset=UTF-8").getParameters(),
67+
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123;\n charset=UTF-8").getParameters(),
7168
equalTo(Map.of("charset", "utf-8", "custom", "123"))
7269
);
7370

7471
mediaType = " application / json ";
75-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType), is(nullValue()));
72+
assertThat(MediaTypeRegistry.parseMediaType(mediaType), is(nullValue()));
7673
}
7774

7875
public void testInvalidParameters() {
7976
String mediaType = "application/json";
80-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + "; keyvalueNoEqualsSign"), is(nullValue()));
77+
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; keyvalueNoEqualsSign"), is(nullValue()));
8178

82-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + "; key = value"), is(nullValue()));
83-
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + "; key="), is(nullValue()));
79+
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; key = value"), is(nullValue()));
80+
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; key="), is(nullValue()));
8481
}
8582
}

server/src/main/java/org/opensearch/rest/AbstractRestChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.opensearch.common.io.stream.BytesStreamOutput;
3737
import org.opensearch.core.common.Strings;
3838
import org.opensearch.core.xcontent.MediaType;
39-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
39+
import org.opensearch.core.xcontent.MediaTypeRegistry;
4040
import org.opensearch.core.xcontent.XContentBuilder;
4141
import org.opensearch.common.xcontent.XContentFactory;
4242

@@ -132,7 +132,7 @@ public XContentBuilder newBuilder(@Nullable MediaType requestContentType, @Nulla
132132
responseContentType = requestContentType;
133133
} else {
134134
// default to JSON output when all else fails
135-
responseContentType = MediaTypeParserRegistry.getDefaultMediaType();
135+
responseContentType = MediaTypeRegistry.getDefaultMediaType();
136136
}
137137
}
138138

server/src/main/java/org/opensearch/transport/TransportService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import org.opensearch.common.xcontent.XContentType;
6565
import org.opensearch.core.common.Strings;
6666
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
67-
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
67+
import org.opensearch.core.xcontent.MediaTypeRegistry;
6868
import org.opensearch.node.NodeClosedException;
6969
import org.opensearch.node.ReportingService;
7070
import org.opensearch.tasks.Task;
@@ -177,7 +177,7 @@ public void close() {}
177177
/** Registers OpenSearch server specific exceptions (exceptions outside of core library) */
178178
OpenSearchServerException.registerExceptions();
179179
// set the default media type to JSON (fallback if a media type is not specified)
180-
MediaTypeParserRegistry.setDefaultMediaType(XContentType.JSON);
180+
MediaTypeRegistry.setDefaultMediaType(XContentType.JSON);
181181
}
182182

183183
/** does nothing. easy way to ensure class is loaded so the above static block is called to register the streamables */

0 commit comments

Comments
 (0)