/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, File file) { return addFilePart(new FilePart(name, filename, file)); }
/** * Add bytes data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param data A byte Array (byte[]) representing the contents of the file * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, byte[] data) { return addFilePart(new BytePart(name, filename, data)); }
/** * @return A Builder to build MultipartBody. */ public static Builder builder() { return new Builder(); }
/** * Add a InputStream data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param data An {@link InputStream} data value representing the content of file object * @param contentLength The size of the content to pass to {@link HttpDataFactory} in order to create * {@link io.netty.handler.codec.http.multipart.FileUpload} object * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, InputStream data, long contentLength) { return addFilePart(new InputStreamPart(name, filename, data, contentLength)); }
/** * Create an object of {@link InterfaceHttpData} to build Netty multipart request. * * @see Part#getData(HttpRequest, HttpDataFactory) */ @Override InterfaceHttpData getData(HttpRequest request, HttpDataFactory factory) { MediaType mediaType = contentType; String contentType = mediaType.toString(); String encoding = mediaType.isTextBased() ? null : "binary"; FileUpload fileUpload = factory.createFileUpload(request, name, filename, contentType, encoding, null, getLength()); try { setContent(fileUpload); } catch (IOException e) { throw new IllegalArgumentException(e); } return fileUpload; } }
private HttpPostRequestEncoder buildMultipartRequest(NettyClientHttpRequest clientHttpRequest, Object bodyValue) throws HttpPostRequestEncoder.ErrorDataEncoderException { HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); io.netty.handler.codec.http.HttpRequest request = clientHttpRequest.getFullRequest(null); 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) { postRequestEncoder.setBodyHttpDatas(((MultipartBody) bodyValue).getData(request, factory)); } else { throw new MultipartException(String.format("The type %s is not a supported type for a multipart request body", bodyValue.getClass().getName())); } return postRequestEncoder; }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, File file) { return addPart(name, file.getName(), file); }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter or the key to be passed in multipart request * @param value Plain String value for the parameter * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String value) { parts.add(new StringPart(name, value)); return this; }
/** * Create a list of {@link InterfaceHttpData} to build Netty multipart request. * * @param request Associated request * @param factory The factory used to create the {@link InterfaceHttpData} * @return List of {@link InterfaceHttpData} objects to build Netty multipart request */ public List<InterfaceHttpData> getData(HttpRequest request, HttpDataFactory factory) { List<InterfaceHttpData> data = new ArrayList<>(parts.size()); for (Part part : parts) { data.add(part.getData(request, factory)); } return data; }
/** * Creates {@link MultipartBody} from the provided parts. * * @return The {@link MultipartBody} * @throws MultipartException If there are no parts */ public MultipartBody build() throws MultipartException { if (parts.isEmpty()) { throw new MultipartException("Cannot create a MultipartBody with no parts"); } return new MultipartBody(parts); } }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType File content of type {@link MediaType}, possible values could be "text/plain", "application/json" etc * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, File file) { return addFilePart(new FilePart(name, filename, contentType, file)); }
/** * Add bytes data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType The content type of File, possible values could be "text/plain", "application/json" etc * @param data A byte Array (byte[]) representing the contents of the file * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, byte[] data) { return addFilePart(new BytePart(name, filename, contentType, data)); }
/** * Add a InputStream data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType The content type of File, possible values could be "text/plain", "application/json" etc * @param data An {@link InputStream} data value representing the content of file object * @param contentLength The size of the content to pass to {@link HttpDataFactory} in order to create * {@link io.netty.handler.codec.http.multipart.FileUpload} object * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, InputStream data, long contentLength) { return addFilePart(new InputStreamPart(name, filename, contentType, data, contentLength)); }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, File file) { return addFilePart(new FilePart(name, filename, file)); }
/** * Add bytes data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType The content type of File, possible values could be "text/plain", "application/json" etc * @param data A byte Array (byte[]) representing the contents of the file * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, byte[] data) { return addFilePart(new BytePart(name, filename, contentType, data)); }
/** * Add a InputStream data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param data An {@link InputStream} data value representing the content of file object * @param contentLength The size of the content to pass to {@link HttpDataFactory} in order to create * {@link io.netty.handler.codec.http.multipart.FileUpload} object * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, InputStream data, long contentLength) { return addFilePart(new InputStreamPart(name, filename, data, contentLength)); }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType File content of type {@link MediaType}, possible values could be "text/plain", "application/json" etc * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, File file) { return addFilePart(new FilePart(name, filename, contentType, file)); }
/** * Add bytes data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param data A byte Array (byte[]) representing the contents of the file * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, byte[] data) { return addFilePart(new BytePart(name, filename, data)); }
/** * Add a InputStream data to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param contentType The content type of File, possible values could be "text/plain", "application/json" etc * @param data An {@link InputStream} data value representing the content of file object * @param contentLength The size of the content to pass to {@link HttpDataFactory} in order to create * {@link io.netty.handler.codec.http.multipart.FileUpload} object * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, MediaType contentType, InputStream data, long contentLength) { return addFilePart(new InputStreamPart(name, filename, contentType, data, contentLength)); }
/** * Add a file object to MultipartBody. * * @param name Name of the parameter for file object to be passed in multipart request * @param filename Name of the file * @param file The file object to copy the content to {@link io.micronaut.http.multipart.FileUpload} * @return A {@link MultipartBody.Builder} to build MultipartBody */ public Builder addPart(String name, String filename, File file) { return addFilePart(new FilePart(name, filename, file)); }