org.springframework.web.server

Interface ServerWebExchange

  • All Known Implementing Classes:
    DefaultServerWebExchange, MockServerWebExchange, ServerWebExchangeDecorator


    public interface ServerWebExchange
    Contract for an HTTP request-response interaction. Provides access to the HTTP request and response and also exposes additional server-side processing related properties and features such as request attributes.
    Since:
    5.0
    Author:
    Rossen Stoyanchev
    • Field Detail

      • LOG_ID_ATTRIBUTE

        static final java.lang.String LOG_ID_ATTRIBUTE
        Name of attribute whose value can be used to correlate log messages for this exchange. Use getLogPrefix() to obtain a consistently formatted prefix based on this attribute.
        Since:
        5.1
        See Also:
        getLogPrefix()
    • Method Detail

      • getAttributes

        java.util.Map<java.lang.String,java.lang.Object> getAttributes()
        Return a mutable map of request attributes for the current exchange.
      • getAttribute

        @Nullable
        default <T> T getAttribute(java.lang.String name)
        Return the request attribute value if present.
        Type Parameters:
        T - the attribute type
        Parameters:
        name - the attribute name
        Returns:
        the attribute value
      • getRequiredAttribute

        default <T> T getRequiredAttribute(java.lang.String name)
        Return the request attribute value or if not present raise an IllegalArgumentException.
        Type Parameters:
        T - the attribute type
        Parameters:
        name - the attribute name
        Returns:
        the attribute value
      • getAttributeOrDefault

        default <T> T getAttributeOrDefault(java.lang.String name,
                                            T defaultValue)
        Return the request attribute value, or a default, fallback value.
        Type Parameters:
        T - the attribute type
        Parameters:
        name - the attribute name
        defaultValue - a default value to return instead
        Returns:
        the attribute value
      • getSession

        reactor.core.publisher.Mono<WebSession> getSession()
        Return the web session for the current request. Always guaranteed to return an instance either matching to the session id requested by the client, or with a new session id either because the client did not specify one or because the underlying session had expired. Use of this method does not automatically create a session. See WebSession for more details.
      • getPrincipal

        <T extends java.security.Principal> reactor.core.publisher.Mono<T> getPrincipal()
        Return the authenticated user for the request, if any.
      • getFormData

        reactor.core.publisher.Mono<MultiValueMap<java.lang.String,java.lang.String>> getFormData()
        Return the form data from the body of the request if the Content-Type is "application/x-www-form-urlencoded" or an empty map otherwise.

        Note: calling this method causes the request body to be read and parsed in full and the resulting MultiValueMap is cached so that this method is safe to call more than once.

      • getMultipartData

        reactor.core.publisher.Mono<MultiValueMap<java.lang.String,Part>> getMultipartData()
        Return the parts of a multipart request if the Content-Type is "multipart/form-data" or an empty map otherwise.

        Note: calling this method causes the request body to be read and parsed in full and the resulting MultiValueMap is cached so that this method is safe to call more than once.

      • isNotModified

        boolean isNotModified()
        Returns true if the one of the checkNotModified methods in this contract were used and they returned true.
      • checkNotModified

        boolean checkNotModified(java.time.Instant lastModified)
        An overloaded variant of checkNotModified(String, Instant) with a last-modified timestamp only.
        Parameters:
        lastModified - the last-modified time
        Returns:
        whether the request qualifies as not modified
      • checkNotModified

        boolean checkNotModified(java.lang.String etag)
        An overloaded variant of checkNotModified(String, Instant) with an ETag (entity tag) value only.
        Parameters:
        etag - the entity tag for the underlying resource.
        Returns:
        true if the request does not require further processing.
      • checkNotModified

        boolean checkNotModified(@Nullable
                                 java.lang.String etag,
                                 java.time.Instant lastModified)
        Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp as determined by the application. Also transparently prepares the response, setting HTTP status, and adding "ETag" and "Last-Modified" headers when applicable. This method works with conditional GET/HEAD requests as well as with conditional POST/PUT/DELETE requests.

        Note: The HTTP specification recommends setting both ETag and Last-Modified values, but you can also use #checkNotModified(String) or checkNotModified(Instant).

        Parameters:
        etag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
        lastModified - the last-modified timestamp that the application determined for the underlying resource
        Returns:
        true if the request does not require further processing.
      • transformUrl

        java.lang.String transformUrl(java.lang.String url)
        Transform the given url according to the registered transformation function(s). By default, this method returns the given url, though additional transformation functions can by registered with addUrlTransformer(java.util.function.Function<java.lang.String, java.lang.String>)
        Parameters:
        url - the URL to transform
        Returns:
        the transformed URL
      • addUrlTransformer

        void addUrlTransformer(java.util.function.Function<java.lang.String,java.lang.String> transformer)
        Register an additional URL transformation function for use with transformUrl(java.lang.String). The given function can be used to insert an id for authentication, a nonce for CSRF protection, etc.

        Note that the given function is applied after any previously registered functions.

        Parameters:
        transformer - a URL transformation function to add
      • getLogPrefix

        java.lang.String getLogPrefix()
        Return a log message prefix to use to correlate messages for this exchange. The prefix is based on the value of the attribute LOG_ID_ATTRIBUTE along with some extra formatting so that the prefix can be conveniently prepended with no further formatting no separators required.
        Returns:
        the log message prefix or an empty String if the LOG_ID_ATTRIBUTE is not set.
        Since:
        5.1