Search in sources :

Example 6 with DataChunk

use of io.helidon.common.http.DataChunk in project helidon by oracle.

the class MultiPartDecoder method releaseChunks.

private void releaseChunks() {
    Iterator<DataChunk> it = chunksByIds.values().iterator();
    while (it.hasNext()) {
        DataChunk next = it.next();
        next.release();
        it.remove();
    }
}
Also used : DataChunk(io.helidon.common.http.DataChunk)

Example 7 with DataChunk

use of io.helidon.common.http.DataChunk in project helidon by oracle.

the class ReadableByteChannelPublisher method publishSingleOrFinish.

/**
 * It publish a single item or complete or both. If next item is not yet available but it can be in the future then returns
 * {@code false} and call will be rescheduled based on {@link RetrySchema}.
 *
 * @param subscr a subscriber to publish on
 * @return {@code true} if next item was published or subscriber was completed otherwise {@code false}
 * @throws Exception if any error happens and {@code onError()} must be called on the subscriber
 */
private boolean publishSingleOrFinish(Flow.Subscriber<? super DataChunk> subscr) throws Exception {
    DataChunk chunk;
    if (currentChunk == null) {
        chunk = allocateNewChunk();
    } else {
        chunk = currentChunk;
        currentChunk = null;
    }
    ByteBuffer bb = chunk.data()[0];
    int count = 0;
    while (bb.remaining() > 0) {
        count = channel.read(bb);
        if (count <= 0) {
            break;
        }
    }
    // Send or store
    if (bb.capacity() > bb.remaining()) {
        bb.flip();
        subscr.onNext(chunk);
    } else {
        currentChunk = chunk;
    }
    // Last or not
    if (count < 0) {
        try {
            channel.close();
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Cannot close readable byte channel! (Close attempt after fully read channel.)", e);
        }
        tryComplete();
        if (currentChunk != null) {
            currentChunk.release();
        }
        return true;
    } else {
        return count > 0;
    }
}
Also used : DataChunk(io.helidon.common.http.DataChunk) ByteBuffer(java.nio.ByteBuffer) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with DataChunk

use of io.helidon.common.http.DataChunk in project helidon by oracle.

the class JsonpBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
    context.contentType(contentType);
    // we do not have join operator
    AtomicBoolean first = new AtomicBoolean(true);
    JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
    return Multi.create(publisher).map(jsonToChunks).flatMap(it -> {
        if (first.getAndSet(false)) {
            // first record, do not prepend a comma
            return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
        } else {
            // any subsequent record starts with a comma
            return Multi.just(DataChunk.create(COMMA_BYTES), it);
        }
    }).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) JsonStructure(jakarta.json.JsonStructure) Publisher(java.util.concurrent.Flow.Publisher) DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Example 9 with DataChunk

use of io.helidon.common.http.DataChunk in project helidon by oracle.

the class JsonbNdBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
    MediaType contentType = MediaType.APPLICATION_X_NDJSON;
    context.contentType(contentType);
    AtomicBoolean first = new AtomicBoolean(true);
    return Multi.create(publisher).map(object -> DataChunk.create(jsonb.toJson(object).getBytes(StandardCharsets.UTF_8))).flatMap(dataChunk -> {
        if (first.getAndSet(false)) {
            return Single.just(dataChunk);
        } else {
            return Multi.just(DataChunk.create(NL), dataChunk);
        }
    });
}
Also used : DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) StandardCharsets(java.nio.charset.StandardCharsets) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) Objects(java.util.Objects) Jsonb(jakarta.json.bind.Jsonb) Flow(java.util.concurrent.Flow) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) Multi(io.helidon.common.reactive.Multi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType)

Example 10 with DataChunk

use of io.helidon.common.http.DataChunk in project helidon by oracle.

the class ObjectStorageService method upload.

private void upload(ServerRequest req, ServerResponse res) {
    OptionalLong contentLength = req.headers().contentLength();
    if (contentLength.isEmpty()) {
        req.content().forEach(DataChunk::release);
        res.status(Http.Status.BAD_REQUEST_400).send("Content length must be defined");
        return;
    }
    String objectName = req.path().param("file-name");
    PutObject.Request request = PutObject.Request.builder().objectName(objectName).bucket(bucketName).contentLength(contentLength.getAsLong());
    req.headers().contentType().ifPresent(request::requestMediaType);
    objectStorage.putObject(request, req.content()).forSingle(response -> res.send(response.requestId())).exceptionally(res::send);
}
Also used : DeleteObject(io.helidon.integrations.oci.objectstorage.DeleteObject) DataChunk(io.helidon.common.http.DataChunk) RenameObject(io.helidon.integrations.oci.objectstorage.RenameObject) ServerRequest(io.helidon.webserver.ServerRequest) GetObject(io.helidon.integrations.oci.objectstorage.GetObject) OptionalLong(java.util.OptionalLong) PutObject(io.helidon.integrations.oci.objectstorage.PutObject) GetObjectRx(io.helidon.integrations.oci.objectstorage.GetObjectRx) OciObjectStorageRx(io.helidon.integrations.oci.objectstorage.OciObjectStorageRx) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Service(io.helidon.webserver.Service) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) OptionalLong(java.util.OptionalLong) DataChunk(io.helidon.common.http.DataChunk) PutObject(io.helidon.integrations.oci.objectstorage.PutObject)

Aggregations

DataChunk (io.helidon.common.http.DataChunk)43 Test (org.junit.jupiter.api.Test)18 Multi (io.helidon.common.reactive.Multi)12 Flow (java.util.concurrent.Flow)11 GenericType (io.helidon.common.GenericType)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Http (io.helidon.common.http.Http)8 Single (io.helidon.common.reactive.Single)8 ByteBuffer (java.nio.ByteBuffer)8 MediaType (io.helidon.common.http.MediaType)7 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)7 WebClient (io.helidon.webclient.WebClient)7 WebClientResponse (io.helidon.webclient.WebClientResponse)7 IOException (java.io.IOException)7 List (java.util.List)7 Optional (java.util.Optional)7 TimeUnit (java.util.concurrent.TimeUnit)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Logger (java.util.logging.Logger)6