-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Introduce per REST endpoint media types #64406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pgomulka
merged 15 commits into
elastic:master
from
pgomulka:compat/introduce_per_endpoint_media_types
Nov 5, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e468fdf
Introduce per endpoint media types
pgomulka 5cfa11e
Merge branch 'master' into compat/parsed_media_type
pgomulka 66224e7
allow smile and cbor to parse charset param. See ClientYamlTestExecut…
pgomulka 29b27e8
javadocs
pgomulka 4690362
fix javadoc
pgomulka 2bda74f
code review follow up
pgomulka 7779083
minor tweaks
pgomulka dd26408
Merge branch 'master' into compat/introduce_per_endpoint_media_types
pgomulka 94b4a0a
fix test after exception msg rename
pgomulka 4385591
rename to header value
pgomulka dc61731
remove charset validation
pgomulka 4a3138d
Apply suggestions from code review
pgomulka 0b565e5
javadoc
pgomulka b35ad2c
Merge branch 'master' into compat/introduce_per_endpoint_media_types
pgomulka b908bfe
Merge branch 'compat/introduce_per_endpoint_media_types' of github.co…
pgomulka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
160 changes: 0 additions & 160 deletions
160
libs/x-content/src/main/java/org/elasticsearch/common/xcontent/MediaTypeParser.java
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
libs/x-content/src/main/java/org/elasticsearch/common/xcontent/MediaTypeRegistry.java
This file contains hidden or 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,86 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.xcontent; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * A registry for quick media type lookup. | ||
| * It allows to find media type by a header value - typeWithSubtype aka mediaType without parameters. | ||
| * I.e. application/json will return XContentType.JSON | ||
| * Also allows to find media type by a path parameter <code>format</code>. | ||
| * I.e. txt used in path _sql?format=txt will return TextFormat.PLAIN_TEXT | ||
| * | ||
| * Multiple header representations may map to a single {@link MediaType} for example, "application/json" | ||
| * and "application/vnd.elasticsearch+json" both represent a JSON MediaType. | ||
| * A MediaType can have only one query parameter representation. | ||
| * For example "json" (case insensitive) maps back to a JSON media type. | ||
| * | ||
| * Additionally, a http header may optionally have parameters. For example "application/json; charset=utf-8". | ||
| * This class also allows to define a regular expression for valid values of charset. | ||
| */ | ||
jakelandis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class MediaTypeRegistry<T extends MediaType> { | ||
|
|
||
| private Map<String, T> queryParamToMediaType = new HashMap<>(); | ||
| private Map<String, T> typeWithSubtypeToMediaType = new HashMap<>(); | ||
| private Map<String, Map<String, Pattern>> parametersMap = new HashMap<>(); | ||
|
|
||
| public T queryParamToMediaType(String format) { | ||
| if (format == null) { | ||
| return null; | ||
| } | ||
| return queryParamToMediaType.get(format.toLowerCase(Locale.ROOT)); | ||
| } | ||
|
|
||
| public T typeWithSubtypeToMediaType(String typeWithSubtype) { | ||
| return typeWithSubtypeToMediaType.get(typeWithSubtype.toLowerCase(Locale.ROOT)); | ||
| } | ||
|
|
||
| public Map<String, Pattern> parametersFor(String typeWithSubtype) { | ||
| return parametersMap.get(typeWithSubtype); | ||
| } | ||
|
|
||
| public MediaTypeRegistry<T> register(T[] mediaTypes ) { | ||
| for (T mediaType : mediaTypes) { | ||
| Set<MediaType.HeaderValue> tuples = mediaType.headerValues(); | ||
| for (MediaType.HeaderValue headerValue : tuples) { | ||
| queryParamToMediaType.put(mediaType.queryParameter(), mediaType); | ||
| typeWithSubtypeToMediaType.put(headerValue.v1(), mediaType); | ||
| parametersMap.put(headerValue.v1(), convertPatterns(headerValue.v2())); | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| private Map<String,Pattern> convertPatterns(Map<String, String> paramNameAndValueRegex) { | ||
| Map<String, Pattern> parametersForMediaType = new HashMap<>(paramNameAndValueRegex.size()); | ||
| for (Map.Entry<String, String> params : paramNameAndValueRegex.entrySet()) { | ||
| String parameterName = params.getKey().toLowerCase(Locale.ROOT); | ||
| String parameterRegex = params.getValue(); | ||
| Pattern pattern = Pattern.compile(parameterRegex, Pattern.CASE_INSENSITIVE); | ||
| parametersForMediaType.put(parameterName, pattern); | ||
| } | ||
| return parametersForMediaType; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.