CommonsUploadMultipartInterceptor implements MultipartInterceptor { public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) throws InterceptionException { logger.info("Request contains multipart data. Try to parse with commons-upload."); FileItemFactory factory = createFactoryForDiskBasedFileItems(config.getDirectory()); indexes = HashMultiset.create(); ServletFileUpload uploader = fileUploadCreator.create(factory); uploader.setSizeMax(config.getSizeLimit()); try { final List<FileItem> items = uploader.parseRequest(request); logger.debug("Found {} attributes in the multipart form submission. Parsing them.", items.size()); final Multimap<String, String> params = LinkedListMultimap.create(); for (FileItem item : items) { String name = item.getFieldName(); name = fixIndexedParameters(name); if (item.isFormField()) { logger.debug("{} is a field", name); params.put(name, getValue(item)); } else if (isNotEmpty(item)) { logger.debug("{} is a file", name); processFile(item, name); } else { logger.debug("A file field was empty: {}", item.getFieldName()); } } for (String paramName : params.keySet()) { Collection<String> paramValues = params.get(paramName); parameters.setParameter(paramName, paramValues.toArray(new String[paramValues.size()])); } } catch (final SizeLimitExceededException e) { reportSizeLimitExceeded(e); } catch (FileUploadException e) { reportFileUploadException(e); } stack.next(method, instance); } CommonsUploadMultipartInterceptor(HttpServletRequest request, MutableRequest parameters, MultipartConfig cfg,
		Validator validator, ServletFileUploadCreator fileUploadCreator); boolean accepts(ResourceMethod method); void intercept(InterceptorStack stack, ResourceMethod method, Object instance);  }
@Test public void withFieldsOnly() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("foo", "blah")); elements.add(new MockFileItem("bar", "blah blah")); when(request.getCharacterEncoding()).thenReturn("utf-8"); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(parameters).setParameter("foo", "blah"); verify(parameters).setParameter("bar", "blah blah"); }
@Test public void withFieldsOnlyWithInvalidCharset() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("foo", "blah")); elements.add(new MockFileItem("bar", "blah blah")); when(request.getCharacterEncoding()).thenReturn("www"); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(parameters).setParameter("foo", "blah"); verify(parameters).setParameter("bar", "blah blah"); }
@Test public void withFilesAndFields() throws Exception { final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("foo", "blah")); elements.add(new MockFileItem("bar", "blah blah")); elements.add(new MockFileItem("thefile0", "foo.txt", "foo".getBytes())); elements.add(new MockFileItem("thefile1", "bar.txt", "bar".getBytes())); interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(parameters).setParameter("foo", "blah"); verify(parameters).setParameter("bar", "blah blah"); verify(parameters).setParameter("thefile0", "thefile0"); verify(parameters).setParameter("thefile1", "thefile1"); verify(request).setAttribute(eq("thefile0"), any(UploadedFile.class)); verify(request).setAttribute(eq("thefile1"), any(UploadedFile.class)); }
@Test public void emptyFiles() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("thefile0", "", new byte[0])); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); }
@Test(expected = InvalidParameterException.class) public void throwsInvalidParameterExceptionIfIOExceptionOccurs() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); MockFileItem item = new MockFileItem("thefile0", "file.txt", new byte[0]); item = spy(item); doThrow(new IOException()).when(item).getInputStream(); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); List<FileItem> items = new ArrayList<FileItem>(); items.add(item); when(mockUpload.parseRequest(request)).thenReturn(items); interceptor.intercept(stack, method, instance); }
@Test public void fieldsWithSameName() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("myfile0", "foo.txt", "foo".getBytes())); elements.add(new MockFileItem("myfile1", "foo.txt", "bar".getBytes())); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(parameters).setParameter("myfile0", "myfile0"); verify(parameters).setParameter("myfile1", "myfile1"); verify(request).setAttribute(eq("myfile0"), any(UploadedFile.class)); verify(request).setAttribute(eq("myfile1"), any(UploadedFile.class)); }
@Test public void multipleUpload() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("myfile0[]", "foo.txt", "foo".getBytes())); elements.add(new MockFileItem("myfile0[]", "foo.txt", "bar".getBytes())); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(parameters).setParameter("myfile0[0]", "myfile0[0]"); verify(parameters).setParameter("myfile0[1]", "myfile0[1]"); verify(request).setAttribute(eq("myfile0[0]"), any(UploadedFile.class)); verify(request).setAttribute(eq("myfile0[1]"), any(UploadedFile.class)); }
@Test public void doNothingWhenFileUploadExceptionOccurs() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); when(mockUpload.parseRequest(request)).thenThrow(new FileUploadException()); interceptor.intercept(stack, method, instance); }
@Test public void shouldValidateWhenSizeLimitExceededExceptionOccurs() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); when(mockUpload.parseRequest(request)).thenThrow(new FileUploadBase.SizeLimitExceededException("", 0L, 0L)); interceptor.intercept(stack, method, instance); verify(validator).add(any(I18nMessage.class)); }
@Test public void shouldCreateDirInsideAppIfTempDirAreNotAvailable() throws Exception { DefaultMultipartConfig configSpy = (DefaultMultipartConfig) spy(config); doThrow(new IOException()).when(configSpy).createTempFile(); interceptor = new CommonsUploadMultipartInterceptor(request, parameters, configSpy, validator, mockCreator); final List<FileItem> elements = new ArrayList<FileItem>(); elements.add(new MockFileItem("myfile", "foo.txt", "bar".getBytes())); when(request.getContentType()).thenReturn("multipart/form-data"); when(request.getMethod()).thenReturn("POST"); when(mockUpload.parseRequest(request)).thenReturn(elements); interceptor.intercept(stack, method, instance); verify(configSpy).createDirInsideApplication(); }
@Test public void handleValidatorMessageWhenFileUploadExceptionOccurs() throws Exception { interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator); when(mockUpload.parseRequest(request)).thenThrow(new FileUploadException()); interceptor.intercept(stack, method, instance); verify(validator).add(any(I18nMessage.class)); }