forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register MediaTypes through SPI (opensearch-project#8938)
* Register MediaTypes through SPI This commit provides a new SPI interface MediaContentProvider. Modules, Plugins, Extensions, implement this interface and provide the concrete MediaType implementations (and MIME aliases) through getMediaTypes and getAdditionalMediaTypes, respectively. This enables downstream extensions (e.g., serverless or cloud native implementations) to register their own custom MediaType and define the serialization format that is registered when the classloader loads the MediaTypeRegistry instead of having to register the types explicitly in application code. Signed-off-by: Nicholas Walter Knize <nknize@apache.org> * pass the MediaTypeProvider classloader to the SPI ServiceLoader Signed-off-by: Nicholas Walter Knize <nknize@apache.org> --------- Signed-off-by: Nicholas Walter Knize <nknize@apache.org> Signed-off-by: Ivan Brusic <ivan.brusic@flocksafety.com>
- Loading branch information
Showing
8 changed files
with
120 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
libs/core/src/main/java/org/opensearch/core/xcontent/spi/MediaTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.core.xcontent.spi; | ||
|
||
import org.opensearch.core.xcontent.MediaType; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Service Provider Interface for plugins, modules, extensions providing | ||
* their own Media Types | ||
* | ||
* @opensearch.experimental | ||
* @opensearch.api | ||
*/ | ||
public interface MediaTypeProvider { | ||
/** Extensions that implement their own concrete {@link MediaType}s provide them through this interface method */ | ||
List<MediaType> getMediaTypes(); | ||
|
||
/** Extensions that implement additional {@link MediaType} aliases provide them through this interface method */ | ||
Map<String, MediaType> getAdditionalMediaTypes(); | ||
} |
10 changes: 10 additions & 0 deletions
10
libs/core/src/main/java/org/opensearch/core/xcontent/spi/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** Service Provider Interface for extensible media types */ | ||
package org.opensearch.core.xcontent.spi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
libs/x-content/src/main/java/org/opensearch/common/xcontent/spi/XContentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.xcontent.spi; | ||
|
||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.xcontent.MediaType; | ||
import org.opensearch.core.xcontent.spi.MediaTypeProvider; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Media Type implementations provided by xcontent library | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class XContentProvider implements MediaTypeProvider { | ||
/** Returns the concrete {@link MediaType} provided by the xcontent library */ | ||
@Override | ||
public List<MediaType> getMediaTypes() { | ||
return List.of(XContentType.values()); | ||
} | ||
|
||
/** Returns the additional {@link MediaType} aliases provided by the xcontent library */ | ||
@Override | ||
public Map<String, MediaType> getAdditionalMediaTypes() { | ||
return Map.of("application/*", XContentType.JSON, "application/x-ndjson", XContentType.JSON); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
libs/x-content/src/main/java/org/opensearch/common/xcontent/spi/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** SPI implementation for the xcontent library */ | ||
package org.opensearch.common.xcontent.spi; |
9 changes: 9 additions & 0 deletions
9
...t/src/main/resources/META-INF/services/org.opensearch.core.xcontent.spi.MediaTypeProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
# | ||
|
||
org.opensearch.common.xcontent.spi.XContentProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters