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.web.reactive.function.client;
018
019import java.util.List;
020import java.util.function.Consumer;
021
022import org.springframework.http.codec.ClientCodecConfigurer;
023import org.springframework.http.codec.HttpMessageReader;
024import org.springframework.http.codec.HttpMessageWriter;
025
026/**
027 * Provides strategies for use in an {@link ExchangeFunction}.
028 *
029 * <p>To create an instance, see the static methods {@link #withDefaults()},
030 * {@link #builder()}, and {@link #empty()}.
031 *
032 * @author Brian Clozel
033 * @author Arjen Poutsma
034 * @since 5.0
035 */
036public interface ExchangeStrategies {
037
038        /**
039         * Return {@link HttpMessageReader HttpMessageReaders} to read and decode the response body with.
040         * @return the message readers
041         */
042        List<HttpMessageReader<?>> messageReaders();
043
044        /**
045         * Return {@link HttpMessageWriter HttpMessageWriters} to write and encode the request body with.
046         * @return the message writers
047         */
048        List<HttpMessageWriter<?>> messageWriters();
049
050        /**
051         * Return a builder to create a new {@link ExchangeStrategies} instance
052         * replicated from the current instance.
053         * @since 5.1.12
054         */
055        default Builder mutate() {
056                throw new UnsupportedOperationException();
057        }
058
059
060        // Static builder methods
061
062        /**
063         * Return an {@code ExchangeStrategies} instance with default configuration
064         * provided by {@link ClientCodecConfigurer}.
065         */
066        static ExchangeStrategies withDefaults() {
067                return DefaultExchangeStrategiesBuilder.DEFAULT_EXCHANGE_STRATEGIES;
068        }
069
070        /**
071         * Return a builder pre-configured with default configuration to start.
072         * This is the same as {@link #withDefaults()} but returns a mutable builder
073         * for further customizations.
074         */
075        static Builder builder() {
076                DefaultExchangeStrategiesBuilder builder = new DefaultExchangeStrategiesBuilder();
077                builder.defaultConfiguration();
078                return builder;
079        }
080
081        /**
082         * Return a builder with empty configuration to start.
083         */
084        static Builder empty() {
085                return new DefaultExchangeStrategiesBuilder();
086        }
087
088
089        /**
090         * A mutable builder for an {@link ExchangeStrategies}.
091         */
092        interface Builder {
093
094                /**
095                 * Customize the list of client-side HTTP message readers and writers.
096                 * @param consumer the consumer to customize the codecs
097                 * @return this builder
098                 */
099                Builder codecs(Consumer<ClientCodecConfigurer> consumer);
100
101                /**
102                 * Builds the {@link ExchangeStrategies}.
103                 * @return the built strategies
104                 */
105                ExchangeStrategies build();
106        }
107
108}