Search in sources :

Example 1 with MultipartServletWebRequest

use of org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest in project wicket by apache.

the class Form method handleMultiPart.

/**
 * Handles multi-part processing of the submitted data. <h3>
 * WARNING</h3> If this method is overridden it can break {@link FileUploadField}s on this form
 *
 * @return false if form is multipart and upload failed
 */
protected boolean handleMultiPart() {
    if (isMultiPart()) {
        // parsed out correctly
        try {
            ServletWebRequest request = (ServletWebRequest) getRequest();
            final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(getMaxSize(), getPage().getId());
            multipartWebRequest.setFileMaxSize(getFileMaxSize());
            multipartWebRequest.parseFileParts();
            // TODO: Can't this be detected from header?
            getRequestCycle().setRequest(multipartWebRequest);
        } catch (final FileUploadException fux) {
            // Create model with exception and maximum size values
            final Map<String, Object> model = new HashMap<>();
            model.put("exception", fux);
            model.put("maxSize", getMaxSize());
            model.put("fileMaxSize", getFileMaxSize());
            onFileUploadException(fux, model);
            // don't process the form if there is a FileUploadException
            return false;
        }
    }
    return true;
}
Also used : MultipartServletWebRequest(org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest) MultipartServletWebRequest(org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) HashMap(java.util.HashMap) Map(java.util.Map) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with MultipartServletWebRequest

use of org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest in project wicket by apache.

the class AjaxFileDropBehavior method onEvent.

@Override
protected void onEvent(AjaxRequestTarget target) {
    try {
        ServletWebRequest request = (ServletWebRequest) getComponent().getRequest();
        final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId());
        multipartWebRequest.setFileMaxSize(getFileMaxSize());
        multipartWebRequest.parseFileParts();
        // TODO: Can't this be detected from header?
        getComponent().getRequestCycle().setRequest(multipartWebRequest);
        ArrayList<FileUpload> fileUploads = new ArrayList<>();
        // Get the item for the path
        final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
        if (fileItems != null) {
            for (FileItem item : fileItems) {
                fileUploads.add(new FileUpload(item));
            }
        }
        onFileUpload(target, fileUploads);
    } catch (final FileUploadException fux) {
        onError(target, fux);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MultipartServletWebRequest(org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest) ArrayList(java.util.ArrayList) MultipartServletWebRequest(org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

FileUploadException (org.apache.commons.fileupload.FileUploadException)2 MultipartServletWebRequest (org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest)2 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)1