Skip to content

Commit

Permalink
next level fix hopefully no rename
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Jul 27, 2023
1 parent ef2a6b0 commit 81db3da
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@

package org.opensearch.core.xcontent;

import org.opensearch.core.xcontent.spi.MediaTypeProvider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Parses supported internet media types
Expand All @@ -48,6 +55,24 @@ public final class MediaTypeRegistry {
// Default mediaType singleton
private static MediaType DEFAULT_MEDIA_TYPE;

// JSON is a core type, so we create a static instance for implementations that require JSON format (e.g., tests)
// todo we should explore moving the concrete JSON implementation from the xcontent library to core
public static final MediaType JSON;

static {
List<MediaType> mediaTypes = new ArrayList<>();
Map<String, MediaType> amt = new HashMap<>();
for (MediaTypeProvider provider : ServiceLoader.load(MediaTypeProvider.class)) {
mediaTypes.addAll(provider.getMediaTypes());
amt = Stream.of(amt, provider.getAdditionalMediaTypes())
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
}
register(mediaTypes.toArray(new MediaType[0]), amt);
JSON = fromMediaType("application/json");
setDefaultMediaType(JSON);
}

public static void register(MediaType[] acceptedMediaTypes, Map<String, MediaType> additionalMediaTypes) {
// ensures the map is not overwritten:
Map<String, MediaType> typeMap = new HashMap<>(typeWithSubtypeToMediaType);
Expand Down

0 comments on commit 81db3da

Please sign in to comment.