use of io.micronaut.http.multipart.MultipartException in project zuliasearch by zuliaio.
the class AssociatedController method transferToStream.
// work around for
// https://github.com/micronaut-projects/micronaut-core/issues/6084
public static Publisher<Boolean> transferToStream(ExecutorService ioExecutor, StreamingFileUpload fileUpload, OutputStream outputStream) {
return Mono.<Boolean>create(emitter -> Flux.from(fileUpload).subscribeOn(Schedulers.fromExecutorService(ioExecutor)).subscribe(new Subscriber<>() {
Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
subscription = s;
subscription.request(1);
}
@Override
public void onNext(PartData o) {
try {
outputStream.write(o.getBytes());
subscription.request(1);
} catch (IOException e) {
handleError(e);
}
}
@Override
public void onError(Throwable t) {
emitter.error(t);
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
System.err.println("Failed to close file stream : " + fileUpload.getName());
}
}
@Override
public void onComplete() {
try {
outputStream.close();
emitter.success(true);
} catch (IOException e) {
System.err.println("Failed to close file stream : " + fileUpload.getName());
emitter.success(false);
}
}
private void handleError(Throwable t) {
subscription.cancel();
onError(new MultipartException("Error transferring file: " + fileUpload.getName(), t));
}
})).flux();
}
use of io.micronaut.http.multipart.MultipartException in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method buildMultipartRequest.
private HttpPostRequestEncoder buildMultipartRequest(MutableHttpRequest clientHttpRequest, Object bodyValue) throws HttpPostRequestEncoder.ErrorDataEncoderException {
HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
io.netty.handler.codec.http.HttpRequest request = NettyHttpRequestBuilder.toHttpRequest(clientHttpRequest);
HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(factory, request, true, CharsetUtil.UTF_8, HttpPostRequestEncoder.EncoderMode.HTML5);
if (bodyValue instanceof MultipartBody.Builder) {
bodyValue = ((MultipartBody.Builder) bodyValue).build();
}
if (bodyValue instanceof MultipartBody) {
final MultipartBody multipartBody = (MultipartBody) bodyValue;
postRequestEncoder.setBodyHttpDatas(multipartBody.getData(new MultipartDataFactory<InterfaceHttpData>() {
@NonNull
@Override
public InterfaceHttpData createFileUpload(@NonNull String name, @NonNull String filename, @NonNull MediaType contentType, @Nullable String encoding, @Nullable Charset charset, long length) {
return factory.createFileUpload(request, name, filename, contentType.toString(), encoding, charset, length);
}
@NonNull
@Override
public InterfaceHttpData createAttribute(@NonNull String name, @NonNull String value) {
return factory.createAttribute(request, name, value);
}
@Override
public void setContent(InterfaceHttpData fileUploadObject, Object content) throws IOException {
if (fileUploadObject instanceof FileUpload) {
FileUpload fu = (FileUpload) fileUploadObject;
if (content instanceof InputStream) {
fu.setContent((InputStream) content);
} else if (content instanceof File) {
fu.setContent((File) content);
} else if (content instanceof byte[]) {
final ByteBuf buffer = Unpooled.wrappedBuffer((byte[]) content);
fu.setContent(buffer);
}
}
}
}));
} else {
throw new MultipartException(String.format("The type %s is not a supported type for a multipart request body", bodyValue.getClass().getName()));
}
return postRequestEncoder;
}
Aggregations