/** * Configures multipart request content using a {@link Consumer} which will have an instance of {@link MultipartContent} passed into it for * configuring the multipart content data. * * @param config the configuration {@link Consumer} * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(final Consumer<MultipartContent> config) { MultipartContent content = new MultipartContent(); config.accept(content); return content; }
/** * Configures a field part with the given field name and value (of the specified content type). * * @param fieldName the field name * @param contentType the value content type * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent part(String fieldName, String contentType, String value) { return part(fieldName, null, contentType, value); }
/** * Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be * encoded using the encoders available to the {@link ChainedHttpConfig} object. * * @param config the chained configuration object * @param ts the server adapter */ public static void multipart(final ChainedHttpConfig config, final ToServer ts) { try { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); } final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); } final MimeMultipart mimeMultipart = new MimeMultipart(); for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { mimeMultipart.addBodyPart(part(config, mpe)); } request.setContentType(mimeMultipart.getContentType()); final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); mimeMultipart.writeTo(bytesOut); ts.toServer(new ByteArrayInputStream(bytesOut.toByteArray())); } catch (IOException | MessagingException ioe) { ioe.printStackTrace(); } }
private static InputStream stream(final ChainedHttpConfig config, final MultipartContent content) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024); try { for (final MultipartContent.MultipartPart part : content.parts()) { buffer.write(string(BOUNDARY_MARK, content.boundary(), CRLF)); buffer.write(string("Content-Type: ", part.getContentType(), CRLF)); if (part.getFileName() != null) { buffer.write(string("Content-Disposition: form-data; name=\"", part.getFieldName(), "\"; filename=\"", part.getFileName(), "\"", CRLF)); } else { buffer.write(string("Content-Disposition: form-data; name=\"", part.getFieldName(), "\"", CRLF)); } // TODO: do we need content-transfer-encoding? // buffer.write(string("Content-Transfer-Encoding: ", , CRLF)); buffer.write(string(CRLF)); buffer.write(EmbeddedEncoder.encode(config, part.getContentType(), part.getContent())); buffer.write(string(CRLF)); } buffer.write(string(BOUNDARY_MARK, content.boundary(), BOUNDARY_MARK)); } catch (IOException e) { throw new RuntimeException("Problem while encoding multipart content: " + e.getMessage()); } return new ByteArrayInputStream(buffer.toByteArray()); }
@Override public void accept(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); NativeHandlers.Encoders.checkTypes(body, MULTIPART_TYPES); MultipartContent mp = (MultipartContent) request.actualBody(); request.setContentType("multipart/form-data; boundary=" + mp.boundary()); ts.toServer(stream(config, mp)); }
/** * Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be * encoded using the encoders available to the {@link ChainedHttpConfig} object. * * @param config the chained configuration object * @param ts the server adapter */ public static void multipart(final ChainedHttpConfig config, final ToServer ts) { try { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); } final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); } final MimeMultipart mimeMultipart = new MimeMultipart(); for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { mimeMultipart.addBodyPart(part(config, mpe)); } request.setContentType(mimeMultipart.getContentType()); final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); mimeMultipart.writeTo(bytesOut); ts.toServer(new ByteArrayInputStream(bytesOut.toByteArray())); } catch (IOException | MessagingException ioe) { ioe.printStackTrace(); } }
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent());
/** * Configures a field part with the given field name and value. * * @param fieldName the field name * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent field(String fieldName, String value) { return part(fieldName, value); }
/** * Configures multipart request content using a {@link Consumer} which will have an instance of {@link MultipartContent} passed into it for * configuring the multipart content data. * * @param config the configuration {@link Consumer} * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(final Consumer<MultipartContent> config) { MultipartContent content = new MultipartContent(); config.accept(content); return content; }
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent());
/** * Configures a field part with the given field name and value (of the specified content type). * * @param fieldName the field name * @param contentType the value content type * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent part(String fieldName, String contentType, String value) { return part(fieldName, null, contentType, value); }
/** * Configures multipart request content using a {@link Consumer} which will have an instance of {@link MultipartContent} passed into it for * configuring the multipart content data. * * @param config the configuration {@link Consumer} * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(final Consumer<MultipartContent> config) { MultipartContent content = new MultipartContent(); config.accept(content); return content; }
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { builder.addFormDataPart(mpe.getFieldName(), (String) mpe.getContent());
/** * Configures a field part with the given field name and value (of the specified content type). * * @param fieldName the field name * @param contentType the value content type * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent field(String fieldName, String contentType, String value) { return part(fieldName, contentType, value); }
/** * Configures multipart request content using a Groovy closure (delegated to {@link MultipartContent}). * * @param closure the configuration closure * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(@DelegatesTo(MultipartContent.class) Closure closure) { MultipartContent content = new MultipartContent(); closure.setDelegate(content); closure.call(); return content; }
/** * Configures a field part with the given field name and value. * * @param fieldName the field name * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent field(String fieldName, String value) { return part(fieldName, value); }
/** * Configures multipart request content using a Groovy closure (delegated to {@link MultipartContent}). * * @param closure the configuration closure * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(@DelegatesTo(MultipartContent.class) Closure closure) { MultipartContent content = new MultipartContent(); closure.setDelegate(content); closure.call(); return content; }
/** * Configures a field part with the given field name and value (of the specified content type). * * @param fieldName the field name * @param contentType the value content type * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent field(String fieldName, String contentType, String value) { return part(fieldName, contentType, value); }
/** * Configures multipart request content using a Groovy closure (delegated to {@link MultipartContent}). * * @param closure the configuration closure * @return a configured instance of {@link MultipartContent} */ public static MultipartContent multipart(@DelegatesTo(MultipartContent.class) Closure closure) { MultipartContent content = new MultipartContent(); closure.setDelegate(content); closure.call(); return content; }
/** * Configures a field part with the given field name and value (of the specified content type). * * @param fieldName the field name * @param contentType the value content type * @param value the value * @return a reference to this {@link MultipartContent} instance */ public MultipartContent field(String fieldName, String contentType, String value) { return part(fieldName, contentType, value); }