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