MapMultipartFormDataMessageBodyReader implements MessageBodyReader<Map<String, InputItem>> { @Override public Map<String, InputItem> readFrom(Class<Map<String, InputItem>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { final Type genericSuperclass = ParameterizedTypeImpl.newParameterizedType(Iterator.class, FileItem.class); final MessageBodyReader<Iterator> multipartReader = providers.getMessageBodyReader(Iterator.class, genericSuperclass, annotations, mediaType); final Iterator iterator = multipartReader.readFrom(Iterator.class, genericSuperclass, annotations, mediaType, httpHeaders, entityStream); final Map<String, InputItem> result = new LinkedHashMap<>(); while (iterator.hasNext()) { final DefaultInputItem item = new DefaultInputItem((FileItem)iterator.next(), providers); result.put(item.getName(), item); } return result; } MapMultipartFormDataMessageBodyReader(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override Map<String, InputItem> readFrom(Class<Map<String, InputItem>> type, Type genericType, Annotation[] annotations,
                                           MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream);  }
@Test public void readsMapOfInputItems() throws Exception { Class type = Map.class; ParameterizedType genericType = newParameterizedType(Map.class, String.class, InputItem.class); Annotation[] annotations = new Annotation[0]; MediaType mediaType = MULTIPART_FORM_DATA_TYPE; MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); InputStream in = mock(InputStream.class); FileItem fileItem = createFileItem("fileItem1"); MessageBodyReader fileItemReader = createFileItemMessageBodyReader(annotations, mediaType, headers, in, fileItem); when(providers.getMessageBodyReader(eq(Iterator.class), eq(newParameterizedType(Iterator.class, FileItem.class)), aryEq(annotations), eq(mediaType))) .thenReturn(fileItemReader); Map<String, InputItem> inputItems = mapMultipartFormDataMessageBodyReader.readFrom(type, genericType, annotations, mediaType, headers, in); assertEquals(1, inputItems.size()); assertEquals(fileItem.getFieldName(), inputItems.get(fileItem.getFieldName()).getName()); }