All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.fortnox.reactivewizard.jaxrs.response.JaxRsStreamingResult Maven / Gradle / Ivy

There is a newer version: 24.6.0
Show newest version
package se.fortnox.reactivewizard.jaxrs.response;

import io.netty.handler.codec.http.HttpResponseStatus;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerResponse;

import java.util.Map;
import java.util.function.Function;

import static reactor.core.publisher.Flux.defer;
import static reactor.core.publisher.Flux.empty;

public class JaxRsStreamingResult extends JaxRsResult {
    public JaxRsStreamingResult(Flux output, HttpResponseStatus responseStatus, Function, Flux> serializer, Map headers) {
        super(output, responseStatus, serializer, headers);
    }

    @Override
    public Publisher write(HttpServerResponse response) {
        return output.switchOnFirst((signal, outputBuffered) -> {
            if (signal.isOnError()) {
                return Flux.error(signal.getThrowable());
            }
            response.status(responseStatus);
            headers.forEach(response::addHeader);
            return response.sendByteArray(serializer.apply(outputBuffered));
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy