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