001/*
002 * Copyright 2002-2018 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.web.reactive.function;
018
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import reactor.core.publisher.Mono;
024
025import org.springframework.http.ReactiveHttpOutputMessage;
026import org.springframework.http.codec.HttpMessageWriter;
027import org.springframework.http.server.reactive.ServerHttpRequest;
028
029/**
030 * A combination of functions that can populate a {@link ReactiveHttpOutputMessage} body.
031 *
032 * @author Arjen Poutsma
033 * @since 5.0
034 * @param <T> the type of data to insert
035 * @param <M> the type of {@link ReactiveHttpOutputMessage} this inserter can be applied to
036 * @see BodyInserters
037 */
038@FunctionalInterface
039public interface BodyInserter<T, M extends ReactiveHttpOutputMessage> {
040
041        /**
042         * Insert into the given output message.
043         * @param outputMessage the response to insert into
044         * @param context the context to use
045         * @return a {@code Mono} that indicates completion or error
046         */
047        Mono<Void> insert(M outputMessage, Context context);
048
049
050        /**
051         * Defines the context used during the insertion.
052         */
053        interface Context {
054
055                /**
056                 * Return the {@link HttpMessageWriter HttpMessageWriters} to be used for response body conversion.
057                 * @return the stream of message writers
058                 */
059                List<HttpMessageWriter<?>> messageWriters();
060
061                /**
062                 * Optionally return the {@link ServerHttpRequest}, if present.
063                 */
064                Optional<ServerHttpRequest> serverRequest();
065
066                /**
067                 * Return the map of hints to use for response body conversion.
068                 */
069                Map<String, Object> hints();
070        }
071
072}