type) states:
+ // "Subsequent invocations will result in an {@code IllegalStateException}.
+ // Likewise this method will throw an {@code IllegalStateException} if it is
+ // called after calling {@link #getContent} or {@link #getContent(GenericType)}.
+ try {
+ part.getContent(String.class);
+ Assertions.fail("IllegalStateException is expected when getContent() is " +
+ "invoked more than once.");
+ } catch (IllegalStateException e) {
+ // expected exception
+ } catch (Throwable t) {
+ Assertions.fail("Incorrect Throwable received: " + t);
+ }
+
+ part = find(entityParts, "received-file");
+ Assertions.assertNotNull(part, getMessage(entityParts));
+ Assertions.assertEquals("test file", part.getFileName().get());
+ Assertions.assertTrue(part.getContent(String.class).contains("value6"));
+
+ part = find(entityParts, "added-input-stream");
+ Assertions.assertNotNull(part, getMessage(entityParts));
+ Assertions.assertEquals("Add part on return.", part.getContent(String.class));
+
+ // Check headers. Should be 4: Content-Disposition, Content-Type, and the 2 headers
+ // that were added.
+ if ((part.getHeaders() == null) || (part.getHeaders().size() != 4)) {
+ final String msg = "Expected 4 headers, received " +
+ part.getHeaders().size();
+ Assertions.fail(msg);
+ }
+ Assertions.assertEquals("Test1", part.getHeaders().get("Header1").get(0));
+ Assertions.assertEquals("Test2", part.getHeaders().get("Header2").get(0));
+ }
+ }
+ }
+
+ /**
+ * Verify sending a {@link List} containing three {@link EntityPart}, each injected as a different type.
+ *
+ * The returned result will be {@code multipart/form-data} content with a new name and the content for each
+ * injected field.
+ *
+ *
+ * @throws Exception if an error occurs in the test
+ */
+ @Test
+ public void multiFormParamTest() throws Exception {
+ try (Client client = ClientBuilder.newClient()) {
+ final List multipart = List.of(
+ EntityPart.withName("entity-part")
+ .content("test entity part")
+ .mediaType(MediaType.TEXT_PLAIN_TYPE)
+ .build(),
+ EntityPart.withName("string-part")
+ .content("test string")
+ .mediaType(MediaType.TEXT_PLAIN_TYPE)
+ .build(),
+ EntityPart.withName("input-stream-part")
+ .content("test input stream".getBytes(StandardCharsets.UTF_8))
+ .mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE)
+ .build());
+ try (
+ Response response = client.target(createCombinedUri(uri, "test/multi-form-param"))
+ .request(MediaType.MULTIPART_FORM_DATA_TYPE)
+ .post(Entity.entity(new GenericEntity<>(multipart) {
+ }, MediaType.MULTIPART_FORM_DATA))) {
+ Assertions.assertEquals(200, response.getStatus());
+ final List entityParts = response.readEntity(new GenericType<>() {
+ });
+ if (entityParts.size() != 3) {
+ final String msg = "Expected 3 entries, received " +
+ entityParts.size() +
+ '.' +
+ System.lineSeparator() +
+ getMessage(entityParts);
+ Assertions.fail(msg);
+ }
+ verifyEntityPart(entityParts, "received-entity-part", "test entity part");
+ verifyEntityPart(entityParts, "received-string", "test string");
+ verifyEntityPart(entityParts, "received-input-stream", "test input stream");
+ }
+ }
+ }
+
+ private static void verifyEntityPart(final List parts, final String name, final String text)
+ throws IOException {
+ final EntityPart part = find(parts, name);
+ Assertions.assertNotNull(part,
+ String.format("Failed to find entity part %s in: %s", name, getMessage(parts)));
+ Assertions.assertEquals(text, part.getContent(String.class));
+ }
+
+ private static Supplier getMessage(final List parts) {
+ return () -> {
+ final StringBuilder msg = new StringBuilder();
+ final Iterator iter = parts.iterator();
+ while (iter.hasNext()) {
+ final EntityPart part = iter.next();
+ try {
+ msg.append('[')
+ .append(part.getName())
+ .append("={")
+ .append("headers=")
+ .append(part.getHeaders())
+ .append(", mediaType=")
+ .append(part.getMediaType())
+ .append(", body=")
+ .append(toString(part.getContent()))
+ .append('}');
+ } catch (IOException e) {
+ Assertions.fail("Unable to proces Entityparts." + e);
+ }
+ if (iter.hasNext()) {
+ msg.append("], ");
+ } else {
+ msg.append(']');
+ }
+ }
+ return msg.toString();
+ };
+ }
+
+ private static String toString(final InputStream in) throws IOException {
+ // try-with-resources fails here due to a bug in the
+ //noinspection TryFinallyCanBeTryWithResources
+ try {
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] buffer = new byte[32];
+ int len;
+ while ((len = in.read(buffer)) > 0) {
+ out.write(buffer, 0, len);
+ }
+ return out.toString(StandardCharsets.UTF_8);
+ } finally {
+ in.close();
+ }
+ }
+
+ private static EntityPart find(final Collection parts, final String name) {
+ for (EntityPart part : parts) {
+ if (name.equals(part.getName())) {
+ return part;
+ }
+ }
+ return null;
+ }
+
+ private static URI createCombinedUri(final URI uri, final String path) throws URISyntaxException {
+ if (path == null || path.isEmpty()) {
+ return uri;
+ }
+ String uriString = uri.toString();
+
+ TestUtil.logMsg("Initial uri string: " + uriString);
+
+ final StringBuilder builder = new StringBuilder(uriString);
+ if (builder.charAt(builder.length() - 1) == '/') {
+ if (path.charAt(0) == '/') {
+ builder.append(path.substring(1));
+ } else {
+ builder.append(path);
+ }
+ } else if (path.charAt(0) == '/') {
+ builder.append(path.substring(1));
+ } else {
+ builder.append('/').append(path);
+ }
+
+ String builderString = builder.toString();
+ TestUtil.logMsg("Combined uri string: " + builderString);
+
+ return new URI(builderString);
+ }
+
+ @ApplicationPath("/")
+ public static class MultipartTestApplication extends Application {
+ }
+
+ @Path("/test")
+ public static class TestResource {
+
+ @POST
+ @Consumes(MediaType.MULTIPART_FORM_DATA)
+ @Produces(MediaType.MULTIPART_FORM_DATA)
+ @Path("/basicTest")
+ public Response basicTest(final List parts) throws IOException {
+ final List multipart = List.of(
+ EntityPart.withName("received-string")
+ .content(find(parts,"octet-stream").getContent(byte[].class))
+ .mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE)
+ .build(),
+ EntityPart.withName("received-file")
+ .content(find(parts,"file").getFileName().get(),find(parts,"file").getContent())
+ .mediaType(MediaType.APPLICATION_XML)
+ .build(),
+ EntityPart.withName("added-input-stream")
+ .content(new ByteArrayInputStream("Add part on return.".getBytes()))
+ .mediaType("text/asciidoc")
+ .header("Header1","Test1")
+ .header("Header2","Test2")
+ .build());
+ return Response.ok(new GenericEntity<>(multipart) {
+ }, MediaType.MULTIPART_FORM_DATA).build();
+ }
+
+ @POST
+ @Consumes(MediaType.MULTIPART_FORM_DATA)
+ @Produces(MediaType.MULTIPART_FORM_DATA)
+ @Path("/multi-form-param")
+ public Response multipleFormParamTest(@FormParam("string-part") final String string,
+ @FormParam("entity-part") final EntityPart entityPart,
+ @FormParam("input-stream-part") final InputStream in) throws IOException {
+ final List multipart = List.of(
+ EntityPart.withName("received-entity-part")
+ .content(entityPart.getContent(String.class))
+ .mediaType(entityPart.getMediaType())
+ .fileName(entityPart.getFileName().orElse(null))
+ .build(),
+ EntityPart.withName("received-input-stream")
+ .content(MultipartSupportIT.toString(in).getBytes(StandardCharsets.UTF_8))
+ .mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE)
+ .build(),
+ EntityPart.withName("received-string")
+ .content(string)
+ .mediaType(MediaType.TEXT_PLAIN_TYPE)
+ .build());
+ return Response.ok(new GenericEntity<>(multipart) {
+ }, MediaType.MULTIPART_FORM_DATA).build();
+ }
+
+ }
+
+}
\ No newline at end of file