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()); }
/** * 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(); } }
/** * 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) { builder.addFormDataPart(mpe.getFieldName(), (String) mpe.getContent());
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent());
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent());