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.server;
018
019import java.util.List;
020import java.util.function.Consumer;
021
022import org.springframework.http.codec.HttpMessageReader;
023import org.springframework.http.codec.HttpMessageWriter;
024import org.springframework.http.codec.ServerCodecConfigurer;
025import org.springframework.web.reactive.result.view.ViewResolver;
026import org.springframework.web.server.WebExceptionHandler;
027import org.springframework.web.server.WebFilter;
028import org.springframework.web.server.i18n.LocaleContextResolver;
029
030/**
031 * Defines the strategies to be used for processing {@link HandlerFunction HandlerFunctions}.
032 *
033 * <p>An instance of this class is immutable. Instances are typically created through the
034 * mutable {@link Builder}: either through {@link #builder()} to set up default strategies,
035 * or {@link #empty()} to start from scratch.
036 *
037 * @author Arjen Poutsma
038 * @author Juergen Hoeller
039 * @author Sebastien Deleuze
040 * @since 5.0
041 * @see RouterFunctions#toHttpHandler(RouterFunction, HandlerStrategies)
042 */
043public interface HandlerStrategies {
044
045        /**
046         * Return the {@link HttpMessageReader HttpMessageReaders} to be used for request body conversion.
047         * @return the message readers
048         */
049        List<HttpMessageReader<?>> messageReaders();
050
051        /**
052         * Return the {@link HttpMessageWriter HttpMessageWriters} to be used for response body conversion.
053         * @return the message writers
054         */
055        List<HttpMessageWriter<?>> messageWriters();
056
057        /**
058         * Return the {@link ViewResolver ViewResolvers} to be used for view name resolution.
059         * @return the view resolvers
060         */
061        List<ViewResolver> viewResolvers();
062
063        /**
064         * Return the {@link WebFilter WebFilters} to be used for filtering the request and response.
065         * @return the web filters
066         */
067        List<WebFilter> webFilters();
068
069        /**
070         * Return the {@link WebExceptionHandler WebExceptionHandlers} to be used for handling exceptions.
071         * @return the exception handlers
072         */
073        List<WebExceptionHandler> exceptionHandlers();
074
075        /**
076         * Return the {@link LocaleContextResolver} to be used for resolving locale context.
077         * @return the locale context resolver
078         */
079        LocaleContextResolver localeContextResolver();
080
081
082        // Static builder methods
083
084        /**
085         * Return a new {@code HandlerStrategies} with default initialization.
086         * @return the new {@code HandlerStrategies}
087         */
088        static HandlerStrategies withDefaults() {
089                return builder().build();
090        }
091
092        /**
093         * Return a mutable builder for a {@code HandlerStrategies} with default initialization.
094         * @return the builder
095         */
096        static Builder builder() {
097                DefaultHandlerStrategiesBuilder builder = new DefaultHandlerStrategiesBuilder();
098                builder.defaultConfiguration();
099                return builder;
100        }
101
102        /**
103         * Return a mutable, empty builder for a {@code HandlerStrategies}.
104         * @return the builder
105         */
106        static Builder empty() {
107                return new DefaultHandlerStrategiesBuilder();
108        }
109
110
111        /**
112         * A mutable builder for a {@link HandlerStrategies}.
113         */
114        interface Builder {
115
116                /**
117                 * Customize the list of server-side HTTP message readers and writers.
118                 * @param consumer the consumer to customize the codecs
119                 * @return this builder
120                 */
121                Builder codecs(Consumer<ServerCodecConfigurer> consumer);
122
123                /**
124                 * Add the given view resolver to this builder.
125                 * @param viewResolver the view resolver to add
126                 * @return this builder
127                 */
128                Builder viewResolver(ViewResolver viewResolver);
129
130                /**
131                 * Add the given web filter to this builder.
132                 * @param filter the filter to add
133                 * @return this builder
134                 */
135                Builder webFilter(WebFilter filter);
136
137                /**
138                 * Add the given exception handler to this builder.
139                 * @param exceptionHandler the exception handler to add
140                 * @return this builder
141                 */
142                Builder exceptionHandler(WebExceptionHandler exceptionHandler);
143
144                /**
145                 * Add the given locale context resolver to this builder.
146                 * @param localeContextResolver the locale context resolver to add
147                 * @return this builder
148                 */
149                Builder localeContextResolver(LocaleContextResolver localeContextResolver);
150
151                /**
152                 * Builds the {@link HandlerStrategies}.
153                 * @return the built strategies
154                 */
155                HandlerStrategies build();
156        }
157
158}