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