io.quarkus.vertx.web.runtime.MultiNdjsonSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-reactive-routes Show documentation
Show all versions of quarkus-reactive-routes Show documentation
REST framework offering the route model to define non blocking endpoints
package io.quarkus.vertx.web.runtime;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import io.smallrye.mutiny.Multi;
import io.vertx.core.AsyncResult;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.ext.web.RoutingContext;
@SuppressWarnings("ReactiveStreamsSubscriberImplementation")
public class MultiNdjsonSupport {
private MultiNdjsonSupport() {
// Avoid direct instantiation.
}
private static void initialize(HttpServerResponse response, RoutingContext rc) {
if (response.bytesWritten() == 0) {
MultiMap headers = response.headers();
if (headers.get(HttpHeaders.CONTENT_TYPE) == null) {
if (rc.getAcceptableContentType() == null) {
headers.set(HttpHeaders.CONTENT_TYPE, "application/x-ndjson");
} else {
headers.set(HttpHeaders.CONTENT_TYPE, rc.getAcceptableContentType());
}
}
response.setChunked(true);
}
}
public static void subscribeString(Multi multi, RoutingContext rc) {
write(multi.map(s -> Buffer.buffer("\"" + s + "\"\n")), rc);
}
public static void subscribeObject(Multi