Search in sources :

Example 1 with SingleResult

use of io.micronaut.core.async.annotation.SingleResult 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 SingleResult

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

the class PetFallback method save.

@Override
@SingleResult
public Publisher<Pet> save(String name, int age) {
    Pet pet = new Pet();
    pet.setAge(age);
    pet.setName(name);
    return Mono.just(pet);
}
Also used : Pet(io.micronaut.docs.annotation.Pet) SingleResult(io.micronaut.core.async.annotation.SingleResult)

Example 3 with SingleResult

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

the class UploadController method uploadOutputStream.

// end::file[]
// tag::outputStream[]
// <1>
@Post(value = "/outputStream", consumes = MULTIPART_FORM_DATA, produces = TEXT_PLAIN)
@SingleResult
public Mono<HttpResponse<String>> uploadOutputStream(StreamingFileUpload file) {
    // <2>
    // <3>
    OutputStream outputStream = new ByteArrayOutputStream();
    // <4>
    Publisher<Boolean> uploadPublisher = file.transferTo(outputStream);
    return // <5>
    Mono.from(uploadPublisher).map(success -> {
        if (success) {
            return HttpResponse.ok("Uploaded");
        } else {
            return HttpResponse.<String>status(CONFLICT).body("Upload Failed");
        }
    });
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SingleResult(io.micronaut.core.async.annotation.SingleResult) Post(io.micronaut.http.annotation.Post)

Example 4 with SingleResult

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

the class UploadController method upload.

// end:class[]
// tag::file[]
// <1>
@Post(value = "/", consumes = MULTIPART_FORM_DATA, produces = TEXT_PLAIN)
@SingleResult
public Publisher<HttpResponse<String>> upload(StreamingFileUpload file) {
    // <2>
    File tempFile;
    try {
        tempFile = File.createTempFile(file.getFilename(), "temp");
    } catch (IOException e) {
        return Mono.error(e);
    }
    // <3>
    Publisher<Boolean> uploadPublisher = file.transferTo(tempFile);
    return // <4>
    Mono.from(uploadPublisher).map(success -> {
        if (success) {
            return HttpResponse.ok("Uploaded");
        } else {
            return HttpResponse.<String>status(CONFLICT).body("Upload Failed");
        }
    });
}
Also used : IOException(java.io.IOException) File(java.io.File) SingleResult(io.micronaut.core.async.annotation.SingleResult) Post(io.micronaut.http.annotation.Post)

Example 5 with SingleResult

use of io.micronaut.core.async.annotation.SingleResult 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)

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