You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2023/02/28 07:10:38 UTC
[struts] branch WW-5285-max-files updated (253cd9f53 -> 13c93aaa7)
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a change to branch WW-5285-max-files
in repository https://gitbox.apache.org/repos/asf/struts.git
discard 253cd9f53 WW-5285 Uses Long and null to check if option has been defined
new 13c93aaa7 WW-5285 Uses Long and null to check if option has been defined
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (253cd9f53)
\
N -- N -- N refs/heads/WW-5285-max-files (13c93aaa7)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[struts] 01/01: WW-5285 Uses Long and null to check if option has been defined
Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch WW-5285-max-files
in repository https://gitbox.apache.org/repos/asf/struts.git
commit 13c93aaa75bea84ee8f597af02975bbcc655cd03
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Tue Feb 28 07:26:05 2023 +0100
WW-5285 Uses Long and null to check if option has been defined
---
.../multipart/AbstractMultiPartRequest.java | 12 +++----
.../multipart/JakartaMultiPartRequest.java | 8 +++--
.../multipart/JakartaStreamMultiPartRequest.java | 37 +++++++++++-----------
.../dispatcher/multipart/PellMultiPartRequest.java | 6 ++--
4 files changed, 32 insertions(+), 31 deletions(-)
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java
index b3410e578..e46cecc00 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java
@@ -51,14 +51,12 @@ public abstract class AbstractMultiPartRequest implements MultiPartRequest {
/**
* Specifies the maximum size of the entire request.
*/
- protected long maxSize;
- protected boolean maxSizeProvided;
+ protected Long maxSize;
/**
* Specifies the maximum number of files in one request.
*/
- protected long maxFiles;
- protected boolean maxFilesProvided;
+ protected Long maxFiles;
/**
* Specifies the buffer size to use during streaming.
@@ -90,13 +88,11 @@ public abstract class AbstractMultiPartRequest implements MultiPartRequest {
*/
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
public void setMaxSize(String maxSize) {
- this.maxSizeProvided = true;
this.maxSize = Long.parseLong(maxSize);
}
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXFILES)
public void setMaxFiles(String maxFiles) {
- this.maxFilesProvided = true;
this.maxFiles = Long.parseLong(maxFiles);
}
@@ -146,9 +142,9 @@ public abstract class AbstractMultiPartRequest implements MultiPartRequest {
int forwardSlash = fileName.lastIndexOf('/');
int backwardSlash = fileName.lastIndexOf('\\');
if (forwardSlash != -1 && forwardSlash > backwardSlash) {
- fileName = fileName.substring(forwardSlash + 1, fileName.length());
+ fileName = fileName.substring(forwardSlash + 1);
} else {
- fileName = fileName.substring(backwardSlash + 1, fileName.length());
+ fileName = fileName.substring(backwardSlash + 1);
}
return fileName;
}
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
index c20b1de19..00d922401 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
@@ -160,8 +160,12 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
protected ServletFileUpload createServletFileUpload(DiskFileItemFactory fac) {
ServletFileUpload upload = new ServletFileUpload(fac);
- upload.setSizeMax(maxSize);
- upload.setFileCountMax(maxFiles);
+ if (maxSize != null) {
+ upload.setSizeMax(maxSize);
+ }
+ if (maxFiles != null) {
+ upload.setFileCountMax(maxFiles);
+ }
return upload;
}
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
index f709b3416..c7311ab0a 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
@@ -30,15 +30,15 @@ import org.apache.struts2.dispatcher.LocalizedMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
+import java.nio.file.Files;
import java.util.*;
/**
* Multi-part form data request adapter for Jakarta Commons FileUpload package that
* leverages the streaming API rather than the traditional non-streaming API.
- *
+ * <p>
* For more details see WW-3025
*
- * @author Chris Cranford
* @since 2.3.18
*/
public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
@@ -85,7 +85,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
types.add(fileInfo.getContentType());
}
- return types.toArray(new String[types.size()]);
+ return types.toArray(new String[0]);
}
/* (non-Javadoc)
@@ -102,7 +102,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
files.add(new StrutsUploadedFile(fileInfo.getFile()));
}
- return files.toArray(new UploadedFile[files.size()]);
+ return files.toArray(new UploadedFile[0]);
}
/* (non-Javadoc)
@@ -119,7 +119,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
names.add(getCanonicalName(fileInfo.getOriginalName()));
}
- return names.toArray(new String[names.size()]);
+ return names.toArray(new String[0]);
}
/* (non-Javadoc)
@@ -143,7 +143,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
names.add(fileInfo.getFile().getName());
}
- return names.toArray(new String[names.size()]);
+ return names.toArray(new String[0]);
}
/* (non-Javadoc)
@@ -170,7 +170,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
public String[] getParameterValues(String name) {
List<String> values = parameters.get(name);
if (values != null && values.size() > 0) {
- return values.toArray(new String[values.size()]);
+ return values.toArray(new String[0]);
}
return null;
}
@@ -209,10 +209,10 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
// Interface with Commons FileUpload API
// Using the Streaming API
ServletFileUpload servletFileUpload = new ServletFileUpload();
- if (maxSizeProvided) {
+ if (maxSize != null) {
servletFileUpload.setSizeMax(maxSize);
}
- if (maxFilesProvided) {
+ if (maxFiles != null) {
servletFileUpload.setFileCountMax(maxFiles);
}
FileItemIterator i = servletFileUpload.getItemIterator(request);
@@ -261,7 +261,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
// if maxSize is specified as -1, there is no sanity check and it's
// safe to return true for any request, delegating the failure
// checks later in the upload process.
- if (maxSize == -1 || request == null) {
+ if ((maxSize != null && maxSize == -1) || request == null) {
return true;
}
@@ -289,8 +289,9 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
*/
protected void addFileSkippedError(String fileName, HttpServletRequest request) {
String exceptionMessage = "Skipped file " + fileName + "; request size limit exceeded.";
- FileSizeLimitExceededException exception = new FileUploadBase.FileSizeLimitExceededException(exceptionMessage, getRequestSize(request), maxSize);
- LocalizedMessage message = buildErrorMessage(exception, new Object[]{fileName, getRequestSize(request), maxSize});
+ long allowedMaxSize = maxSize != null ? maxSize : -1;
+ FileSizeLimitExceededException exception = new FileUploadBase.FileSizeLimitExceededException(exceptionMessage, getRequestSize(request), allowedMaxSize);
+ LocalizedMessage message = buildErrorMessage(exception, new Object[]{fileName, getRequestSize(request), allowedMaxSize});
if (!errors.contains(message)) {
errors.add(message);
}
@@ -389,12 +390,12 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
* @throws IOException in case of IO errors
*/
protected boolean streamFileToDisk(FileItemStream itemStream, File file) throws IOException {
- boolean result = false;
+ boolean result;
try (InputStream input = itemStream.openStream();
- OutputStream output = new BufferedOutputStream(new FileOutputStream(file), bufferSize)) {
+ OutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()), bufferSize)) {
byte[] buffer = new byte[bufferSize];
LOG.debug("Streaming file using buffer size {}.", bufferSize);
- for (int length = 0; ((length = input.read(buffer)) > 0); ) {
+ for (int length; ((length = input.read(buffer)) > 0); ) {
output.write(buffer, 0, length);
}
result = true;
@@ -436,9 +437,9 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
private static final long serialVersionUID = 1083158552766906037L;
- private File file;
- private String contentType;
- private String originalName;
+ private final File file;
+ private final String contentType;
+ private final String originalName;
/**
* Default constructor.
diff --git a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
index aaf1f8b12..4eb931231 100644
--- a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
+++ b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
@@ -51,15 +51,15 @@ public class PellMultiPartRequest extends AbstractMultiPartRequest {
//calling the constructor. See javadoc for MultipartRequest.setEncoding().
synchronized (this) {
setEncoding();
- if (maxSizeProvided){
- int intMaxSize = (maxSize >= Integer.MAX_VALUE ? Integer.MAX_VALUE : Long.valueOf(maxSize).intValue());
+ if (maxSize != null && maxSize > -1){
+ int intMaxSize = (maxSize >= Integer.MAX_VALUE ? Integer.MAX_VALUE : maxSize.intValue());
multi = new ServletMultipartRequest(servletRequest, saveDir, intMaxSize);
}else{
multi = new ServletMultipartRequest(servletRequest, saveDir);
}
}
}
-
+
public Enumeration getFileParameterNames() {
return multi.getFileParameterNames();
}