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));
}
});
});
}
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);
}
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");
}
});
}
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");
}
});
}
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));
}
});
});
}
Aggregations