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

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

Introduction

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

Prototype

static ServerRequest create(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) 

Source Link

Document

Create a new ServerRequest based on the given ServerWebExchange and message readers.

Usage

From source file:org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler.java

@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable throwable) {
    if (exchange.getResponse().isCommitted() || isDisconnectedClientError(throwable)) {
        return Mono.error(throwable);
    }/*from  w  ww.  j ava  2s .c o  m*/
    this.errorAttributes.storeErrorInformation(throwable, exchange);
    ServerRequest request = ServerRequest.create(exchange, this.messageReaders);
    return getRoutingFunction(this.errorAttributes).route(request).switchIfEmpty(Mono.error(throwable))
            .flatMap((handler) -> handler.handle(request))
            .doOnNext((response) -> logError(request, response, throwable))
            .flatMap((response) -> write(exchange, response));
}

From source file:org.springframework.cloud.gateway.handler.predicate.ReadBodyPredicateFactory.java

@Override
@SuppressWarnings("unchecked")
public AsyncPredicate<ServerWebExchange> applyAsync(Config config) {
    return exchange -> {
        Class inClass = config.getInClass();

        Object cachedBody = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY);
        Mono<?> modifiedBody;/*w w  w. j ava2  s  .  co m*/
        // We can only read the body from the request once, once that happens if we
        // try to read the body again an exception will be thrown. The below if/else
        // caches the body object as a request attribute in the ServerWebExchange
        // so if this filter is run more than once (due to more than one route
        // using it) we do not try to read the request body multiple times
        if (cachedBody != null) {
            try {
                boolean test = config.predicate.test(cachedBody);
                exchange.getAttributes().put(TEST_ATTRIBUTE, test);
                return Mono.just(test);
            } catch (ClassCastException e) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Predicate test failed because class in predicate "
                            + "does not match the cached body object", e);
                }
            }
            return Mono.just(false);
        } else {
            // Join all the DataBuffers so we have a single DataBuffer for the body
            return DataBufferUtils.join(exchange.getRequest().getBody()).flatMap(dataBuffer -> {
                // Update the retain counts so we can read the body twice,
                // once to parse into an object
                // that we can test the predicate against and a second time
                // when the HTTP client sends
                // the request downstream
                // Note: if we end up reading the body twice we will run into
                // a problem, but as of right
                // now there is no good use case for doing this
                DataBufferUtils.retain(dataBuffer);
                // Make a slice for each read so each read has its own
                // read/write indexes
                Flux<DataBuffer> cachedFlux = Flux
                        .defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));

                ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
                    @Override
                    public Flux<DataBuffer> getBody() {
                        return cachedFlux;
                    }
                };
                return ServerRequest.create(exchange.mutate().request(mutatedRequest).build(), messageReaders)
                        .bodyToMono(inClass).doOnNext(objectValue -> {
                            exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, objectValue);
                            exchange.getAttributes().put(CACHED_REQUEST_BODY_KEY, cachedFlux);
                        }).map(objectValue -> config.predicate.test(objectValue));
            });

        }
    };
}