use of io.helidon.media.common.MessageBodyWriterContext in project helidon by oracle.
the class JacksonBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
AtomicBoolean first = new AtomicBoolean(true);
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset());
return Multi.create(publisher).flatMap(objectToChunks).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.media.common.MessageBodyWriterContext 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.media.common.MessageBodyWriterContext 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);
}
});
}
use of io.helidon.media.common.MessageBodyWriterContext in project helidon by oracle.
the class OpenAPISupport method registerJsonpSupport.
private void registerJsonpSupport(ServerRequest req, ServerResponse res) {
MessageBodyReaderContext readerContext = req.content().readerContext();
MessageBodyWriterContext writerContext = res.writerContext();
JsonpSupport.create().register(readerContext, writerContext);
req.next();
}
use of io.helidon.media.common.MessageBodyWriterContext in project helidon by oracle.
the class UnstableTempTest method cleanedTmpDuringRuntime.
@Test
void cleanedTmpDuringRuntime() throws IOException {
List<String> contents = new ArrayList<>(2);
Path jar = createJar();
URL jarUrl = new URL("jar:file:" + jar.toUri().getPath() + "!/" + FILE_NAME);
LOGGER.fine(() -> "Generated test jar url: " + jarUrl.toString());
ClassPathContentHandler classPathContentHandler = new ClassPathContentHandler(null, new ContentTypeSelector(null), "/", tmpDir, Thread.currentThread().getContextClassLoader());
// Empty headers
RequestHeaders headers = mock(RequestHeaders.class);
when(headers.isAccepted(any())).thenReturn(true);
when(headers.acceptedTypes()).thenReturn(Collections.emptyList());
ResponseHeaders responseHeaders = mock(ResponseHeaders.class);
ServerRequest request = Mockito.mock(ServerRequest.class);
Mockito.when(request.headers()).thenReturn(headers);
ServerResponse response = Mockito.mock(ServerResponse.class);
MessageBodyWriterContext ctx = MessageBodyWriterContext.create(HashParameters.create());
ctx.registerFilter(dataChunkPub -> {
String fileContent = new String(Single.create(dataChunkPub).await().bytes());
contents.add(fileContent);
return Single.just(DataChunk.create(ByteBuffer.wrap(fileContent.getBytes())));
});
Mockito.when(response.headers()).thenReturn(responseHeaders);
@SuppressWarnings("unchecked") Function<MessageBodyWriterContext, Flow.Publisher<DataChunk>> anyFunction = (Function<MessageBodyWriterContext, Flow.Publisher<DataChunk>>) Mockito.any(Function.class);
Mockito.when(response.send(anyFunction)).then(mock -> {
Function<MessageBodyWriterContext, Flow.Publisher<DataChunk>> argument = mock.getArgument(0);
return Single.create(argument.apply(ctx)).onError(throwable -> throwable.printStackTrace());
});
classPathContentHandler.sendJar(Http.Method.GET, FILE_NAME, jarUrl, request, response);
deleteTmpFiles();
classPathContentHandler.sendJar(Http.Method.GET, FILE_NAME, jarUrl, request, response);
assertThat(contents, containsInAnyOrder(FILE_CONTENT, FILE_CONTENT));
}
Aggregations