Search in sources :

Example 1 with MultipartException

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();
}
Also used : Document(org.bson.Document) JsonObject(com.google.gson.JsonObject) StreamingFileUpload(io.micronaut.http.multipart.StreamingFileUpload) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) Produces(io.micronaut.http.annotation.Produces) Level(java.util.logging.Level) TaskExecutors(io.micronaut.scheduling.TaskExecutors) PartData(io.micronaut.http.multipart.PartData) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(io.micronaut.http.MediaType) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) Schedulers(reactor.core.scheduler.Schedulers) ZuliaNodeProvider(io.zulia.server.util.ZuliaNodeProvider) MultipartException(io.micronaut.http.multipart.MultipartException) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) Subscriber(org.reactivestreams.Subscriber) ExecutorService(java.util.concurrent.ExecutorService) Get(io.micronaut.http.annotation.Get) ZuliaConstants(io.zulia.ZuliaConstants) ZuliaBase(io.zulia.message.ZuliaBase) OutputStream(java.io.OutputStream) Controller(io.micronaut.http.annotation.Controller) Publisher(org.reactivestreams.Publisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) QueryValue(io.micronaut.http.annotation.QueryValue) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Logger(java.util.logging.Logger) Post(io.micronaut.http.annotation.Post) Flux(reactor.core.publisher.Flux) JsonArray(com.google.gson.JsonArray) List(java.util.List) Writable(io.micronaut.core.io.Writable) Subscription(org.reactivestreams.Subscription) Inject(jakarta.inject.Inject) Named(jakarta.inject.Named) InputStream(java.io.InputStream) MultipartException(io.micronaut.http.multipart.MultipartException) PartData(io.micronaut.http.multipart.PartData) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription)

Example 2 with MultipartException

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;
}
Also used : InputStream(java.io.InputStream) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) NettyClientSslBuilder(io.micronaut.http.client.netty.ssl.NettyClientSslBuilder) NettyHttpRequestBuilder(io.micronaut.http.netty.NettyHttpRequestBuilder) NettyHttpResponseBuilder(io.micronaut.http.netty.NettyHttpResponseBuilder) UriBuilder(io.micronaut.http.uri.UriBuilder) Charset(java.nio.charset.Charset) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) NonNull(io.micronaut.core.annotation.NonNull) MultipartException(io.micronaut.http.multipart.MultipartException) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) io.netty.handler.codec.http(io.netty.handler.codec.http) MediaType(io.micronaut.http.MediaType) MultipartDataFactory(io.micronaut.http.client.multipart.MultipartDataFactory) File(java.io.File) Nullable(io.micronaut.core.annotation.Nullable) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Aggregations

Nullable (io.micronaut.core.annotation.Nullable)2 MediaType (io.micronaut.http.MediaType)2 MultipartException (io.micronaut.http.multipart.MultipartException)2 InputStream (java.io.InputStream)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 NonNull (io.micronaut.core.annotation.NonNull)1 Writable (io.micronaut.core.io.Writable)1 HttpResponse (io.micronaut.http.HttpResponse)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Controller (io.micronaut.http.annotation.Controller)1 Get (io.micronaut.http.annotation.Get)1 Post (io.micronaut.http.annotation.Post)1 Produces (io.micronaut.http.annotation.Produces)1 QueryValue (io.micronaut.http.annotation.QueryValue)1 MultipartBody (io.micronaut.http.client.multipart.MultipartBody)1 MultipartDataFactory (io.micronaut.http.client.multipart.MultipartDataFactory)1 NettyClientSslBuilder (io.micronaut.http.client.netty.ssl.NettyClientSslBuilder)1 PartData (io.micronaut.http.multipart.PartData)1 StreamingFileUpload (io.micronaut.http.multipart.StreamingFileUpload)1