Search in sources :

Example 1 with CompletedPart

use of io.micronaut.http.multipart.CompletedPart in project micronaut-core by micronaut-projects.

the class UploadController method go.

@Post(value = "/receive-multipart-body", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
@SingleResult
public Publisher<String> go(@Body MultipartBody multipartBody) {
    return Mono.create(emitter -> {
        multipartBody.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 2 with CompletedPart

use of io.micronaut.http.multipart.CompletedPart in project micronaut-core by micronaut-projects.

the class UploadController method multipartBodyWithPrincipal.

@Post(value = "/receive-multipart-body-principal", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
@SingleResult
public Publisher<String> multipartBodyWithPrincipal(Principal principal, @Body MultipartBody multipartBody) {
    return Mono.create(emitter -> {
        multipartBody.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 3 with CompletedPart

use of io.micronaut.http.multipart.CompletedPart 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)

Aggregations

SingleResult (io.micronaut.core.async.annotation.SingleResult)3 Post (io.micronaut.http.annotation.Post)3 CompletedPart (io.micronaut.http.multipart.CompletedPart)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Subscription (org.reactivestreams.Subscription)3