|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.xcontent; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +/** |
| 29 | + * A registry for quick media type lookup. |
| 30 | + * It allows to find media type by a header value - typeWithSubtype aka mediaType without parameters. |
| 31 | + * I.e. application/json will return XContentType.JSON |
| 32 | + * Also allows to find media type by a path parameter <code>format</code>. |
| 33 | + * I.e. txt used in path _sql?format=txt will return TextFormat.PLAIN_TEXT |
| 34 | + * |
| 35 | + * Multiple header representations may map to a single {@link MediaType} for example, "application/json" |
| 36 | + * and "application/vnd.elasticsearch+json" both represent a JSON MediaType. |
| 37 | + * A MediaType can have only one query parameter representation. |
| 38 | + * For example "json" (case insensitive) maps back to a JSON media type. |
| 39 | + * |
| 40 | + * Additionally, a http header may optionally have parameters. For example "application/json; charset=utf-8". |
| 41 | + * This class also allows to define a regular expression for valid values of charset. |
| 42 | + */ |
| 43 | +public class MediaTypeRegistry<T extends MediaType> { |
| 44 | + |
| 45 | + private Map<String, T> queryParamToMediaType = new HashMap<>(); |
| 46 | + private Map<String, T> typeWithSubtypeToMediaType = new HashMap<>(); |
| 47 | + private Map<String, Map<String, Pattern>> parametersMap = new HashMap<>(); |
| 48 | + |
| 49 | + public T queryParamToMediaType(String format) { |
| 50 | + if (format == null) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + return queryParamToMediaType.get(format.toLowerCase(Locale.ROOT)); |
| 54 | + } |
| 55 | + |
| 56 | + public T typeWithSubtypeToMediaType(String typeWithSubtype) { |
| 57 | + return typeWithSubtypeToMediaType.get(typeWithSubtype.toLowerCase(Locale.ROOT)); |
| 58 | + } |
| 59 | + |
| 60 | + public Map<String, Pattern> parametersFor(String typeWithSubtype) { |
| 61 | + return parametersMap.get(typeWithSubtype); |
| 62 | + } |
| 63 | + |
| 64 | + public MediaTypeRegistry<T> register(T[] mediaTypes ) { |
| 65 | + for (T mediaType : mediaTypes) { |
| 66 | + Set<MediaType.HeaderValue> tuples = mediaType.headerValues(); |
| 67 | + for (MediaType.HeaderValue headerValue : tuples) { |
| 68 | + queryParamToMediaType.put(mediaType.queryParameter(), mediaType); |
| 69 | + typeWithSubtypeToMediaType.put(headerValue.v1(), mediaType); |
| 70 | + parametersMap.put(headerValue.v1(), convertPatterns(headerValue.v2())); |
| 71 | + } |
| 72 | + } |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + private Map<String,Pattern> convertPatterns(Map<String, String> paramNameAndValueRegex) { |
| 77 | + Map<String, Pattern> parametersForMediaType = new HashMap<>(paramNameAndValueRegex.size()); |
| 78 | + for (Map.Entry<String, String> params : paramNameAndValueRegex.entrySet()) { |
| 79 | + String parameterName = params.getKey().toLowerCase(Locale.ROOT); |
| 80 | + String parameterRegex = params.getValue(); |
| 81 | + Pattern pattern = Pattern.compile(parameterRegex, Pattern.CASE_INSENSITIVE); |
| 82 | + parametersForMediaType.put(parameterName, pattern); |
| 83 | + } |
| 84 | + return parametersForMediaType; |
| 85 | + } |
| 86 | +} |
0 commit comments