[ https://issues.apache.org/jira/browse/WW-5388?focusedWorklogId=902097&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-902097 ]
ASF GitHub Bot logged work on WW-5388: -------------------------------------- Author: ASF GitHub Bot Created on: 27/Jan/24 18:27 Start Date: 27/Jan/24 18:27 Worklog Time Spent: 10m Work Description: burtonrhodes commented on code in PR #861: URL: https://github.com/apache/struts/pull/861#discussion_r1468546626 ########## core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java: ########## @@ -21,375 +21,116 @@ import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.fileupload2.core.DiskFileItem; import org.apache.commons.fileupload2.core.DiskFileItemFactory; -import org.apache.commons.fileupload2.core.FileItem; -import org.apache.commons.fileupload2.core.FileUploadByteCountLimitException; -import org.apache.commons.fileupload2.core.FileUploadContentTypeException; -import org.apache.commons.fileupload2.core.FileUploadException; -import org.apache.commons.fileupload2.core.FileUploadFileCountLimitException; -import org.apache.commons.fileupload2.core.FileUploadSizeException; -import org.apache.commons.fileupload2.core.RequestContext; -import org.apache.commons.fileupload2.jakarta.JakartaServletFileUpload; +import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.struts2.dispatcher.LocalizedMessage; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Set; /** - * Multipart form data request adapter for Jakarta Commons Fileupload package. + * Multipart form data request adapter for Jakarta Commons FileUpload package. */ -public class JakartaMultiPartRequest extends AbstractMultiPartRequest { +public class JakartaMultiPartRequest extends AbstractMultiPartRequest<File> { - static final Logger LOG = LogManager.getLogger(JakartaMultiPartRequest.class); + private static final Logger LOG = LogManager.getLogger(JakartaMultiPartRequest.class); - // maps parameter name -> List of FileItem objects - protected Map<String, List<FileItem>> files = new HashMap<>(); + @Override + protected void processUpload(HttpServletRequest request, String saveDir) throws IOException { + String charset = StringUtils.isBlank(request.getCharacterEncoding()) + ? defaultEncoding + : request.getCharacterEncoding(); - // maps parameter name -> List of param values - protected Map<String, List<String>> params = new HashMap<>(); + JakartaServletDiskFileUpload servletFileUpload = + prepareServletFileUpload(Charset.forName(charset), Path.of(saveDir)); - /** - * Creates a new request wrapper to handle multipart data using methods adapted from Jason Pell's - * multipart classes (see class description). - * - * @param saveDir the directory to save off the file - * @param request the request containing the multipart - * @throws java.io.IOException is thrown if encoding fails. - */ - public void parse(HttpServletRequest request, String saveDir) throws IOException { - try { - setLocale(request); - processUpload(request, saveDir); - } catch (FileUploadException e) { - LOG.debug("Request exceeded size limit!", e); - LocalizedMessage errorMessage; - if (e instanceof FileUploadByteCountLimitException) { - FileUploadByteCountLimitException ex = (FileUploadByteCountLimitException) e; - errorMessage = buildErrorMessage(e, new Object[]{ - ex.getFieldName(), ex.getFileName(), ex.getPermitted(), ex.getActualSize() - }); - } else if (e instanceof FileUploadFileCountLimitException) { - FileUploadFileCountLimitException ex = (FileUploadFileCountLimitException) e; - errorMessage = buildErrorMessage(e, new Object[]{ - ex.getPermitted(), ex.getActualSize() - }); - } else if (e instanceof FileUploadSizeException) { - FileUploadSizeException ex = (FileUploadSizeException) e; - errorMessage = buildErrorMessage(e, new Object[]{ - ex.getPermitted(), ex.getActualSize() - }); - } else if (e instanceof FileUploadContentTypeException) { - FileUploadContentTypeException ex = (FileUploadContentTypeException) e; - errorMessage = buildErrorMessage(e, new Object[]{ - ex.getContentType() - }); + for (DiskFileItem item : servletFileUpload.parseRequest(request)) { Review Comment: @lukaszlenart Quick thought on the maxFiles setting: I still think it makes more sense that maxFiles would apply to only file fields and not all form fields in the request. However it appears that this logic is actually baked into the JakartaServletFileUpload class itself when calling `servletFileUpload.parseRequest()`. Wonder why they coded it this way?? Anyway, if we ever wanted to change this, you could set the maxFiles to unlimited in the servletFileUpload object, and then perform the files count check in the `if (item.isFormField()...` logic below. Just a thought. Issue Time Tracking ------------------- Worklog Id: (was: 902097) Time Spent: 3h 40m (was: 3.5h) > Upgrade Commons Fileupload to FileUpload Jakarta Servlet 6 > ---------------------------------------------------------- > > Key: WW-5388 > URL: https://issues.apache.org/jira/browse/WW-5388 > Project: Struts 2 > Issue Type: Improvement > Components: Core > Reporter: Lukasz Lenart > Assignee: Lukasz Lenart > Priority: Major > Fix For: 7.0.0 > > Time Spent: 3h 40m > Remaining Estimate: 0h > > There is a new version of JakartaEE FileUpload > {code:xml} > <dependency> > <groupId>org.apache.commons</groupId> > <artifactId>commons-fileupload2-jakarta-servlet6</artifactId> > <version>2.0.0-M2</version> > </dependency> > {code} -- This message was sent by Atlassian Jira (v8.20.10#820010)