Search in sources :

Example 1 with DiskFileItemFactory

use of com.helger.web.fileupload.parse.DiskFileItemFactory in project ph-web by phax.

the class StreamingFuncTest method _parseUploadToList.

@Nonnull
@ReturnsMutableCopy
private ICommonsList<IFileItem> _parseUploadToList(final InputStream pStream, final int pLength) throws FileUploadException {
    final String contentType = "multipart/form-data; boundary=---1234";
    final AbstractFileUploadBase upload = new ServletFileUpload(new DiskFileItemFactory(10240));
    final MockHttpServletRequest request = new MockHttpServletRequest() {

        @Override
        public int getContentLength() {
            return pLength;
        }

        @Override
        public ServletInputStream getInputStream() {
            return new AbstractServletInputStream() {

                @Override
                public int read() throws IOException {
                    return pStream.read();
                }
            };
        }
    };
    request.setContentType(contentType);
    return upload.parseRequest(new ServletRequestContext(request));
}
Also used : ServletFileUpload(com.helger.web.fileupload.servlet.ServletFileUpload) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) ServletRequestContext(com.helger.web.fileupload.servlet.ServletRequestContext) AbstractServletInputStream(com.helger.servlet.io.AbstractServletInputStream) DiskFileItemFactory(com.helger.web.fileupload.parse.DiskFileItemFactory) AbstractFileUploadBase(com.helger.web.fileupload.parse.AbstractFileUploadBase) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 2 with DiskFileItemFactory

use of com.helger.web.fileupload.parse.DiskFileItemFactory in project ph-web by phax.

the class StreamingFuncTest method _parseUploadToIterator.

private IFileItemIterator _parseUploadToIterator(final byte[] aContent) throws FileUploadException, IOException {
    final String contentType = "multipart/form-data; boundary=---1234";
    final AbstractFileUploadBase upload = new ServletFileUpload(new DiskFileItemFactory(10240));
    final HttpServletRequest request = new MockHttpServletRequest().setContent(aContent).setContentType(contentType);
    return upload.getItemIterator(new ServletRequestContext(request));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) ServletFileUpload(com.helger.web.fileupload.servlet.ServletFileUpload) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) ServletRequestContext(com.helger.web.fileupload.servlet.ServletRequestContext) DiskFileItemFactory(com.helger.web.fileupload.parse.DiskFileItemFactory) AbstractFileUploadBase(com.helger.web.fileupload.parse.AbstractFileUploadBase)

Example 3 with DiskFileItemFactory

use of com.helger.web.fileupload.parse.DiskFileItemFactory in project ph-web by phax.

the class IProgressListenerTest method _runTest.

private void _runTest(final int NUM_ITEMS, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
    final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(CGlobal.BYTES_PER_MEGABYTE, null));
    final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
    upload.setProgressListener(listener);
    final IFileItemIterator iter = upload.getItemIterator(request);
    for (int i = 0; i < NUM_ITEMS; i++) {
        final IFileItemStream stream = iter.next();
        final InputStream istream = stream.openStream();
        for (int j = 0; j < 16384 + i; j++) {
            /**
             * This used to be assertEquals((byte) j, (byte) istream.read()); but
             * this seems to trigger a bug in JRockit, so we express the same like
             * this:
             */
            final byte b1 = (byte) j;
            final byte b2 = (byte) istream.read();
            if (b1 != b2) {
                fail("Expected " + b1 + ", got " + b2);
            }
        }
        assertEquals(-1, istream.read());
    }
    assertFalse(iter.hasNext());
    listener._checkFinished();
}
Also used : ServletFileUpload(com.helger.web.fileupload.servlet.ServletFileUpload) IFileItemStream(com.helger.web.fileupload.IFileItemStream) IFileItemIterator(com.helger.web.fileupload.IFileItemIterator) InputStream(java.io.InputStream) DiskFileItemFactory(com.helger.web.fileupload.parse.DiskFileItemFactory)

Example 4 with DiskFileItemFactory

use of com.helger.web.fileupload.parse.DiskFileItemFactory in project ph-web by phax.

the class SizesFuncTest method testFileSizeLimit.

/**
 * Checks, whether limiting the file size works.
 *
 * @throws FileUploadException
 *         In case of error
 */
@Test
public void testFileSizeLimit() throws FileUploadException {
    final String request = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234--\r\n";
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(10240));
    upload.setFileSizeMax(-1);
    HttpServletRequest req = new MockHttpServletRequest().setContent(request.getBytes(StandardCharsets.US_ASCII)).setContentType(CONTENT_TYPE);
    List<IFileItem> fileItems = upload.parseRequest(req);
    assertEquals(1, fileItems.size());
    IFileItem item = fileItems.get(0);
    assertEquals("This is the content of the file\n", new String(item.directGet(), StandardCharsets.US_ASCII));
    upload = new ServletFileUpload(new DiskFileItemFactory(10240));
    upload.setFileSizeMax(40);
    req = new MockHttpServletRequest().setContent(request.getBytes(StandardCharsets.US_ASCII)).setContentType(CONTENT_TYPE);
    fileItems = upload.parseRequest(req);
    assertEquals(1, fileItems.size());
    item = fileItems.get(0);
    assertEquals("This is the content of the file\n", new String(item.directGet(), StandardCharsets.US_ASCII));
    upload = new ServletFileUpload(new DiskFileItemFactory(10240));
    upload.setFileSizeMax(30);
    req = new MockHttpServletRequest().setContent(request.getBytes(StandardCharsets.US_ASCII)).setContentType(CONTENT_TYPE);
    try {
        upload.parseRequest(req);
        fail("Expected exception.");
    } catch (final FileSizeLimitExceededException e) {
        assertEquals(30, e.getPermittedSize());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) FileSizeLimitExceededException(com.helger.web.fileupload.exception.FileSizeLimitExceededException) IFileItem(com.helger.web.fileupload.IFileItem) DiskFileItemFactory(com.helger.web.fileupload.parse.DiskFileItemFactory) Test(org.junit.Test)

Aggregations

DiskFileItemFactory (com.helger.web.fileupload.parse.DiskFileItemFactory)4 MockHttpServletRequest (com.helger.servlet.mock.MockHttpServletRequest)3 ServletFileUpload (com.helger.web.fileupload.servlet.ServletFileUpload)3 AbstractFileUploadBase (com.helger.web.fileupload.parse.AbstractFileUploadBase)2 ServletRequestContext (com.helger.web.fileupload.servlet.ServletRequestContext)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 AbstractServletInputStream (com.helger.servlet.io.AbstractServletInputStream)1 IFileItem (com.helger.web.fileupload.IFileItem)1 IFileItemIterator (com.helger.web.fileupload.IFileItemIterator)1 IFileItemStream (com.helger.web.fileupload.IFileItemStream)1 FileSizeLimitExceededException (com.helger.web.fileupload.exception.FileSizeLimitExceededException)1 InputStream (java.io.InputStream)1 Nonnull (javax.annotation.Nonnull)1 Test (org.junit.Test)1