Search in sources :

Example 1 with FileUploadBase

use of org.apache.commons.fileupload.FileUploadBase in project wicket by apache.

the class MultipartServletWebRequestImpl method parseFileParts.

@Override
public void parseFileParts() throws FileUploadException {
    HttpServletRequest request = getContainerRequest();
    // The encoding that will be used to decode the string parameters
    // It should NOT be null at this point, but it may be
    // especially if the older Servlet API 2.2 is used
    String encoding = request.getCharacterEncoding();
    // the attribute 'accept-encoding' in wicket forms.
    if (encoding == null) {
        encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
    }
    FileUploadBase fileUpload = newFileUpload(encoding);
    List<FileItem> items;
    if (wantUploadProgressUpdates()) {
        ServletRequestContext ctx = new ServletRequestContext(request) {

            @Override
            public InputStream getInputStream() throws IOException {
                return new CountingInputStream(super.getInputStream());
            }
        };
        totalBytes = request.getContentLength();
        onUploadStarted(totalBytes);
        try {
            items = fileUpload.parseRequest(ctx);
        } finally {
            onUploadCompleted();
        }
    } else {
        // try to parse the file uploads by using Apache Commons FileUpload APIs
        // because they are feature richer (e.g. progress updates, cleaner)
        items = fileUpload.parseRequest(new ServletRequestContext(request));
        if (items.isEmpty()) {
            // fallback to Servlet 3.0 APIs
            items = readServlet3Parts(request);
        }
    }
    // Loop through items
    for (final FileItem item : items) {
        // If item is a form field
        if (item.isFormField()) {
            // Set parameter value
            final String value;
            if (encoding != null) {
                try {
                    value = item.getString(encoding);
                } catch (UnsupportedEncodingException e) {
                    throw new WicketRuntimeException(e);
                }
            } else {
                value = item.getString();
            }
            addParameter(item.getFieldName(), value);
        } else {
            List<FileItem> fileItems = files.get(item.getFieldName());
            if (fileItems == null) {
                fileItems = new ArrayList<>();
                files.put(item.getFieldName(), fileItems);
            }
            // Add to file list
            fileItems.add(item);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FileItem(org.apache.commons.fileupload.FileItem) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadBase (org.apache.commons.fileupload.FileUploadBase)1 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1