Search in sources :

Example 1 with MessageBodyWriterContext

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));
}
Also used : Objects(java.util.Objects) Flow(java.util.concurrent.Flow) DataChunk(io.helidon.common.http.DataChunk) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) 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)

Example 2 with MessageBodyWriterContext

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));
}
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 3 with MessageBodyWriterContext

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);
        }
    });
}
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 4 with MessageBodyWriterContext

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();
}
Also used : MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext)

Example 5 with MessageBodyWriterContext

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));
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) URL(java.net.URL) Flow(java.util.concurrent.Flow) Function(java.util.function.Function) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) DataChunk(io.helidon.common.http.DataChunk) Test(org.junit.jupiter.api.Test)

Aggregations

MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)6 DataChunk (io.helidon.common.http.DataChunk)5 GenericType (io.helidon.common.GenericType)4 MediaType (io.helidon.common.http.MediaType)4 Multi (io.helidon.common.reactive.Multi)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Flow (java.util.concurrent.Flow)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Objects (java.util.Objects)3 Jsonb (jakarta.json.bind.Jsonb)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Single (io.helidon.common.reactive.Single)1 MessageBodyReaderContext (io.helidon.media.common.MessageBodyReaderContext)1 ObjectToChunks (io.helidon.media.jsonb.JsonbBodyWriter.ObjectToChunks)1 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)1 JsonStructure (jakarta.json.JsonStructure)1 JsonWriterFactory (jakarta.json.JsonWriterFactory)1 URL (java.net.URL)1 Path (java.nio.file.Path)1