Search in sources :

Example 91 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Assert.state(this.methodResolver != null && this.modelInitializer != null, "Not initialized");
    InitBinderBindingContext bindingContext = new InitBinderBindingContext(getWebBindingInitializer(), this.methodResolver.getInitBinderMethods(handlerMethod));
    InvocableHandlerMethod invocableMethod = this.methodResolver.getRequestMappingMethod(handlerMethod);
    Function<Throwable, Mono<HandlerResult>> exceptionHandler = ex -> handleException(ex, handlerMethod, bindingContext, exchange);
    return this.modelInitializer.initModel(handlerMethod, bindingContext, exchange).then(Mono.defer(() -> invocableMethod.invoke(exchange, bindingContext))).doOnNext(result -> result.setExceptionHandler(exceptionHandler)).doOnNext(result -> bindingContext.saveModel()).onErrorResume(exceptionHandler);
}
Also used : WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) ApplicationContext(org.springframework.context.ApplicationContext) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) HandlerResult(org.springframework.web.reactive.HandlerResult) InitializingBean(org.springframework.beans.factory.InitializingBean) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethod(org.springframework.web.method.HandlerMethod) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Log(org.apache.commons.logging.Log) Nullable(org.springframework.lang.Nullable) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) ApplicationContextAware(org.springframework.context.ApplicationContextAware) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HandlerMapping(org.springframework.web.reactive.HandlerMapping) Assert(org.springframework.util.Assert) HandlerAdapter(org.springframework.web.reactive.HandlerAdapter) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) Mono(reactor.core.publisher.Mono) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 92 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolver method decode.

@SuppressWarnings("unchecked")
private <T> Mono<T> decode(Part part, MethodParameter elementType, BindingContext bindingContext, ServerWebExchange exchange, boolean isRequired) {
    ServerHttpRequest partRequest = new PartServerHttpRequest(exchange.getRequest(), part);
    ServerWebExchange partExchange = exchange.mutate().request(partRequest).build();
    if (logger.isDebugEnabled()) {
        logger.debug(exchange.getLogPrefix() + "Decoding part '" + part.name() + "'");
    }
    return (Mono<T>) readBody(elementType, isRequired, bindingContext, partExchange);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mono(reactor.core.publisher.Mono)

Example 93 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) {
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = (requestPart == null || requestPart.required());
    Class<?> paramType = parameter.getParameterType();
    Flux<Part> partValues = getPartValues(parameter, requestPart, isRequired, exchange);
    if (Part.class.isAssignableFrom(paramType)) {
        return partValues.next().cast(Object.class);
    }
    if (Collection.class.isAssignableFrom(paramType) || List.class.isAssignableFrom(paramType)) {
        MethodParameter elementType = parameter.nested();
        if (Part.class.isAssignableFrom(elementType.getNestedParameterType())) {
            return partValues.collectList().cast(Object.class);
        } else {
            return partValues.next().flatMap(part -> decode(part, parameter, bindingContext, exchange, isRequired)).defaultIfEmpty(Collections.emptyList());
        }
    }
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(paramType);
    if (adapter == null) {
        return partValues.next().flatMap(part -> decode(part, parameter, bindingContext, exchange, isRequired));
    }
    MethodParameter elementType = parameter.nested();
    if (Part.class.isAssignableFrom(elementType.getNestedParameterType())) {
        return Mono.just(adapter.fromPublisher(partValues));
    }
    Flux<?> flux = partValues.flatMap(part -> decode(part, elementType, bindingContext, exchange, isRequired));
    return Mono.just(adapter.fromPublisher(flux));
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) HttpHeaders(org.springframework.http.HttpHeaders) Collection(java.util.Collection) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Mono(reactor.core.publisher.Mono) RequestPart(org.springframework.web.bind.annotation.RequestPart) BindingContext(org.springframework.web.reactive.BindingContext) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Flux(reactor.core.publisher.Flux) List(java.util.List) Part(org.springframework.http.codec.multipart.Part) CollectionUtils(org.springframework.util.CollectionUtils) MethodParameter(org.springframework.core.MethodParameter) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) Nullable(org.springframework.lang.Nullable) Collections(java.util.Collections) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) StringUtils(org.springframework.util.StringUtils) RequestPart(org.springframework.web.bind.annotation.RequestPart) RequestPart(org.springframework.web.bind.annotation.RequestPart) Part(org.springframework.http.codec.multipart.Part) Collection(java.util.Collection) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 94 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class MultipartHttpMessageWriter method writeMultipart.

private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
    byte[] boundary = generateMultipartBoundary();
    mediaType = getMultipartMediaType(mediaType, boundary);
    outputMessage.getHeaders().setContentType(mediaType);
    LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)"));
    DataBufferFactory bufferFactory = outputMessage.bufferFactory();
    Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()).concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory)).concatWith(generateLastLine(boundary, bufferFactory)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
    if (logger.isDebugEnabled()) {
        body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return outputMessage.writeWith(body);
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Arrays(java.util.Arrays) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Hints(org.springframework.core.codec.Hints) ResolvableTypeProvider(org.springframework.core.ResolvableTypeProvider) Supplier(java.util.function.Supplier) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) LogFormatUtils(org.springframework.core.log.LogFormatUtils) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Map(java.util.Map) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) Resource(org.springframework.core.io.Resource) HttpHeaders(org.springframework.http.HttpHeaders) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) PooledDataBuffer(org.springframework.core.io.buffer.PooledDataBuffer) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Optional(java.util.Optional) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) PooledDataBuffer(org.springframework.core.io.buffer.PooledDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 95 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ResourceHttpMessageWriter method writeResource.

private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    HttpHeaders headers = message.getHeaders();
    MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);
    headers.setContentType(resourceMediaType);
    if (headers.getContentLength() < 0) {
        long length = lengthOf(resource);
        if (length != -1) {
            headers.setContentLength(length);
        }
    }
    return zeroCopy(resource, null, message, hints).orElseGet(() -> {
        Mono<Resource> input = Mono.just(resource);
        DataBufferFactory factory = message.bufferFactory();
        Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints);
        if (logger.isDebugEnabled()) {
            body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
        }
        return message.writeWith(body);
    });
}
Also used : HttpLogging(org.springframework.http.HttpLogging) ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Hints(org.springframework.core.codec.Hints) ResourceRegion(org.springframework.core.io.support.ResourceRegion) ResourceRegionEncoder(org.springframework.core.codec.ResourceRegionEncoder) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) MediaTypeFactory(org.springframework.http.MediaTypeFactory) Map(java.util.Map) InputStreamResource(org.springframework.core.io.InputStreamResource) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ResourceDecoder(org.springframework.core.codec.ResourceDecoder) Resource(org.springframework.core.io.Resource) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ResourceEncoder(org.springframework.core.codec.ResourceEncoder) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) File(java.io.File) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) HttpHeaders(org.springframework.http.HttpHeaders) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

Mono (reactor.core.publisher.Mono)308 Test (org.junit.jupiter.api.Test)143 StepVerifier (reactor.test.StepVerifier)117 List (java.util.List)116 Flux (reactor.core.publisher.Flux)110 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)102 Collections (java.util.Collections)85 Map (java.util.Map)75 HttpStatus (org.springframework.http.HttpStatus)66 ServerWebExchange (org.springframework.web.server.ServerWebExchange)65 Duration (java.time.Duration)60 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)53 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assert (org.springframework.util.Assert)49 Mockito.mock (org.mockito.Mockito.mock)48 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)46 Optional (java.util.Optional)44 StandardCharsets (java.nio.charset.StandardCharsets)43