Search in sources :

Example 1 with UploadListener

use of io.milton.servlet.upload.UploadListener in project lobcder by skoulouzis.

the class ServletRequest method parseRequestParameters.

@Override
public void parseRequestParameters(Map<String, String> params, Map<String, io.milton.http.FileItem> files) throws RequestParseException {
    try {
        if (isMultiPart()) {
            log.trace("parseRequestParameters: isMultiPart");
            UploadListener listener = new UploadListener();
            MonitoredDiskFileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(request);
            parseQueryString(params);
            for (Object o : items) {
                FileItem item = (FileItem) o;
                if (item.isFormField()) {
                    params.put(item.getFieldName(), item.getString());
                } else {
                    // See http://jira.ettrema.com:8080/browse/MIL-118 - ServletRequest#parseRequestParameters overwrites multiple file uploads when using input type="file" multiple="multiple"
                    String itemKey = item.getFieldName();
                    if (files.containsKey(itemKey)) {
                        int count = 1;
                        while (files.containsKey(itemKey + count)) {
                            count++;
                        }
                        itemKey = itemKey + count;
                    }
                    files.put(itemKey, new FileItemWrapper(item));
                }
            }
        } else {
            for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
                String nm = (String) en.nextElement();
                String[] vals = request.getParameterValues(nm);
                if (vals.length == 1) {
                    params.put(nm, vals[0]);
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (String s : vals) {
                        sb.append(s).append(",");
                    }
                    if (sb.length() > 0) {
                        // remove last comma
                        sb.deleteCharAt(sb.length() - 1);
                    }
                    params.put(nm, sb.toString());
                }
            }
        }
    } catch (FileUploadException ex) {
        throw new RequestParseException("FileUploadException", ex);
    } catch (Throwable ex) {
        throw new RequestParseException(ex.getMessage(), ex);
    }
}
Also used : Enumeration(java.util.Enumeration) UploadListener(io.milton.servlet.upload.UploadListener) FileItem(org.apache.commons.fileupload.FileItem) RequestParseException(io.milton.http.RequestParseException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ArrayList(java.util.ArrayList) List(java.util.List) MonitoredDiskFileItemFactory(io.milton.servlet.upload.MonitoredDiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

RequestParseException (io.milton.http.RequestParseException)1 MonitoredDiskFileItemFactory (io.milton.servlet.upload.MonitoredDiskFileItemFactory)1 UploadListener (io.milton.servlet.upload.UploadListener)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1