001/*
002 * Copyright 2002-2020 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.server.reactive;
018
019import org.springframework.http.HttpStatus;
020import org.springframework.http.ReactiveHttpOutputMessage;
021import org.springframework.http.ResponseCookie;
022import org.springframework.lang.Nullable;
023import org.springframework.util.MultiValueMap;
024
025/**
026 * Represents a reactive server-side HTTP response.
027 *
028 * @author Arjen Poutsma
029 * @author Sebastien Deleuze
030 * @author Rossen Stoyanchev
031 * @since 5.0
032 */
033public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
034
035        /**
036         * Set the HTTP status code of the response.
037         * @param status the HTTP status as an {@link HttpStatus} enum value
038         * @return {@code false} if the status code change wasn't processed because
039         * the HTTP response is committed, {@code true} if successfully set.
040         */
041        boolean setStatusCode(@Nullable HttpStatus status);
042
043        /**
044         * Return the status code that has been set, or otherwise fall back on the
045         * status of the response from the underlying server. The return value may
046         * be {@code null} if the status code value is outside the
047         * {@link HttpStatus} enum range, or if there is no default value from the
048         * underlying server.
049         */
050        @Nullable
051        HttpStatus getStatusCode();
052
053        /**
054         * Set the HTTP status code to the given value (potentially non-standard and
055         * not resolvable through the {@link HttpStatus} enum) as an integer.
056         * @param value the status code value
057         * @return {@code false} if the status code change wasn't processed because
058         * the HTTP response is committed, {@code true} if successfully set.
059         * @since 5.2.4
060         */
061        default boolean setRawStatusCode(@Nullable Integer value) {
062                if (value == null) {
063                        return setStatusCode(null);
064                }
065                else {
066                        HttpStatus httpStatus = HttpStatus.resolve(value);
067                        if (httpStatus == null) {
068                                throw new IllegalStateException(
069                                                "Unresolvable HttpStatus for general ServerHttpResponse: " + value);
070                        }
071                        return setStatusCode(httpStatus);
072                }
073        }
074
075        /**
076         * Return the status code that has been set, or otherwise fall back on the
077         * status of the response from the underlying server. The return value may
078         * be {@code null} if there is no default value from the underlying server.
079         * @since 5.2.4
080         */
081        @Nullable
082        default Integer getRawStatusCode() {
083                HttpStatus httpStatus = getStatusCode();
084                return (httpStatus != null ? httpStatus.value() : null);
085        }
086
087        /**
088         * Return a mutable map with the cookies to send to the server.
089         */
090        MultiValueMap<String, ResponseCookie> getCookies();
091
092        /**
093         * Add the given {@code ResponseCookie}.
094         * @param cookie the cookie to add
095         * @throws IllegalStateException if the response has already been committed
096         */
097        void addCookie(ResponseCookie cookie);
098
099}