Skip to content
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

Introduce per REST endpoint media types #64406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,45 @@

package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.collect.Tuple;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
* Abstracts a <a href="http://en.wikipedia.org/wiki/Internet_media_type">Media Type</a> and a format parameter.
* Abstracts a <a href="http://en.wikipedia.org/wiki/Internet_media_type">Media Type</a> and a query parameter <code>format</code>.
* Media types are used as values on Content-Type and Accept headers
* format is an URL parameter, specifies response media type.
*/
public interface MediaType {
/**
* Returns a type part of a MediaType
* i.e. application for application/json
*/
String type();
String COMPATIBLE_WITH_PARAMETER_NAME = "compatible-with";
String VERSION_PATTERN = "\\d+";
jakelandis marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a subtype part of a MediaType.
* i.e. json for application/json
* Returns a corresponding format path parameter for a MediaType.
* i.e. ?format=txt for plain/text media type
*/
String subtype();
String queryParameter();

/**
* Returns a corresponding format for a MediaType. i.e. json for application/json media type
* Can differ from the MediaType's subtype i.e plain/text has a subtype of text but format is txt
* Returns a set of HeaderValues - allowed media type values on Accept or Content-Type headers
* Also defines media type parameters for validation.
*/
String format();
Set<HeaderValue> headerValues();

/**
* returns a string representation of a media type.
* A class to represent supported mediaType values i.e. application/json and parameters to be validated.
* Parameters for validation is a map where a key is a parameter name, value is a parameter regex which is used for validation.
* Regex will be applied with case insensitivity.
*/
default String typeWithSubtype(){
return type() + "/" + subtype();
class HeaderValue extends Tuple<String, Map<String, String>> {
public HeaderValue(String headerValue, Map<String, String> parametersForValidation) {
super(headerValue, parametersForValidation);
}

public HeaderValue(String headerValue) {
this(headerValue, Collections.emptyMap());
}
}
}

This file was deleted.

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;
}
}
Loading