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));
}
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()));
}
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);
}
});
}
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()));
}
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)));
}
Aggregations