use of com.helger.web.fileupload.servlet.ServletFileUpload 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.servlet.ServletFileUpload 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.servlet.ServletFileUpload in project ph-web by phax.
the class AbstractFileUploadTestCase method parseUpload.
protected final ICommonsList<IFileItem> parseUpload(final byte[] bytes, final String contentType) throws FileUploadException {
final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(10240));
final HttpServletRequest request = new MockHttpServletRequest().setContent(bytes).setContentType(contentType);
final ICommonsList<IFileItem> fileItems = upload.parseRequest(request);
return fileItems;
}
use of com.helger.web.fileupload.servlet.ServletFileUpload 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.servlet.ServletFileUpload in project ph-web by phax.
the class RequestMultipartHelper method handleMultipartFormData.
/**
* Parse the provided servlet request as multipart, if the Content-Type starts
* with <code>multipart/form-data</code>.
*
* @param aHttpRequest
* Source HTTP request from which multipart/form-data (aka file
* uploads) should be extracted.
* @param aConsumer
* A consumer that takes either {@link IFileItem} or
* {@link IFileItem}[] or {@link String} or {@link String}[].
* @return {@link EChange#CHANGED} if something was added
*/
@Nonnull
public static EChange handleMultipartFormData(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final BiConsumer<String, Object> aConsumer) {
if (aHttpRequest instanceof MockHttpServletRequest) {
// UnsupportedOperationExceptions
return EChange.UNCHANGED;
}
if (!RequestHelper.isMultipartFormDataContent(aHttpRequest)) {
// It's not a multipart request
return EChange.UNCHANGED;
}
// It is a multipart request!
// Note: this handles only POST parameters!
boolean bAddedFileUploadItems = false;
try {
// Setup the ServletFileUpload....
final ServletFileUpload aUpload = new ServletFileUpload(PROVIDER.getFileItemFactory());
aUpload.setSizeMax(MAX_REQUEST_SIZE);
aUpload.setHeaderEncoding(CWeb.CHARSET_REQUEST_OBJ.name());
final IProgressListener aProgressListener = ProgressListenerProvider.getProgressListener();
if (aProgressListener != null)
aUpload.setProgressListener(aProgressListener);
ServletHelper.setRequestCharacterEncoding(aHttpRequest, CWeb.CHARSET_REQUEST_OBJ);
// Group all items with the same name together
final ICommonsMap<String, ICommonsList<String>> aFormFields = new CommonsHashMap<>();
final ICommonsMap<String, ICommonsList<IFileItem>> aFormFiles = new CommonsHashMap<>();
final ICommonsList<IFileItem> aFileItems = aUpload.parseRequest(aHttpRequest);
for (final IFileItem aFileItem : aFileItems) {
if (aFileItem.isFormField()) {
// We need to explicitly use the charset, as by default only the
// charset from the content type is used!
aFormFields.computeIfAbsent(aFileItem.getFieldName(), k -> new CommonsArrayList<>()).add(aFileItem.getString(CWeb.CHARSET_REQUEST_OBJ));
} else
aFormFiles.computeIfAbsent(aFileItem.getFieldName(), k -> new CommonsArrayList<>()).add(aFileItem);
}
// set all form fields
for (final Map.Entry<String, ICommonsList<String>> aEntry : aFormFields.entrySet()) {
// Convert list of String to value (String or String[])
final ICommonsList<String> aValues = aEntry.getValue();
final Object aValue = aValues.size() == 1 ? aValues.getFirst() : ArrayHelper.newArray(aValues, String.class);
aConsumer.accept(aEntry.getKey(), aValue);
}
// name)
for (final Map.Entry<String, ICommonsList<IFileItem>> aEntry : aFormFiles.entrySet()) {
// Convert list of String to value (IFileItem or IFileItem[])
final ICommonsList<IFileItem> aValues = aEntry.getValue();
final Object aValue = aValues.size() == 1 ? aValues.getFirst() : ArrayHelper.newArray(aValues, IFileItem.class);
aConsumer.accept(aEntry.getKey(), aValue);
}
// Parsing complex file upload succeeded -> do not use standard scan for
// parameters
bAddedFileUploadItems = true;
} catch (final FileUploadException ex) {
if (!StreamHelper.isKnownEOFException(ex.getCause()))
LOGGER.error("Error parsing multipart request content", ex);
} catch (final RuntimeException ex) {
LOGGER.error("Error parsing multipart request content", ex);
}
return EChange.valueOf(bAddedFileUploadItems);
}
Aggregations