Search in sources :

Example 41 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class AbstractWebSocketSession method textMessage.

// WebSocketMessage factory methods
@Override
public WebSocketMessage textMessage(String payload) {
    byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = bufferFactory().wrap(bytes);
    return new WebSocketMessage(WebSocketMessage.Type.TEXT, buffer);
}
Also used : WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 42 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class JettyWebSocketHandlerAdapter method toMessage.

private <T> WebSocketMessage toMessage(Type type, T message) {
    WebSocketSession session = this.delegateSession;
    Assert.state(session != null, "Cannot create message without a session");
    if (Type.TEXT.equals(type)) {
        byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = session.bufferFactory().wrap(bytes);
        return new WebSocketMessage(Type.TEXT, buffer);
    } else if (Type.BINARY.equals(type)) {
        DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
        return new WebSocketMessage(Type.BINARY, buffer);
    } else if (Type.PONG.equals(type)) {
        DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
        return new WebSocketMessage(Type.PONG, buffer);
    } else {
        throw new IllegalArgumentException("Unexpected message type: " + message);
    }
}
Also used : OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) ByteBuffer(java.nio.ByteBuffer) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 43 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer 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 44 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReader method decodeData.

@Nullable
private Object decodeData(StringBuilder data, ResolvableType dataType, Map<String, Object> hints) {
    if (String.class == dataType.resolve()) {
        return data.substring(0, data.length() - 1);
    }
    if (this.decoder == null) {
        throw new CodecException("No SSE decoder configured and the data is not String.");
    }
    byte[] bytes = data.toString().getBytes(StandardCharsets.UTF_8);
    // wrapping only, no allocation
    DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
    return this.decoder.decode(buffer, dataType, MediaType.TEXT_EVENT_STREAM, hints);
}
Also used : CodecException(org.springframework.core.codec.CodecException) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Nullable(org.springframework.lang.Nullable)

Example 45 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class AbstractJackson2Decoder method decode.

@Override
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    ObjectMapper mapper = selectObjectMapper(elementType, mimeType);
    if (mapper == null) {
        throw new IllegalStateException("No ObjectMapper for " + elementType);
    }
    boolean forceUseOfBigDecimal = mapper.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    if (BigDecimal.class.equals(elementType.getType())) {
        forceUseOfBigDecimal = true;
    }
    Flux<DataBuffer> processed = processInput(input, elementType, mimeType, hints);
    Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(processed, mapper.getFactory(), mapper, true, forceUseOfBigDecimal, getMaxInMemorySize());
    ObjectReader reader = getObjectReader(mapper, elementType, hints);
    return tokens.handle((tokenBuffer, sink) -> {
        try {
            Object value = reader.readValue(tokenBuffer.asParser(mapper));
            logValue(value, hints);
            if (value != null) {
                sink.next(value);
            }
        } catch (IOException ex) {
            sink.error(processException(ex));
        }
    });
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)30 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23