Search in sources :

Example 1 with ReactiveHttpInputMessage

use of org.springframework.http.ReactiveHttpInputMessage in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReader method read.

@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
    boolean hasSseWrapper = ServerSentEvent.class.isAssignableFrom(elementType.getRawClass());
    ResolvableType dataType = (hasSseWrapper ? elementType.getGeneric(0) : elementType);
    return Flux.from(inputMessage.getBody()).concatMap(ServerSentEventHttpMessageReader::splitOnNewline).map(buffer -> {
        CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
        DataBufferUtils.release(buffer);
        return charBuffer.toString();
    }).bufferUntil(line -> line.equals("\n")).concatMap(rawLines -> {
        String[] lines = rawLines.stream().collect(joining()).split("\\r?\\n");
        ServerSentEvent<Object> event = buildEvent(lines, dataType, hints);
        return (hasSseWrapper ? Mono.just(event) : Mono.justOrEmpty(event.data()));
    }).cast(Object.class);
}
Also used : Decoder(org.springframework.core.codec.Decoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharBuffer(java.nio.CharBuffer) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) IntPredicate(java.util.function.IntPredicate) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Duration(java.time.Duration) Map(java.util.Map) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Assert(org.springframework.util.Assert) CharBuffer(java.nio.CharBuffer) ResolvableType(org.springframework.core.ResolvableType)

Example 2 with ReactiveHttpInputMessage

use of org.springframework.http.ReactiveHttpInputMessage in project spring-framework by spring-projects.

the class BodyExtractorsTests method toDataBuffers.

@Test
public void toDataBuffers() throws Exception {
    BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Flux<DataBuffer> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 3 with ReactiveHttpInputMessage

use of org.springframework.http.ReactiveHttpInputMessage in project spring-framework by spring-projects.

the class BodyExtractors method readWithMessageReaders.

private static <T, S extends Publisher<T>> S readWithMessageReaders(ReactiveHttpInputMessage inputMessage, BodyExtractor.Context context, ResolvableType elementType, Function<HttpMessageReader<T>, S> readerFunction, Function<Throwable, S> unsupportedError) {
    MediaType contentType = contentType(inputMessage);
    Supplier<Stream<HttpMessageReader<?>>> messageReaders = context.messageReaders();
    return messageReaders.get().filter(r -> r.canRead(elementType, contentType)).findFirst().map(BodyExtractors::<T>cast).map(readerFunction).orElseGet(() -> {
        List<MediaType> supportedMediaTypes = messageReaders.get().flatMap(reader -> reader.getReadableMediaTypes().stream()).collect(Collectors.toList());
        UnsupportedMediaTypeException error = new UnsupportedMediaTypeException(contentType, supportedMediaTypes);
        return unsupportedError.apply(error);
    });
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) ResolvableType(org.springframework.core.ResolvableType) HttpMessage(org.springframework.http.HttpMessage) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Stream(java.util.stream.Stream)

Example 4 with ReactiveHttpInputMessage

use of org.springframework.http.ReactiveHttpInputMessage in project spring-framework by spring-projects.

the class BodyExtractorsTests method toMono.

@Test
public void toMono() throws Exception {
    BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Mono<String> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext("foo").expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 5 with ReactiveHttpInputMessage

use of org.springframework.http.ReactiveHttpInputMessage in project spring-framework by spring-projects.

the class BodyExtractorsTests method toMonoWithHints.

@Test
public void toMonoWithHints() throws Exception {
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Mono<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertEquals("foo", user.getUsername());
        assertNull(user.getPassword());
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)8 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)8 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)7 Test (org.junit.Test)6 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)6 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)5 List (java.util.List)3 Stream (java.util.stream.Stream)3 MediaType (org.springframework.http.MediaType)3 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Supplier (java.util.function.Supplier)2 ResolvableType (org.springframework.core.ResolvableType)2 StringDecoder (org.springframework.core.codec.StringDecoder)2 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)2 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)2