Search in sources :

Example 6 with MediaType

use of io.helidon.common.http.MediaType 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 7 with MediaType

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

the class JsonpBodyWriter method write.

@Override
public Publisher<DataChunk> write(Single<? extends JsonStructure> content, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
    context.contentType(contentType);
    return content.map(new JsonStructureToChunks(jsonWriterFactory, context.charset()));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 8 with MediaType

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

the class JsonpNdBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(MediaType.APPLICATION_X_NDJSON);
    context.contentType(contentType);
    JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
    AtomicBoolean first = new AtomicBoolean(true);
    return Multi.create(publisher).map(jsonToChunks).flatMap(dataChunk -> {
        if (first.getAndSet(false)) {
            return Single.just(dataChunk);
        } else {
            return Multi.just(DataChunk.create(NL), dataChunk);
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Example 9 with MediaType

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

the class JsonbBodyWriter method write.

@Override
public Publisher<DataChunk> write(Single<? extends Object> content, GenericType<? extends Object> type, MessageBodyWriterContext context) {
    MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
    context.contentType(contentType);
    return content.flatMap(new ObjectToChunks(jsonb, context.charset()));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 10 with MediaType

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

the class JsonbEsBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
    MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(TEXT_EVENT_STREAM_JSON);
    context.contentType(contentType);
    return Multi.create(publisher).flatMap(m -> Multi.just(DataChunk.create(DATA), DataChunk.create(jsonb.toJson(m).getBytes(StandardCharsets.UTF_8)), DataChunk.create(NL)));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Aggregations

MediaType (io.helidon.common.http.MediaType)37 Charset (java.nio.charset.Charset)6 Map (java.util.Map)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 GenericType (io.helidon.common.GenericType)5 DataChunk (io.helidon.common.http.DataChunk)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Multi (io.helidon.common.reactive.Multi)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)4 Objects (java.util.Objects)4 Flow (java.util.concurrent.Flow)4 Http (io.helidon.common.http.Http)3 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)3 List (java.util.List)3 Single (io.helidon.common.reactive.Single)2 RoutingChecker (io.helidon.webserver.RoutingTest.RoutingChecker)2 RoutingTest.mockResponse (io.helidon.webserver.RoutingTest.mockResponse)2 JsonString (jakarta.json.JsonString)2 Jsonb (jakarta.json.bind.Jsonb)2