-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Allow parsing Content-Type and Accept headers with version #61427
Changes from 28 commits
7514d87
921bc6f
ee280ba
433de74
51467e0
ccaadbd
de13d4b
d9ee420
41150a6
2fc8f86
734031a
c04af65
474eb29
5c30813
7b760ba
5853561
b6ebd63
ff6c94f
b969f2b
0684e58
b91d472
9edc49f
c5d8166
ad673b6
1ae6a60
1ae63c6
ee1e5a0
9f4b6c3
bd4dee1
ce42f1b
0f8ae7b
c4f02ad
176302e
eab471c
e128fd7
66435dd
08f1395
3f3f0d5
427c391
e031605
4134d92
a976f5b
3e0eec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,9 @@ | |
import org.elasticsearch.common.xcontent.smile.SmileXContent; | ||
import org.elasticsearch.common.xcontent.yaml.YamlXContent; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* The content type of {@link org.elasticsearch.common.xcontent.XContent}. | ||
|
@@ -113,9 +115,24 @@ public XContent xContent() { | |
} | ||
}; | ||
|
||
public static final MediaTypeParser<XContentType> mediaTypeParser = new MediaTypeParser<>(XContentType.values(), | ||
Map.of("application/*", JSON, "application/x-ndjson", JSON)); | ||
|
||
public static final MediaTypeParser<XContentType> mediaTypeParser = new MediaTypeParser.Builder<XContentType>() | ||
.withMediaTypeAndParams("application/smile", SMILE, Collections.emptyMap()) | ||
.withMediaTypeAndParams("application/cbor", CBOR, Collections.emptyMap()) | ||
.withMediaTypeAndParams("application/json", JSON, Map.of("charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/yaml", YAML, Map.of("charset", Pattern.compile("UTF-8"))) | ||
jakelandis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.withMediaTypeAndParams("application/*", JSON, Map.of("charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/x-ndjson", JSON, Map.of("charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/vnd.elasticsearch+json", JSON, | ||
Map.of("compatible-with", Pattern.compile("\\d+"),"charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/vnd.elasticsearch+smile", SMILE, | ||
Map.of("compatible-with", Pattern.compile("\\d+"),"charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/vnd.elasticsearch+yaml", YAML, | ||
Map.of("compatible-with", Pattern.compile("\\d+"),"charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/vnd.elasticsearch+cbor", CBOR, | ||
Map.of("compatible-with", Pattern.compile("\\d+"),"charset", Pattern.compile("UTF-8"))) | ||
.withMediaTypeAndParams("application/vnd.elasticsearch+x-ndjson", JSON, | ||
Map.of("compatible-with", Pattern.compile("\\d+"),"charset", Pattern.compile("UTF-8"))) | ||
.build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the discussion: is there an easy way to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a 404 (or maybe 400) is the right thing to do here since the infrastructure for version support is in OSS and we do not have a compatible handler for V7. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this also would affect plugins like SQL. In oss we don't know about MediaTypes which are defined over there
@bpintea any thoughts on this? I guess SQL had to face this in the past There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any way to register a custom parser ? or just add the SQL values to the core parser and specificy which rule set to use when parsing ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that is an interesting idea.. We could have something like a SqlMediaTypeParser and it would either return TextFormat or XContentType. The parsing logic would remain the same in the MediaTypeParser. The idea of SqlMediaTypeParser woudl be just to abstract on the setup of the parser. |
||
|
||
/** | ||
* Accepts a format string, which is most of the time is equivalent to {@link XContentType#subtype()} | ||
|
@@ -131,19 +148,29 @@ public static XContentType fromFormat(String mediaType) { | |
* Attempts to match the given media type with the known {@link XContentType} values. This match is done in a case-insensitive manner. | ||
* The provided media type can optionally has parameters. | ||
* This method is suitable for parsing of the {@code Content-Type} and {@code Accept} HTTP headers. | ||
* This method will return {@code null} if no match is found | ||
* This method will throw IllegalArgumentException if no match is found | ||
*/ | ||
public static XContentType fromMediaType(String mediaTypeHeaderValue) { | ||
return mediaTypeParser.fromMediaType(mediaTypeHeaderValue); | ||
} | ||
|
||
|
||
private int index; | ||
|
||
XContentType(int index) { | ||
this.index = index; | ||
} | ||
|
||
public static Byte parseVersion(String mediaType) { | ||
MediaTypeParser<XContentType>.ParsedMediaType parsedMediaType = mediaTypeParser.parseMediaType(mediaType); | ||
if (parsedMediaType != null) { | ||
String version = parsedMediaType | ||
.getParameters() | ||
.get("compatible-with"); | ||
return version != null ? Byte.parseByte(version) : null; | ||
} | ||
return null; | ||
} | ||
|
||
public int index() { | ||
return index; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this prevents defaulting Accept header to
text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
bysun.net.www.protocol.http.HttpURLConnection