Search in sources :

Example 11 with MediaType

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

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

the class MultiPartBodyWriter method write.

@Override
public Publisher<DataChunk> write(Single<? extends WriteableMultiPart> content, GenericType<? extends WriteableMultiPart> type, MessageBodyWriterContext context) {
    MediaType mediaType = MediaType.MULTIPART_FORM_DATA;
    MediaType mediaWithBoundary = MediaType.builder().type(mediaType.type()).subtype(mediaType.subtype()).addParameter("boundary", "\"" + boundary + "\"").build();
    context.headers().put(Http.Header.CONTENT_TYPE, mediaWithBoundary.toString());
    return content.flatMap(new MultiPartToChunks(boundary, context));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 13 with MediaType

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

the class MetricsSupport method getByName.

private void getByName(ServerRequest req, ServerResponse res, Registry registry) {
    String metricName = req.path().param("metric");
    registry.getOptionalMetricEntry(metricName).ifPresentOrElse(entry -> {
        MediaType mediaType = findBestAccepted(req.headers());
        if (mediaType == MediaType.APPLICATION_JSON) {
            sendJson(res, jsonDataByName(registry, metricName));
        } else if (mediaType == MediaType.TEXT_PLAIN) {
            res.send(prometheusDataByName(registry, metricName));
        } else {
            res.status(Http.Status.NOT_ACCEPTABLE_406);
            res.send();
        }
    }, () -> {
        res.status(Http.Status.NOT_FOUND_404);
        res.send();
    });
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 14 with MediaType

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

the class MetricsSupport method getAll.

private static void getAll(ServerRequest req, ServerResponse res, Registry registry) {
    if (registry.empty()) {
        res.status(Http.Status.NO_CONTENT_204);
        res.send();
        return;
    }
    MediaType mediaType = findBestAccepted(req.headers());
    if (mediaType == MediaType.APPLICATION_JSON) {
        sendJson(res, toJsonData(registry));
    } else if (mediaType == MediaType.TEXT_PLAIN) {
        res.send(toPrometheusData(registry));
    } else {
        res.status(Http.Status.NOT_ACCEPTABLE_406);
        res.send();
    }
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 15 with MediaType

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

the class FormParamsBodyReader method read.

@Override
@SuppressWarnings("unchecked")
public <U extends FormParams> Single<U> read(Flow.Publisher<DataChunk> publisher, GenericType<U> type, MessageBodyReaderContext context) {
    MediaType mediaType = context.contentType().orElseThrow();
    Charset charset = mediaType.charset().map(Charset::forName).orElse(StandardCharsets.UTF_8);
    Function<String, String> decoder = decoder(mediaType, charset);
    return (Single<U>) ContentReaders.readString(publisher, charset).map(formStr -> create(formStr, mediaType, decoder));
}
Also used : URLDecoder(java.net.URLDecoder) DataChunk(io.helidon.common.http.DataChunk) GenericType(io.helidon.common.GenericType) Function(java.util.function.Function) StandardCharsets(java.nio.charset.StandardCharsets) MediaType(io.helidon.common.http.MediaType) Matcher(java.util.regex.Matcher) Charset(java.nio.charset.Charset) Flow(java.util.concurrent.Flow) Map(java.util.Map) FormParams(io.helidon.common.http.FormParams) Single(io.helidon.common.reactive.Single) Pattern(java.util.regex.Pattern) Single(io.helidon.common.reactive.Single) MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset)

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