001/*
002 * Copyright 2002-2016 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.http;
018
019import java.util.function.Supplier;
020
021import org.reactivestreams.Publisher;
022import reactor.core.publisher.Mono;
023
024import org.springframework.core.io.buffer.DataBuffer;
025import org.springframework.core.io.buffer.DataBufferFactory;
026
027/**
028 * A "reactive" HTTP output message that accepts output as a {@link Publisher}.
029 *
030 * <p>Typically implemented by an HTTP request on the client-side or an
031 * HTTP response on the server-side.
032 *
033 * @author Arjen Poutsma
034 * @author Sebastien Deleuze
035 * @since 5.0
036 */
037public interface ReactiveHttpOutputMessage extends HttpMessage {
038
039        /**
040         * Return a {@link DataBufferFactory} that can be used to create the body.
041         * @return a buffer factory
042         * @see #writeWith(Publisher)
043         */
044        DataBufferFactory bufferFactory();
045
046        /**
047         * Register an action to apply just before the HttpOutputMessage is committed.
048         * <p><strong>Note:</strong> the supplied action must be properly deferred,
049         * e.g. via {@link Mono#defer} or {@link Mono#fromRunnable}, to ensure it's
050         * executed in the right order, relative to other actions.
051         * @param action the action to apply
052         */
053        void beforeCommit(Supplier<? extends Mono<Void>> action);
054
055        /**
056         * Whether the HttpOutputMessage is committed.
057         */
058        boolean isCommitted();
059
060        /**
061         * Use the given {@link Publisher} to write the body of the message to the
062         * underlying HTTP layer.
063         * @param body the body content publisher
064         * @return a {@link Mono} that indicates completion or error
065         */
066
067        Mono<Void> writeWith(Publisher<? extends DataBuffer> body);
068
069        /**
070         * Use the given {@link Publisher} of {@code Publishers} to write the body
071         * of the HttpOutputMessage to the underlying HTTP layer, flushing after
072         * each {@code Publisher<DataBuffer>}.
073         * @param body the body content publisher
074         * @return a {@link Mono} that indicates completion or error
075         */
076        Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body);
077
078        /**
079         * Indicate that message handling is complete, allowing for any cleanup or
080         * end-of-processing tasks to be performed such as applying header changes
081         * made via {@link #getHeaders()} to the underlying HTTP message (if not
082         * applied already).
083         * <p>This method should be automatically invoked at the end of message
084         * processing so typically applications should not have to invoke it.
085         * If invoked multiple times it should have no side effects.
086         * @return a {@link Mono} that indicates completion or error
087         */
088        Mono<Void> setComplete();
089
090}