Search in sources :

Example 1 with DataBufferFactory

use of cn.taketoday.core.io.buffer.DataBufferFactory in project today-infrastructure by TAKETODAY.

the class ResourceRegionEncoder method writeResourceRegion.

private Flux<DataBuffer> writeResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {
    Resource resource = region.getResource();
    long position = region.getPosition();
    long count = region.getCount();
    if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
        logger.debug("{}Writing region {}-{} of [{}]", Hints.getLogPrefix(hints), position, (position + count), resource);
    }
    Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
    if (logger.isDebugEnabled()) {
        in = in.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return DataBufferUtils.takeUntilByteCount(in, count);
}
Also used : Assert(cn.taketoday.lang.Assert) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) StreamUtils(cn.taketoday.util.StreamUtils) StandardCharsets(java.nio.charset.StandardCharsets) OptionalLong(java.util.OptionalLong) Flux(reactor.core.publisher.Flux) Nullable(cn.taketoday.lang.Nullable) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) ResourceRegion(cn.taketoday.core.io.ResourceRegion) InputStreamResource(cn.taketoday.core.io.InputStreamResource) MimeType(cn.taketoday.util.MimeType) Resource(cn.taketoday.core.io.Resource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 2 with DataBufferFactory

use of cn.taketoday.core.io.buffer.DataBufferFactory in project today-infrastructure by TAKETODAY.

the class MultipartHttpMessageWriter method encodePart.

@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory) {
    MultipartHttpOutputMessage message = new MultipartHttpOutputMessage(factory);
    HttpHeaders headers = message.getHeaders();
    T body;
    ResolvableType resolvableType = null;
    if (value instanceof HttpEntity) {
        HttpEntity<T> httpEntity = (HttpEntity<T>) value;
        headers.putAll(httpEntity.getHeaders());
        body = httpEntity.getBody();
        Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
        if (httpEntity instanceof ResolvableTypeProvider) {
            resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType();
        }
    } else {
        body = value;
    }
    if (resolvableType == null) {
        resolvableType = ResolvableType.fromClass(body.getClass());
    }
    if (!headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
        if (body instanceof Resource) {
            headers.setContentDispositionFormData(name, ((Resource) body).getName());
        } else if (resolvableType.resolve() == Resource.class) {
            body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> headers.setContentDispositionFormData(name, ((Resource) o).getName()));
        } else {
            headers.setContentDispositionFormData(name, null);
        }
    }
    MediaType contentType = headers.getContentType();
    final ResolvableType finalBodyType = resolvableType;
    Optional<HttpMessageWriter<?>> writer = this.partWriters.stream().filter(partWriter -> partWriter.canWrite(finalBodyType, contentType)).findFirst();
    if (writer.isEmpty()) {
        return Flux.error(new CodecException("No suitable writer found for part: " + name));
    }
    Publisher<T> bodyPublisher = body instanceof Publisher ? (Publisher<T>) body : Mono.just(body);
    // The writer will call MultipartHttpOutputMessage#write which doesn't actually write
    // but only stores the body Flux and returns Mono.empty().
    Mono<Void> partContentReady = ((HttpMessageWriter<T>) writer.get()).write(bodyPublisher, resolvableType, contentType, message, DEFAULT_HINTS);
    // After partContentReady, we can access the part content from MultipartHttpOutputMessage
    // and use it for writing to the actual request body
    Flux<DataBuffer> partContent = partContentReady.thenMany(Flux.defer(message::getBody));
    return Flux.concat(generateBoundaryLine(boundary, factory), partContent, generateNewLine(factory));
}
Also used : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Resource(cn.taketoday.core.io.Resource) Publisher(org.reactivestreams.Publisher) MediaType(cn.taketoday.http.MediaType) CodecException(cn.taketoday.core.codec.CodecException) ResolvableType(cn.taketoday.core.ResolvableType) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 3 with DataBufferFactory

use of cn.taketoday.core.io.buffer.DataBufferFactory in project today-framework by TAKETODAY.

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 : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 4 with DataBufferFactory

use of cn.taketoday.core.io.buffer.DataBufferFactory in project today-framework by TAKETODAY.

the class ResourceRegionEncoder method writeResourceRegion.

private Flux<DataBuffer> writeResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {
    Resource resource = region.getResource();
    long position = region.getPosition();
    long count = region.getCount();
    if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
        logger.debug("{}Writing region {}-{} of [{}]", Hints.getLogPrefix(hints), position, (position + count), resource);
    }
    Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
    if (logger.isDebugEnabled()) {
        in = in.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return DataBufferUtils.takeUntilByteCount(in, count);
}
Also used : Assert(cn.taketoday.lang.Assert) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) StreamUtils(cn.taketoday.util.StreamUtils) StandardCharsets(java.nio.charset.StandardCharsets) OptionalLong(java.util.OptionalLong) Flux(reactor.core.publisher.Flux) Nullable(cn.taketoday.lang.Nullable) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) ResourceRegion(cn.taketoday.core.io.ResourceRegion) InputStreamResource(cn.taketoday.core.io.InputStreamResource) MimeType(cn.taketoday.util.MimeType) Resource(cn.taketoday.core.io.Resource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 5 with DataBufferFactory

use of cn.taketoday.core.io.buffer.DataBufferFactory in project today-framework by TAKETODAY.

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 : DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) ResourceDecoder(cn.taketoday.core.codec.ResourceDecoder) ZeroCopyHttpOutputMessage(cn.taketoday.http.ZeroCopyHttpOutputMessage) HttpHeaders(cn.taketoday.http.HttpHeaders) ResourceEncoder(cn.taketoday.core.codec.ResourceEncoder) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) ResourceRegion(cn.taketoday.core.io.ResourceRegion) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) File(java.io.File) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Nullable(cn.taketoday.lang.Nullable) HttpLogging(cn.taketoday.http.HttpLogging) MediaTypeFactory(cn.taketoday.http.MediaTypeFactory) Logger(cn.taketoday.logging.Logger) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) ResourceRegionEncoder(cn.taketoday.core.codec.ResourceRegionEncoder) MediaType(cn.taketoday.http.MediaType) InputStreamResource(cn.taketoday.core.io.InputStreamResource) HttpHeaders(cn.taketoday.http.HttpHeaders) Resource(cn.taketoday.core.io.Resource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) MediaType(cn.taketoday.http.MediaType) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Aggregations

ResolvableType (cn.taketoday.core.ResolvableType)8 Resource (cn.taketoday.core.io.Resource)8 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 DataBufferFactory (cn.taketoday.core.io.buffer.DataBufferFactory)8 Nullable (cn.taketoday.lang.Nullable)8 Map (java.util.Map)8 Publisher (org.reactivestreams.Publisher)8 Flux (reactor.core.publisher.Flux)8 Mono (reactor.core.publisher.Mono)8 Hints (cn.taketoday.core.codec.Hints)6 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)6 HttpHeaders (cn.taketoday.http.HttpHeaders)6 MediaType (cn.taketoday.http.MediaType)6 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)6 Assert (cn.taketoday.lang.Assert)6 List (java.util.List)6 Optional (java.util.Optional)6 MultiValueMap (cn.taketoday.core.MultiValueMap)4 ResolvableTypeProvider (cn.taketoday.core.ResolvableTypeProvider)4 CharSequenceEncoder (cn.taketoday.core.codec.CharSequenceEncoder)4