Example usage for org.apache.commons.fileupload FileItemIterator FileItemIterator

List of usage examples for org.apache.commons.fileupload FileItemIterator FileItemIterator

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItemIterator FileItemIterator.

Prototype

FileItemIterator

Source Link

Usage

From source file:com.threewks.thundr.bind.http.MultipartHttpBinderTest.java

@Before
public void before() throws FileUploadException, IOException {
    parameterBinderRegistry = new ParameterBinderRegistry(TransformerManager.createWithDefaults());
    ParameterBinderRegistry.addDefaultBinders(parameterBinderRegistry);
    binder = new MultipartHttpBinder(parameterBinderRegistry);
    parameterDescriptions = new LinkedHashMap<ParameterDescription, Object>();
    pathVariables = new HashMap<String, String>();

    multipartData = new ArrayList<FileItemStream>();
    ServletFileUpload mockUpload = mock(ServletFileUpload.class);
    when(mockUpload.getItemIterator(request)).thenAnswer(new Answer<FileItemIterator>() {

        @Override//from   w w w  .  jav a 2  s .c om
        public FileItemIterator answer(InvocationOnMock invocation) throws Throwable {
            return new FileItemIterator() {
                Iterator<FileItemStream> iterator = multipartData.iterator();

                @Override
                public FileItemStream next() throws FileUploadException, IOException {
                    return iterator.next();
                }

                @Override
                public boolean hasNext() throws FileUploadException, IOException {
                    return iterator.hasNext();
                }
            };
        }
    });
    TestSupport.setField(binder, "upload", mockUpload);
}

From source file:com.boundlessgeo.geoserver.api.controllers.ApiController.java

protected FileItemIterator doFileUpload(final HttpServletRequest request)
        throws FileUploadException, IOException {
    final ServletFileUpload upload = newFileUpload();
    //Delegate FileItemIterator to only return files
    return new FileItemIterator() {
        FileItemIterator delegate = upload.getItemIterator(request);
        FileItemStream next = null;//from  w  w w.  j  a  v  a  2 s . c o  m

        @Override
        public boolean hasNext() throws FileUploadException, IOException {
            if (next != null) {
                return true;
            }
            while (delegate.hasNext()) {
                FileItemStream item = delegate.next();
                if (!item.isFormField()) {
                    next = item;
                    break;
                }
            }
            return next != null;
        }

        @Override
        public FileItemStream next() throws FileUploadException, IOException {
            if (hasNext()) {
                FileItemStream current = next;
                next = null;
                return current;
            }
            throw new NoSuchElementException();
        }
    };
}