All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.reactive.function.server.ServerRequest Maven / Gradle / Ivy

/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.reactive.function.server;

import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.security.Principal;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.Jackson2CodecSupport;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import org.springframework.web.util.UriBuilder;

/**
 * Represents a server-side HTTP request, as handled by a {@code HandlerFunction}.
 * Access to headers and body is offered by {@link Headers} and
 * {@link #body(BodyExtractor)}, respectively.
 *
 * @author Arjen Poutsma
 * @author Sebastien Deleuze
 * @since 5.0
 */
public interface ServerRequest {

	/**
	 * Return the HTTP method.
	 * @return the HTTP method as an HttpMethod enum value, or {@code null}
	 * if not resolvable (e.g. in case of a non-standard HTTP method)
	 */
	@Nullable
	default HttpMethod method() {
		return HttpMethod.resolve(methodName());
	}

	/**
	 * Return the name of the HTTP method.
	 * @return the HTTP method as a String
	 */
	String methodName();

	/**
	 * Return the request URI.
	 */
	URI uri();

	/**
	 * Return a {@code UriBuilderComponents}  from the URI associated with this
	 * {@code ServerRequest}, while also overlaying with values from the headers
	 * "Forwarded" (RFC 7239),
	 * or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
	 * "Forwarded" is not found.
	 * @return a URI builder
	 */
	UriBuilder uriBuilder();

	/**
	 * Return the request path.
	 */
	default String path() {
		return uri().getRawPath();
	}

	/**
	 * Return the request path as {@code PathContainer}.
	 */
	default PathContainer pathContainer() {
		return PathContainer.parsePath(path());
	}

	/**
	 * Return the headers of this request.
	 */
	Headers headers();

	/**
	 * Return the cookies of this request.
	 */
	MultiValueMap cookies();

	/**
	 * Extract the body with the given {@code BodyExtractor}.
	 * @param extractor the {@code BodyExtractor} that reads from the request
	 * @param  the type of the body returned
	 * @return the extracted body
	 * @see #body(BodyExtractor, Map)
	 */
	 T body(BodyExtractor extractor);

	/**
	 * Extract the body with the given {@code BodyExtractor} and hints.
	 * @param extractor the {@code BodyExtractor} that reads from the request
	 * @param hints the map of hints like {@link Jackson2CodecSupport#JSON_VIEW_HINT}
	 * to use to customize body extraction
	 * @param  the type of the body returned
	 * @return the extracted body
	 */
	 T body(BodyExtractor extractor, Map hints);

	/**
	 * Extract the body to a {@code Mono}.
	 * @param elementClass the class of element in the {@code Mono}
	 * @param  the element type
	 * @return the body as a mono
	 */
	 Mono bodyToMono(Class elementClass);

	/**
	 * Extract the body to a {@code Mono}.
	 * @param typeReference a type reference describing the expected response request type
	 * @param  the element type
	 * @return a mono containing the body of the given type {@code T}
	 */
	 Mono bodyToMono(ParameterizedTypeReference typeReference);

	/**
	 * Extract the body to a {@code Flux}.
	 * @param elementClass the class of element in the {@code Flux}
	 * @param  the element type
	 * @return the body as a flux
	 */
	 Flux bodyToFlux(Class elementClass);

	/**
	 * Extract the body to a {@code Flux}.
	 * @param typeReference a type reference describing the expected request body type
	 * @param  the element type
	 * @return a flux containing the body of the given type {@code T}
	 */
	 Flux bodyToFlux(ParameterizedTypeReference typeReference);

	/**
	 * Return the request attribute value if present.
	 * @param name the attribute name
	 * @return the attribute value
	 */
	default Optional attribute(String name) {
		Map attributes = attributes();
		if (attributes.containsKey(name)) {
			return Optional.of(attributes.get(name));
		}
		else {
			return Optional.empty();
		}
	}

	/**
	 * Return a mutable map of request attributes.
	 * @return the request attributes
	 */
	Map attributes();

	/**
	 * Return the first query parameter with the given name, if present.
	 * @param name the parameter name
	 * @return the parameter value
	 */
	default Optional queryParam(String name) {
		List queryParamValues = queryParams().get(name);
		if (CollectionUtils.isEmpty(queryParamValues)) {
			return Optional.empty();
		}
		else {
			String value = queryParamValues.get(0);
			if (value == null) {
				value = "";
			}
			return Optional.of(value);
		}
	}

	/**
	 * Return all query parameters for this request.
	 */
	MultiValueMap queryParams();

	/**
	 * Return the path variable with the given name, if present.
	 * @param name the variable name
	 * @return the variable value
	 * @throws IllegalArgumentException if there is no path variable with the given name
	 */
	default String pathVariable(String name) {
		Map pathVariables = pathVariables();
		if (pathVariables.containsKey(name)) {
			return pathVariables().get(name);
		}
		else {
			throw new IllegalArgumentException("No path variable with name \"" + name + "\" available");
		}
	}

	/**
	 * Return all path variables for this request.
	 */
	Map pathVariables();

	/**
	 * Return the web session for this 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.
	 */
	Mono session();

	/**
	 * Return the authenticated user for the request, if any.
	 */
	Mono principal();

	/**
	 * Return the form data from the body of the request if the Content-Type is
	 * {@code "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 {@code MultiValueMap} is * cached so that this method is safe to call more than once. */ Mono> formData(); /** * Return the parts of a multipart request if the Content-Type is * {@code "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 {@code MultiValueMap} is * cached so that this method is safe to call more than once. */ Mono> multipartData(); // Static methods /** * Create a new {@code ServerRequest} based on the given {@code ServerWebExchange} and * message readers. * @param exchange the exchange * @param messageReaders the message readers * @return the created {@code ServerRequest} */ static ServerRequest create(ServerWebExchange exchange, List> messageReaders) { return new DefaultServerRequest(exchange, messageReaders); } /** * Represents the headers of the HTTP request. * @see ServerRequest#headers() */ interface Headers { /** * Return the list of acceptable {@linkplain MediaType media types}, * as specified by the {@code Accept} header. *

Returns an empty list when the acceptable media types are unspecified. */ List accept(); /** * Return the list of acceptable {@linkplain Charset charsets}, * as specified by the {@code Accept-Charset} header. */ List acceptCharset(); /** * Return the list of acceptable {@linkplain Locale.LanguageRange languages}, * as specified by the {@code Accept-Language} header. */ List acceptLanguage(); /** * Return the length of the body in bytes, as specified by the * {@code Content-Length} header. */ OptionalLong contentLength(); /** * Return the {@linkplain MediaType media type} of the body, as specified * by the {@code Content-Type} header. */ Optional contentType(); /** * Return the value of the required {@code Host} header. *

If the header value does not contain a port, the returned * {@linkplain InetSocketAddress#getPort() port} will be {@code 0}. */ @Nullable InetSocketAddress host(); /** * Return the value of the {@code Range} header. *

Returns an empty list when the range is unknown. */ List range(); /** * Return the header value(s), if any, for the header of the given name. *

Return an empty list if no header values are found. * @param headerName the header name */ List header(String headerName); /** * Return the headers as a {@link HttpHeaders} instance. */ HttpHeaders asHttpHeaders(); } }