@Test public void writesCollectionOfOutputItems() throws Exception { OutputItem item1 = anOutputItem().withName("item1").withEntity("item1 entity").withMediaType(TEXT_PLAIN_TYPE).withFilename("item1.txt").build(); OutputItem item2 = anOutputItem().withName("item2").withEntity("{\"item2\":\"entity\"}").withMediaType(APPLICATION_JSON_TYPE).withFilename("item2.json").build(); Collection<OutputItem> items = newArrayList(item1, item2); Class<Collection> type = Collection.class; ParameterizedType genericType = ParameterizedTypeImpl.newParameterizedType(Collection.class, OutputItem.class); Annotation[] annotations = new Annotation[0]; MediaType mediaType = new MediaType("multipart", "form-data", ImmutableMap.of("boundary", "1234567")); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); OutputStream out = new ByteArrayOutputStream(); collectionMultipartFormDataMessageBodyWriter.writeTo(items, type, genericType, annotations, mediaType, headers, out); verify(multipartFormDataWriter).writeItems(same(items), same(out), aryEq("1234567".getBytes())); MediaType mediaTypeWithBoundary = (MediaType)headers.getFirst("Content-type"); assertNotNull(mediaTypeWithBoundary); String boundary = mediaTypeWithBoundary.getParameters().get("boundary"); assertEquals("1234567", boundary); }
@Override public void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String boundary = mediaType.getParameters().get("boundary"); if (boundary == null) { boundary = Long.toString(System.currentTimeMillis()); } httpHeaders.putSingle("Content-type", createMediaTypeWithBoundary(mediaType, boundary)); final byte[] boundaryBytes = boundary.getBytes(); multipartFormDataWriter.writeItems(items, entityStream, boundaryBytes); }
CollectionMultipartFormDataMessageBodyWriter implements MessageBodyWriter<Collection<OutputItem>> { @Override public void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String boundary = mediaType.getParameters().get("boundary"); if (boundary == null) { boundary = Long.toString(System.currentTimeMillis()); } httpHeaders.putSingle("Content-type", createMediaTypeWithBoundary(mediaType, boundary)); final byte[] boundaryBytes = boundary.getBytes(); multipartFormDataWriter.writeItems(items, entityStream, boundaryBytes); } }
CollectionMultipartFormDataMessageBodyWriter implements MessageBodyWriter<Collection<OutputItem>> { @Override public void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String boundary = mediaType.getParameters().get("boundary"); if (boundary == null) { boundary = Long.toString(System.currentTimeMillis()); } httpHeaders.putSingle("Content-type", createMediaTypeWithBoundary(mediaType, boundary)); final byte[] boundaryBytes = boundary.getBytes(); multipartFormDataWriter.writeItems(items, entityStream, boundaryBytes); } CollectionMultipartFormDataMessageBodyWriter(@Context Providers providers);   CollectionMultipartFormDataMessageBodyWriter(MultipartFormDataWriter multipartFormDataWriter); }
CollectionMultipartFormDataMessageBodyWriter implements MessageBodyWriter<Collection<OutputItem>> { @Override public void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String boundary = mediaType.getParameters().get("boundary"); if (boundary == null) { boundary = Long.toString(System.currentTimeMillis()); } httpHeaders.putSingle("Content-type", createMediaTypeWithBoundary(mediaType, boundary)); final byte[] boundaryBytes = boundary.getBytes(); multipartFormDataWriter.writeItems(items, entityStream, boundaryBytes); } CollectionMultipartFormDataMessageBodyWriter(@Context Providers providers);   CollectionMultipartFormDataMessageBodyWriter(MultipartFormDataWriter multipartFormDataWriter); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream); }
CollectionMultipartFormDataMessageBodyWriter implements MessageBodyWriter<Collection<OutputItem>> { @Override public void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String boundary = mediaType.getParameters().get("boundary"); if (boundary == null) { boundary = Long.toString(System.currentTimeMillis()); } httpHeaders.putSingle("Content-type", createMediaTypeWithBoundary(mediaType, boundary)); final byte[] boundaryBytes = boundary.getBytes(); multipartFormDataWriter.writeItems(items, entityStream, boundaryBytes); } CollectionMultipartFormDataMessageBodyWriter(@Context Providers providers);   CollectionMultipartFormDataMessageBodyWriter(MultipartFormDataWriter multipartFormDataWriter); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(Collection<OutputItem> items, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream);  }