001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.rsocket;
018
019import java.util.Map;
020import java.util.function.BiConsumer;
021
022import org.springframework.core.ParameterizedTypeReference;
023import org.springframework.lang.Nullable;
024import org.springframework.util.MimeType;
025
026/**
027 * Stores registrations of extractors for metadata entries.
028 * Each metadata entry is decoded based on its {@code MimeType} and a name
029 * is assigned to the decoded value.
030 *
031 * @author Rossen Stoyanchev
032 * @author Brian Clozel
033 * @since 5.2
034 * @see MetadataExtractor
035 */
036public interface MetadataExtractorRegistry {
037
038        /**
039         * Decode metadata entries with the given {@link MimeType} to the specified
040         * target class, and store the decoded value in the output map under the
041         * given name.
042         * @param mimeType the mime type of metadata entries to extract
043         * @param targetType the target value type to decode to
044         * @param name assign a name for the decoded value; if not provided, then
045         * the mime type is used as the key
046         */
047        default void metadataToExtract(MimeType mimeType, Class<?> targetType, @Nullable String name) {
048                String key = name != null ? name : mimeType.toString();
049                metadataToExtract(mimeType, targetType, (value, map) -> map.put(key, value));
050        }
051
052        /**
053         * Variant of {@link #metadataToExtract(MimeType, Class, String)} that accepts
054         * {@link ParameterizedTypeReference} instead of {@link Class} for
055         * specifying a target type with generic parameters.
056         * @param mimeType the mime type of metadata entries to extract
057         * @param targetType the target value type to decode to
058         */
059        default void metadataToExtract(
060                        MimeType mimeType, ParameterizedTypeReference<?> targetType, @Nullable String name) {
061
062                String key = name != null ? name : mimeType.toString();
063                metadataToExtract(mimeType, targetType, (value, map) -> map.put(key, value));
064        }
065
066        /**
067         * Variant of {@link #metadataToExtract(MimeType, Class, String)} that allows
068         * custom logic to be used to map the decoded value to any number of values
069         * in the output map.
070         * @param mimeType the mime type of metadata entries to extract
071         * @param targetType the target value type to decode to
072         * @param mapper custom logic to add the decoded value to the output map
073         * @param <T> the target value type
074         */
075        <T> void metadataToExtract(
076                        MimeType mimeType, Class<T> targetType, BiConsumer<T, Map<String, Object>> mapper);
077
078        /**
079         * Variant of {@link #metadataToExtract(MimeType, Class, BiConsumer)} that
080         * accepts {@link ParameterizedTypeReference} instead of {@link Class} for
081         * specifying a target type with generic parameters.
082         * @param mimeType the mime type of metadata entries to extract
083         * @param type the target value type to decode to
084         * @param mapper custom logic to add the decoded value to the output map
085         * @param <T> the target value type
086         */
087        <T> void metadataToExtract(
088                        MimeType mimeType, ParameterizedTypeReference<T> type, BiConsumer<T, Map<String, Object>> mapper);
089
090}