Skip to content

Commit

Permalink
[Refactor] MediaTypeParserRegistry to MediaTypeRegistry (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#8940)

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.

Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Kaushal Kumar <[email protected]>
  • Loading branch information
nknize authored and kaushalmahi12 committed Sep 12, 2023
1 parent c3b967e commit 140a4a5
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -187,7 +187,7 @@ public CreateIndexRequest mapping(XContentBuilder source) {
*/
public CreateIndexRequest mapping(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType());
XContentBuilder builder = XContentFactory.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 @@ -40,7 +40,7 @@
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -111,7 +111,7 @@ public MediaType mediaType() {
*/
public PutMappingRequest source(Map<String, ?> mappingSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType());
XContentBuilder builder = XContentFactory.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 @@ -54,7 +54,7 @@
import org.opensearch.core.common.Strings;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
Expand Down Expand Up @@ -347,7 +347,7 @@ public BigInteger readBigInteger() throws IOException {
}

public MediaType readMediaType() throws IOException {
return MediaTypeParserRegistry.fromMediaType(readString());
return MediaTypeRegistry.fromMediaType(readString());
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ default String mediaType() {
* This method will return {@code null} if no match is found
*/
static MediaType fromFormat(String mediaType) {
return MediaTypeParserRegistry.fromFormat(mediaType);
return MediaTypeRegistry.fromFormat(mediaType);
}

/**
Expand All @@ -93,7 +93,7 @@ static MediaType fromFormat(String mediaType) {
*/
static MediaType fromMediaType(String mediaTypeHeaderValue) {
mediaTypeHeaderValue = removeVersionInMediaType(mediaTypeHeaderValue);
return MediaTypeParserRegistry.fromMediaType(mediaTypeHeaderValue);
return MediaTypeRegistry.fromMediaType(mediaTypeHeaderValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* @opensearch.internal
*/
public final class MediaTypeParserRegistry {
public final class MediaTypeRegistry {
private static Map<String, MediaType> formatToMediaType = Map.of();
private static Map<String, MediaType> typeWithSubtypeToMediaType = Map.of();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.opensearch.common.xcontent.yaml.YamlXContent;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.XContent;

import java.io.IOException;
Expand Down Expand Up @@ -133,7 +133,7 @@ public XContent xContent() {

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

private int index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

package org.opensearch.common.xcontent;

import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.test.OpenSearchTestCase;

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

public void testJsonWithParameters() throws Exception {
String mediaType = "application/json";
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType).getParameters(), equalTo(Collections.emptyMap()));
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + ";").getParameters(), equalTo(Collections.emptyMap()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType).getParameters(), equalTo(Collections.emptyMap()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType + ";").getParameters(), equalTo(Collections.emptyMap()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; charset=UTF-8").getParameters(), equalTo(Map.of("charset", "utf-8")));
assertThat(
MediaTypeParserRegistry.parseMediaType(mediaType + "; charset=UTF-8").getParameters(),
equalTo(Map.of("charset", "utf-8"))
);
assertThat(
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123;charset=UTF-8").getParameters(),
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123;charset=UTF-8").getParameters(),
equalTo(Map.of("charset", "utf-8", "custom", "123"))
);
}

public void testWhiteSpaceInTypeSubtype() {
String mediaType = " application/json ";
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType).getMediaType(), equalTo(XContentType.JSON));
assertThat(MediaTypeRegistry.parseMediaType(mediaType).getMediaType(), equalTo(XContentType.JSON));

assertThat(
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123; charset=UTF-8").getParameters(),
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123; charset=UTF-8").getParameters(),
equalTo(Map.of("charset", "utf-8", "custom", "123"))
);
assertThat(
MediaTypeParserRegistry.parseMediaType(mediaType + "; custom=123;\n charset=UTF-8").getParameters(),
MediaTypeRegistry.parseMediaType(mediaType + "; custom=123;\n charset=UTF-8").getParameters(),
equalTo(Map.of("charset", "utf-8", "custom", "123"))
);

mediaType = " application / json ";
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType), is(nullValue()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType), is(nullValue()));
}

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

assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + "; key = value"), is(nullValue()));
assertThat(MediaTypeParserRegistry.parseMediaType(mediaType + "; key="), is(nullValue()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; key = value"), is(nullValue()));
assertThat(MediaTypeRegistry.parseMediaType(mediaType + "; key="), is(nullValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;

Expand Down Expand Up @@ -132,7 +132,7 @@ public XContentBuilder newBuilder(@Nullable MediaType requestContentType, @Nulla
responseContentType = requestContentType;
} else {
// default to JSON output when all else fails
responseContentType = MediaTypeParserRegistry.getDefaultMediaType();
responseContentType = MediaTypeRegistry.getDefaultMediaType();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.core.xcontent.MediaTypeParserRegistry;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.node.NodeClosedException;
import org.opensearch.node.ReportingService;
import org.opensearch.tasks.Task;
Expand Down Expand Up @@ -174,7 +174,7 @@ public void close() {}
/** Registers OpenSearch server specific exceptions (exceptions outside of core library) */
OpenSearchServerException.registerExceptions();
// set the default media type to JSON (fallback if a media type is not specified)
MediaTypeParserRegistry.setDefaultMediaType(XContentType.JSON);
MediaTypeRegistry.setDefaultMediaType(XContentType.JSON);
}

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

0 comments on commit 140a4a5

Please sign in to comment.