Example usage for org.springframework.web.reactive.function.server ServerRequest headers

List of usage examples for org.springframework.web.reactive.function.server ServerRequest headers

Introduction

In this page you can find the example usage for org.springframework.web.reactive.function.server ServerRequest headers.

Prototype

Headers headers();

Source Link

Document

Get the headers of this request.

Usage

From source file:org.springframework.web.reactive.function.server.RequestPredicates.java

/**
 * Return a {@code RequestPredicate} that tests the request's headers against the given headers predicate.
 * @param headersPredicate a predicate that tests against the request headers
 * @return a predicate that tests against the given header predicate
 *//*from  w ww. jav  a2 s . com*/
public static RequestPredicate headers(Predicate<ServerRequest.Headers> headersPredicate) {
    return new HeadersPredicate(headersPredicate);
}

From source file:org.springframework.web.reactive.function.server.RequestPredicates.java

/**
 * Return a {@code RequestPredicate} that tests if the request's
 * {@linkplain ServerRequest.Headers#contentType() content type} is
 * {@linkplain MediaType#includes(MediaType) included} by any of the given media types.
 * @param mediaTypes the media types to match the request's content type against
 * @return a predicate that tests the request's content type against the given media types
 */// w w w  .j  a va  2 s  . c  o  m
public static RequestPredicate contentType(MediaType... mediaTypes) {
    Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
    Set<MediaType> mediaTypeSet = new HashSet<>(Arrays.asList(mediaTypes));

    return headers(new Predicate<ServerRequest.Headers>() {
        @Override
        public boolean test(ServerRequest.Headers headers) {
            MediaType contentType = headers.contentType().orElse(MediaType.APPLICATION_OCTET_STREAM);
            boolean match = mediaTypeSet.stream().anyMatch(mediaType -> mediaType.includes(contentType));
            traceMatch("Content-Type", mediaTypeSet, contentType, match);
            return match;
        }

        @Override
        public String toString() {
            return String.format("Content-Type: %s", mediaTypeSet);
        }
    });
}

From source file:org.springframework.web.reactive.function.server.RequestPredicates.java

/**
 * Return a {@code RequestPredicate} that tests if the request's
 * {@linkplain ServerRequest.Headers#accept() accept} header is
 * {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with any of the given media types.
 * @param mediaTypes the media types to match the request's accept header against
 * @return a predicate that tests the request's accept header against the given media types
 *//*from   www  .java2 s  .  c o  m*/
public static RequestPredicate accept(MediaType... mediaTypes) {
    Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
    Set<MediaType> mediaTypeSet = new HashSet<>(Arrays.asList(mediaTypes));

    return headers(new Predicate<ServerRequest.Headers>() {
        @Override
        public boolean test(ServerRequest.Headers headers) {
            List<MediaType> acceptedMediaTypes = headers.accept();
            if (acceptedMediaTypes.isEmpty()) {
                acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
            } else {
                MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
            }
            boolean match = acceptedMediaTypes.stream().anyMatch(
                    acceptedMediaType -> mediaTypeSet.stream().anyMatch(acceptedMediaType::isCompatibleWith));
            traceMatch("Accept", mediaTypeSet, acceptedMediaTypes, match);
            return match;
        }

        @Override
        public String toString() {
            return String.format("Accept: %s", mediaTypeSet);
        }
    });
}