Search in sources :

Example 6 with SingleResult

use of io.micronaut.core.async.annotation.SingleResult in project micronaut-core by micronaut-projects.

the class UploadController method publisherCompletedPart.

@Post(value = "/publisher-completedpart", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
@SingleResult
public Publisher<String> publisherCompletedPart(Publisher<CompletedPart> recipients) {
    return Mono.create(emitter -> {
        recipients.subscribe(new Subscriber<CompletedPart>() {

            private Subscription s;

            List<String> datas = new ArrayList<>();

            @Override
            public void onSubscribe(Subscription s) {
                this.s = s;
                s.request(1);
            }

            @Override
            public void onNext(CompletedPart data) {
                try {
                    datas.add(new String(data.getBytes(), StandardCharsets.UTF_8));
                    s.request(1);
                } catch (IOException e) {
                    s.cancel();
                    emitter.error(e);
                }
            }

            @Override
            public void onError(Throwable t) {
                emitter.error(t);
            }

            @Override
            public void onComplete() {
                emitter.success(String.join("|", datas));
            }
        });
    });
}
Also used : CompletedPart(io.micronaut.http.multipart.CompletedPart) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription) SingleResult(io.micronaut.core.async.annotation.SingleResult) Post(io.micronaut.http.annotation.Post)

Example 7 with SingleResult

use of io.micronaut.core.async.annotation.SingleResult in project micronaut-core by micronaut-projects.

the class UploadController method receiveBigAttribute.

@Post(value = "/receive-big-attribute", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
@SingleResult
public Publisher<HttpResponse> receiveBigAttribute(Publisher<PartData> data) {
    return Mono.create(emitter -> {
        data.subscribe(new Subscriber<PartData>() {

            private Subscription s;

            List<String> datas = new ArrayList<>();

            @Override
            public void onSubscribe(Subscription s) {
                this.s = s;
                s.request(1);
            }

            @Override
            public void onNext(PartData data) {
                try {
                    datas.add(new String(data.getBytes(), StandardCharsets.UTF_8));
                    s.request(1);
                } catch (IOException e) {
                    s.cancel();
                    emitter.error(e);
                }
            }

            @Override
            public void onError(Throwable t) {
                emitter.error(t);
            }

            @Override
            public void onComplete() {
                emitter.success(HttpResponse.ok(String.join("", datas)));
            }
        });
    });
}
Also used : PartData(io.micronaut.http.multipart.PartData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription) SingleResult(io.micronaut.core.async.annotation.SingleResult) Post(io.micronaut.http.annotation.Post)

Aggregations

SingleResult (io.micronaut.core.async.annotation.SingleResult)7 Post (io.micronaut.http.annotation.Post)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 Subscription (org.reactivestreams.Subscription)4 CompletedPart (io.micronaut.http.multipart.CompletedPart)3 Pet (io.micronaut.docs.annotation.Pet)1 PartData (io.micronaut.http.multipart.PartData)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1