Skip to content

Commit

Permalink
pr review fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Jul 22, 2023
1 parent 354f142 commit 0516680
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@
import org.opensearch.core.common.text.Text;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
import org.opensearch.core.xcontent.MediaType;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.file.AccessDeniedException;
Expand Down Expand Up @@ -497,14 +495,6 @@ public final void writeBigInteger(BigInteger v) throws IOException {
writeString(v.toString());
}

public final void writeMediaType(final MediaType v) throws IOException {
if (v.getClass().isEnum()) {
writeString(v.mediaType());
} else {
throw new NotSerializableException("unable to serialize MediaType [" + v.getClass().getSimpleName() + "]");
}
}

private static byte ZERO = 0;
private static byte ONE = 1;
private static byte TWO = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,39 @@
* @opensearch.internal
*/
public final class MediaTypeParserRegistry {
private static Map<String, MediaType> formatToMediaType;
private static Map<String, MediaType> typeWithSubtypeToMediaType;
private static Map<String, MediaType> formatToMediaType = new HashMap<>();
private static Map<String, MediaType> typeWithSubtypeToMediaType = new HashMap<>();

// Default mediaType singleton
private static MediaType DEFAULT_MEDIA_TYPE;

public static void register(MediaType[] acceptedMediaTypes, Map<String, MediaType> additionalMediaTypes) {
final int size = acceptedMediaTypes.length + additionalMediaTypes.size();
Map<String, MediaType> typeMap = new HashMap<>(size);
Map<String, MediaType> formatMap = new HashMap<>(size);
// ensures the map is not overwritten:
Map<String, MediaType> typeMap = new HashMap<>(typeWithSubtypeToMediaType);
Map<String, MediaType> formatMap = new HashMap<>(formatToMediaType);
for (MediaType mediaType : acceptedMediaTypes) {
if (formatMap.containsKey(mediaType.format())) {
throw new IllegalArgumentException("unable to register mediaType: [" + mediaType.format() + "]. Type already exists.");
}
typeMap.put(mediaType.typeWithSubtype(), mediaType);
formatMap.put(mediaType.format(), mediaType);
}
for (Map.Entry<String, MediaType> entry : additionalMediaTypes.entrySet()) {
String typeWithSubtype = entry.getKey();
MediaType mediaType = entry.getValue();
String typeWithSubtype = entry.getKey().toLowerCase(Locale.ROOT);
if (typeMap.containsKey(typeWithSubtype)) {
throw new IllegalArgumentException(
"unable to register mediaType: ["
+ entry.getKey()
+ "]. "
+ "Type already exists and is mapped to: [."
+ entry.getValue().format()
+ "]"
);
}

typeMap.put(typeWithSubtype.toLowerCase(Locale.ROOT), mediaType);
formatMap.put(mediaType.format(), mediaType);
MediaType mediaType = entry.getValue();
typeMap.put(typeWithSubtype, mediaType);
formatMap.putIfAbsent(mediaType.format(), mediaType); // ignore if the additional type mapping already exists
}

formatToMediaType = Map.copyOf(formatMap);
Expand Down

0 comments on commit 0516680

Please sign in to comment.