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.result.view;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import reactor.core.publisher.Mono;
024
025import org.springframework.http.MediaType;
026import org.springframework.lang.Nullable;
027import org.springframework.web.reactive.HandlerResult;
028import org.springframework.web.server.ServerWebExchange;
029
030/**
031 * Contract to render {@link HandlerResult} to the HTTP response.
032 *
033 * <p>In contrast to an {@link org.springframework.core.codec.Encoder Encoder}
034 * which is a singleton and encodes any object of a given type, a {@code View}
035 * is typically selected by name and resolved using a {@link ViewResolver}
036 * which may for example match it to an HTML template. Furthermore a {@code View}
037 * may render based on multiple attributes contained in the model.
038 *
039 * <p>A {@code View} can also choose to select an attribute from the model use
040 * any existing {@code Encoder} to render alternate media types.
041 *
042 * @author Rossen Stoyanchev
043 * @since 5.0
044 */
045public interface View {
046
047        /**
048         * The name of the exchange attribute that contains the
049         * {@link org.springframework.web.reactive.BindingContext BindingContext}
050         * for the request which can be used to create
051         * {@link org.springframework.validation.BindingResult BindingResult}
052         * instances for objects in to the model.
053         * <p>Note: This attribute is not required and may not be present.
054         * @since 5.1.8
055         */
056        String BINDING_CONTEXT_ATTRIBUTE = View.class.getName() + ".bindingContext";
057
058
059        /**
060         * Return the list of media types this View supports, or an empty list.
061         */
062        default List<MediaType> getSupportedMediaTypes() {
063                return Collections.emptyList();
064        }
065
066        /**
067         * Whether this View does rendering by performing a redirect.
068         */
069        default boolean isRedirectView() {
070                return false;
071        }
072
073        /**
074         * Render the view based on the given {@link HandlerResult}. Implementations
075         * can access and use the model or only a specific attribute in it.
076         * @param model a Map with name Strings as keys and corresponding model
077         * objects as values (Map can also be {@code null} in case of empty model)
078         * @param contentType the content type selected to render with which should
079         * match one of the {@link #getSupportedMediaTypes() supported media types}.
080         * @param exchange the current exchange
081         * @return {@code Mono} to represent when and if rendering succeeds
082         */
083        Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType contentType, ServerWebExchange exchange);
084
085}