MultipartDecoderImpl implements MultipartDecoder, ThreadCleanupListener { protected HttpServletRequest processFileItems(HttpServletRequest request, List<FileItem> fileItems) { if (uploadException == null && fileItems.isEmpty()) { return request; } ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(request); for (Map.Entry<String, String[]> e : request.getParameterMap().entrySet()) { for (String s : e.getValue()) wrapper.addParameter(e.getKey(), s); } for (FileItem item : fileItems) { if (item.isFormField()) { String fieldValue; try { fieldValue = item.getString(requestEncoding); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } wrapper.addParameter(item.getFieldName(), fieldValue); } else { wrapper.addParameter(item.getFieldName(), item.getName()); addUploadedFile(item.getFieldName(), new UploadedFileItem(item)); } } return wrapper; } MultipartDecoderImpl(

            FileItemFactory fileItemFactory,

            @Symbol(UploadSymbols.REQUESTSIZE_MAX)
            long maxRequestSize,

            @Symbol(UploadSymbols.FILESIZE_MAX)
            long maxFileSize,

            @Symbol(SymbolConstants.CHARSET)
            String requestEncoding); @Override UploadedFile getFileUpload(String parameterName); @Override HttpServletRequest decode(HttpServletRequest request); @Override void threadDidCleanup(); @Override FileUploadException getUploadException();  }
@Test public void process_file_items_does_nothing_when_null_file_items() throws Exception { HttpServletRequest request = mockHttpServletRequest(); MultipartDecoderImpl decoder = new MultipartDecoderImpl(fileItemFactory, -1, -1, CHARSET); List<FileItem> items = Collections.emptyList(); replay(); HttpServletRequest decodedRequest = decoder.processFileItems(request, items); verify(); assertSame(request, decodedRequest); }
@Test public void process_file_items_does_nothing_when_empty_file_items() throws Exception { HttpServletRequest request = mockHttpServletRequest(); MultipartDecoderImpl decoder = new MultipartDecoderImpl(fileItemFactory, -1, -1, CHARSET); List<FileItem> fileItems = Collections.emptyList(); replay(); HttpServletRequest decodedRequest = decoder.processFileItems(request, fileItems); verify(); assertSame(request, decodedRequest); }
@Test public void process_file_items_creates_wrapped_request_and_sets_non_file_parameters() throws Exception { HttpServletRequest request = mockHttpServletRequest(); expect(request.getParameterMap()).andReturn(Collections.EMPTY_MAP); MultipartDecoderImpl decoder = new MultipartDecoderImpl(fileItemFactory, -1, -1, CHARSET); List<FileItem> fileItems = Arrays.asList(createValueItem("one", "first"), createValueItem("two", "second")); replay(); HttpServletRequest decodedRequest = decoder.processFileItems(request, fileItems); assertNotSame(decodedRequest, request); assertEquals(decodedRequest.getParameter("one"), "first"); assertEquals(decodedRequest.getParameter("two"), "second"); verify(); }
@Test public void non_file_items_with_null_request_encoding() throws Exception { HttpServletRequest request = mockHttpServletRequest(); expect(request.getParameterMap()).andReturn(Collections.EMPTY_MAP); MultipartDecoderImpl decoder = new MultipartDecoderImpl(fileItemFactory, -1, -1, CHARSET); List<FileItem> fileItems = Arrays.asList(createValueItem("one", "first"), createValueItem("two", "second")); replay(); HttpServletRequest decodedRequest = decoder.processFileItems(request, fileItems); assertNotSame(decodedRequest, request); assertEquals(decodedRequest.getParameter("one"), "first"); assertEquals(decodedRequest.getParameter("two"), "second"); verify(); }
@Test public void process_file_items_set_file_parameters_with_file_name() throws Exception { HttpServletRequest request = mockHttpServletRequest(); expect(request.getParameterMap()).andReturn(Collections.EMPTY_MAP); MultipartDecoderImpl decoder = new MultipartDecoderImpl(fileItemFactory, -1, -1, CHARSET); List<FileItem> fileItems = Arrays.asList(createFileItem("one", "first.txt"), createFileItem("two", "second.txt")); replay(); HttpServletRequest decodedRequest = decoder.processFileItems(request, fileItems); assertNotSame(decodedRequest, request); assertEquals(decodedRequest.getParameter("one"), "first.txt"); assertEquals(decodedRequest.getParameter("two"), "second.txt"); verify(); }