Skip to content

Commit 0516680

Browse files
committed
pr review fixes
Signed-off-by: Nicholas Walter Knize <[email protected]>
1 parent 354f142 commit 0516680

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@
5353
import org.opensearch.core.common.text.Text;
5454
import org.opensearch.common.unit.TimeValue;
5555
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
56-
import org.opensearch.core.xcontent.MediaType;
5756

5857
import java.io.EOFException;
5958
import java.io.FileNotFoundException;
6059
import java.io.IOException;
61-
import java.io.NotSerializableException;
6260
import java.io.OutputStream;
6361
import java.math.BigInteger;
6462
import java.nio.file.AccessDeniedException;
@@ -497,14 +495,6 @@ public final void writeBigInteger(BigInteger v) throws IOException {
497495
writeString(v.toString());
498496
}
499497

500-
public final void writeMediaType(final MediaType v) throws IOException {
501-
if (v.getClass().isEnum()) {
502-
writeString(v.mediaType());
503-
} else {
504-
throw new NotSerializableException("unable to serialize MediaType [" + v.getClass().getSimpleName() + "]");
505-
}
506-
}
507-
508498
private static byte ZERO = 0;
509499
private static byte ONE = 1;
510500
private static byte TWO = 2;

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,39 @@
4242
* @opensearch.internal
4343
*/
4444
public final class MediaTypeParserRegistry {
45-
private static Map<String, MediaType> formatToMediaType;
46-
private static Map<String, MediaType> typeWithSubtypeToMediaType;
45+
private static Map<String, MediaType> formatToMediaType = new HashMap<>();
46+
private static Map<String, MediaType> typeWithSubtypeToMediaType = new HashMap<>();
4747

4848
// Default mediaType singleton
4949
private static MediaType DEFAULT_MEDIA_TYPE;
5050

5151
public static void register(MediaType[] acceptedMediaTypes, Map<String, MediaType> additionalMediaTypes) {
52-
final int size = acceptedMediaTypes.length + additionalMediaTypes.size();
53-
Map<String, MediaType> typeMap = new HashMap<>(size);
54-
Map<String, MediaType> formatMap = new HashMap<>(size);
52+
// ensures the map is not overwritten:
53+
Map<String, MediaType> typeMap = new HashMap<>(typeWithSubtypeToMediaType);
54+
Map<String, MediaType> formatMap = new HashMap<>(formatToMediaType);
5555
for (MediaType mediaType : acceptedMediaTypes) {
56+
if (formatMap.containsKey(mediaType.format())) {
57+
throw new IllegalArgumentException("unable to register mediaType: [" + mediaType.format() + "]. Type already exists.");
58+
}
5659
typeMap.put(mediaType.typeWithSubtype(), mediaType);
5760
formatMap.put(mediaType.format(), mediaType);
5861
}
5962
for (Map.Entry<String, MediaType> entry : additionalMediaTypes.entrySet()) {
60-
String typeWithSubtype = entry.getKey();
61-
MediaType mediaType = entry.getValue();
63+
String typeWithSubtype = entry.getKey().toLowerCase(Locale.ROOT);
64+
if (typeMap.containsKey(typeWithSubtype)) {
65+
throw new IllegalArgumentException(
66+
"unable to register mediaType: ["
67+
+ entry.getKey()
68+
+ "]. "
69+
+ "Type already exists and is mapped to: [."
70+
+ entry.getValue().format()
71+
+ "]"
72+
);
73+
}
6274

63-
typeMap.put(typeWithSubtype.toLowerCase(Locale.ROOT), mediaType);
64-
formatMap.put(mediaType.format(), mediaType);
75+
MediaType mediaType = entry.getValue();
76+
typeMap.put(typeWithSubtype, mediaType);
77+
formatMap.putIfAbsent(mediaType.format(), mediaType); // ignore if the additional type mapping already exists
6578
}
6679

6780
formatToMediaType = Map.copyOf(formatMap);

0 commit comments

Comments
 (0)