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));
}
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));
}
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();
}
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());
}
}
Aggregations