Search in sources :

Example 1 with MultipartDataFactory

use of io.micronaut.http.client.multipart.MultipartDataFactory 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

NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 MediaType (io.micronaut.http.MediaType)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 MultipartException (io.micronaut.http.multipart.MultipartException)1 NettyHttpRequestBuilder (io.micronaut.http.netty.NettyHttpRequestBuilder)1 NettyHttpResponseBuilder (io.micronaut.http.netty.NettyHttpResponseBuilder)1 UriBuilder (io.micronaut.http.uri.UriBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)1 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 File (java.io.File)1