This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch struts-2-5-x
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/struts-2-5-x by this push:
     new e58edfba8 add some improvements
e58edfba8 is described below

commit e58edfba83189fd17dcce94dc2be57a8b4e1571e
Author: Yasser Zamani <yasserzam...@apache.org>
AuthorDate: Mon Jun 5 01:15:15 2023 +0430

    add some improvements
---
 .../ognl/accessor/XWorkListPropertyAccessor.java   |  5 ++
 .../java/org/apache/struts2/StrutsConstants.java   | 11 ++-
 .../multipart/AbstractMultiPartRequest.java        | 14 +++-
 .../multipart/JakartaMultiPartRequest.java         | 41 ++++++----
 .../org/apache/struts2/default.properties          |  1 +
 .../org/apache/struts2/struts-messages.properties  |  1 +
 .../accessor/XWorkListPropertyAccessorTest.java    | 16 ++--
 .../interceptor/FileUploadInterceptorTest.java     | 89 +++++++++++++++++-----
 8 files changed, 131 insertions(+), 47 deletions(-)

diff --git 
a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessor.java
 
b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessor.java
index 242d09772..e844c1a0b 100644
--- 
a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessor.java
+++ 
b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessor.java
@@ -115,6 +115,11 @@ public class XWorkListPropertyAccessor extends 
ListPropertyAccessor {
             if (listSize <= index) {
                 Object result;
 
+                if (index > autoGrowCollectionLimit) {
+                    throw new OgnlException("Error auto growing collection 
size to " + index + " which limited to "
+                                            + autoGrowCollectionLimit);
+                }
+
                 for (int i = listSize; i < index; i++) {
                     list.add(null);
                 }
diff --git a/core/src/main/java/org/apache/struts2/StrutsConstants.java 
b/core/src/main/java/org/apache/struts2/StrutsConstants.java
index 8c076d24e..30c1bb685 100644
--- a/core/src/main/java/org/apache/struts2/StrutsConstants.java
+++ b/core/src/main/java/org/apache/struts2/StrutsConstants.java
@@ -96,13 +96,13 @@ public final class StrutsConstants {
 
     /** Update freemarker templates cache in seconds */
     public static final String STRUTS_FREEMARKER_TEMPLATES_CACHE_UPDATE_DELAY 
= "struts.freemarker.templatesCache.updateDelay";
-    
+
     /** Cache model instances at BeanWrapper level */
     public static final String STRUTS_FREEMARKER_BEANWRAPPER_CACHE = 
"struts.freemarker.beanwrapperCache";
-    
+
     /** Maximum strong sizing for MruCacheStorage for freemarker */
     public static final String STRUTS_FREEMARKER_MRU_MAX_STRONG_SIZE = 
"struts.freemarker.mru.max.strong.size";
-    
+
     /** org.apache.struts2.views.velocity.VelocityManager implementation class 
*/
     public static final String STRUTS_VELOCITY_MANAGER_CLASSNAME = 
"struts.velocity.manager.classname";
 
@@ -127,6 +127,9 @@ public final class StrutsConstants {
     /** The maximize size of a multipart request (file upload) */
     public static final String STRUTS_MULTIPART_MAXSIZE = 
"struts.multipart.maxSize";
 
+    /** The maximum length of a string parameter in a multipart request. */
+    public static final String STRUTS_MULTIPART_MAX_STRING_LENGTH = 
"struts.multipart.maxStringLength";
+
     /** The directory to use for storing uploaded files */
     public static final String STRUTS_MULTIPART_SAVEDIR = 
"struts.multipart.saveDir";
 
@@ -180,7 +183,7 @@ public final class StrutsConstants {
      * You can specify different prefixes that will be handled by different 
mappers
      */
     public static final String PREFIX_BASED_MAPPER_CONFIGURATION = 
"struts.mapper.prefixMapping";
-    
+
     /** Whether the Struts filter should serve static content or not */
     public static final String STRUTS_SERVE_STATIC_CONTENT = 
"struts.serve.static";
 
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 700364047..76cf75ba8 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
@@ -54,6 +54,11 @@ public abstract class AbstractMultiPartRequest implements 
MultiPartRequest {
     protected long maxSize;
     protected boolean maxSizeProvided;
 
+    /**
+     * Specifies the maximum length of a string parameter in a multipart 
request.
+     */
+    protected Long maxStringLength;
+
     /**
      * Specifies the buffer size to use during streaming.
      */
@@ -88,6 +93,11 @@ public abstract class AbstractMultiPartRequest implements 
MultiPartRequest {
         this.maxSize = Long.parseLong(maxSize);
     }
 
+    @Inject(StrutsConstants.STRUTS_MULTIPART_MAX_STRING_LENGTH)
+    public void setMaxStringLength(String maxStringLength) {
+        this.maxStringLength = Long.parseLong(maxStringLength);
+    }
+
     @Inject
     public void setLocaleProviderFactory(LocaleProviderFactory 
localeProviderFactory) {
         defaultLocale = 
localeProviderFactory.createLocaleProvider().getLocale();
@@ -134,9 +144,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 800d54280..5b03a262e 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
@@ -25,6 +25,7 @@ import org.apache.commons.fileupload.RequestContext;
 import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.struts2.dispatcher.LocalizedMessage;
@@ -55,9 +56,8 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
      *
      * @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 {
+    public void parse(HttpServletRequest request, String saveDir) {
         try {
             setLocale(request);
             processUpload(request, saveDir);
@@ -126,13 +126,26 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             values = new ArrayList<>();
         }
 
-        // note: see http://jira.opensymphony.com/browse/WW-633
-        // basically, in some cases the charset may be null, so
-        // we're just going to try to "other" method (no idea if this
-        // will work)
-        if (charset != null) {
+        long size = item.getSize();
+        if (size == 0) {
+            values.add(StringUtils.EMPTY);
+        } else if (size > maxStringLength) {
+            String errorKey = 
"struts.messages.upload.error.parameter.too.long";
+            LocalizedMessage localizedMessage = new 
LocalizedMessage(this.getClass(), errorKey, null,
+                    new Object[] { item.getFieldName(), maxStringLength, size 
});
+
+            if (!errors.contains(localizedMessage)) {
+                errors.add(localizedMessage);
+            }
+            return;
+
+        } else if (charset != null) {
             values.add(item.getString(charset));
         } else {
+            // note: see http://jira.opensymphony.com/browse/WW-633
+            // basically, in some cases the charset may be null, so
+            // we're just going to try to "other" method (no idea if this
+            // will work)
             values.add(item.getString());
         }
         params.put(item.getFieldName(), values);
@@ -183,7 +196,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             contentTypes.add(fileItem.getContentType());
         }
 
-        return contentTypes.toArray(new String[contentTypes.size()]);
+        return contentTypes.toArray(new String[0]);
     }
 
     /* (non-Javadoc)
@@ -209,7 +222,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             fileList.add(new StrutsUploadedFile(storeLocation));
         }
 
-        return fileList.toArray(new UploadedFile[fileList.size()]);
+        return fileList.toArray(new UploadedFile[0]);
     }
 
     /* (non-Javadoc)
@@ -227,7 +240,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             fileNames.add(getCanonicalName(fileItem.getName()));
         }
 
-        return fileNames.toArray(new String[fileNames.size()]);
+        return fileNames.toArray(new String[0]);
     }
 
     /* (non-Javadoc)
@@ -245,7 +258,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             fileNames.add(((DiskFileItem) 
fileItem).getStoreLocation().getName());
         }
 
-        return fileNames.toArray(new String[fileNames.size()]);
+        return fileNames.toArray(new String[0]);
     }
 
     /* (non-Javadoc)
@@ -253,7 +266,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
      */
     public String getParameter(String name) {
         List<String> v = params.get(name);
-        if (v != null && v.size() > 0) {
+        if (v != null && !v.isEmpty()) {
             return v.get(0);
         }
 
@@ -272,8 +285,8 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
      */
     public String[] getParameterValues(String name) {
         List<String> v = params.get(name);
-        if (v != null && v.size() > 0) {
-            return v.toArray(new String[v.size()]);
+        if (v != null && !v.isEmpty()) {
+            return v.toArray(new String[0]);
         }
 
         return null;
diff --git a/core/src/main/resources/org/apache/struts2/default.properties 
b/core/src/main/resources/org/apache/struts2/default.properties
index c84452d2e..3e2ef95bb 100644
--- a/core/src/main/resources/org/apache/struts2/default.properties
+++ b/core/src/main/resources/org/apache/struts2/default.properties
@@ -68,6 +68,7 @@ struts.multipart.parser=jakarta
 # uses javax.servlet.context.tempdir by default
 struts.multipart.saveDir=
 struts.multipart.maxSize=2097152
+struts.multipart.maxStringLength=4096
 
 ### Load custom property files (does not override struts.properties!)
 # struts.custom.properties=application,org/apache/struts2/extension/custom
diff --git 
a/core/src/main/resources/org/apache/struts2/struts-messages.properties 
b/core/src/main/resources/org/apache/struts2/struts-messages.properties
index aa6e842e4..3a01420f6 100644
--- a/core/src/main/resources/org/apache/struts2/struts-messages.properties
+++ b/core/src/main/resources/org/apache/struts2/struts-messages.properties
@@ -26,6 +26,7 @@ struts.messages.invalid.content.type=Could not find a 
Content-Type for {0}. Veri
 struts.messages.removing.file=Removing file {0} {1}
 struts.messages.error.uploading=Error uploading: {0}
 struts.messages.error.file.too.large=File {0} is too large to be uploaded. 
Maximum allowed size is {4} bytes!
+struts.messages.upload.error.parameter.too.long=The request parameter "{0}" 
was too long.  Max length allowed is {1}, but found {2}!
 struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} 
"{1}" "{2}" {3}
 struts.messages.error.file.extension.not.allowed=File extension not allowed: 
{0} "{1}" "{2}" {3}
 
diff --git 
a/core/src/test/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessorTest.java
 
b/core/src/test/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessorTest.java
index 542efda77..a5da080a0 100644
--- 
a/core/src/test/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessorTest.java
+++ 
b/core/src/test/java/com/opensymphony/xwork2/ognl/accessor/XWorkListPropertyAccessorTest.java
@@ -22,7 +22,7 @@ import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.XWorkTestCase;
 import com.opensymphony.xwork2.util.ListHolder;
 import com.opensymphony.xwork2.util.ValueStack;
-import ognl.ListPropertyAccessor;
+import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
 import ognl.PropertyAccessor;
 
 import java.util.ArrayList;
@@ -42,11 +42,11 @@ public class XWorkListPropertyAccessorTest extends 
XWorkTestCase {
 
         assertNotNull(listHolder.getLongs());
         assertEquals(3, listHolder.getLongs().size());
-        assertEquals(new Long(1), (Long) listHolder.getLongs().get(0));
-        assertEquals(new Long(2), (Long) listHolder.getLongs().get(1));
-        assertEquals(new Long(3), (Long) listHolder.getLongs().get(2));
+        assertEquals(new Long(1), listHolder.getLongs().get(0));
+        assertEquals(new Long(2), listHolder.getLongs().get(1));
+        assertEquals(new Long(3), listHolder.getLongs().get(2));
 
-        assertTrue(((Boolean) 
vs.findValue("longs.contains(1)")).booleanValue());
+        assertTrue((Boolean) vs.findValue("longs.contains(1)"));
     }
 
     public void testCanAccessListSizeProperty() {
@@ -60,8 +60,8 @@ public class XWorkListPropertyAccessorTest extends 
XWorkTestCase {
 
         vs.push(listHolder);
 
-        assertEquals(new Integer(myList.size()), 
vs.findValue("strings.size()"));
-        assertEquals(new Integer(myList.size()), vs.findValue("strings.size"));
+        assertEquals(myList.size(), vs.findValue("strings.size()"));
+        assertEquals(myList.size(), vs.findValue("strings.size"));
     }
 
     public void testAutoGrowthCollectionLimit() {
@@ -73,12 +73,14 @@ public class XWorkListPropertyAccessorTest extends 
XWorkTestCase {
         listHolder.setStrings(myList);
 
         ValueStack vs = ActionContext.getContext().getValueStack();
+        ReflectionContextState.setCreatingNullObjects(vs.getContext(), true);
         vs.push(listHolder);
 
         vs.setValue("strings[0]", "a");
         vs.setValue("strings[1]", "b");
         vs.setValue("strings[2]", "c");
         vs.setValue("strings[3]", "d");
+        vs.findValue("strings[3]");
 
         assertEquals(3, vs.findValue("strings.size()"));
     }
diff --git 
a/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
 
b/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
index 3e621b1a4..14d2feb0e 100644
--- 
a/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
+++ 
b/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
@@ -89,7 +89,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
     private File tempDir;
     private TestAction action;
 
-    public void testAcceptFileWithEmptyAllowedTypesAndExtensions() throws 
Exception {
+    public void testAcceptFileWithEmptyAllowedTypesAndExtensions() {
         // when allowed type is empty
         ValidationAwareSupport validation = new ValidationAwareSupport();
         boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename", 
"text/plain", "inputName", validation);
@@ -99,7 +99,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertFalse(validation.hasErrors());
     }
 
-    public void testAcceptFileWithoutEmptyTypes() throws Exception {
+    public void testAcceptFileWithoutEmptyTypes() {
         interceptor.setAllowedTypes("text/plain");
 
         // when file is of allowed types
@@ -120,7 +120,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
     }
 
 
-    public void testAcceptFileWithWildcardContent() throws Exception {
+    public void testAcceptFileWithWildcardContent() {
         interceptor.setAllowedTypes("text/*");
 
         ValidationAwareSupport validation = new ValidationAwareSupport();
@@ -139,7 +139,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertTrue(validation.hasErrors());
     }
 
-    public void testAcceptFileWithoutEmptyExtensions() throws Exception {
+    public void testAcceptFileWithoutEmptyExtensions() {
         interceptor.setAllowedExtensions(".txt");
 
         // when file is of allowed extensions
@@ -168,7 +168,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertFalse(validation.hasErrors());
     }
 
-    public void testAcceptFileWithNoFile() throws Exception {
+    public void testAcceptFileWithNoFile() {
         FileUploadInterceptor interceptor = new FileUploadInterceptor();
         interceptor.setAllowedTypes("text/plain");
 
@@ -188,7 +188,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
 
     public void testAcceptFileWithMaxSize() throws Exception {
         interceptor.setAllowedTypes("text/plain");
-        interceptor.setMaximumSize(new Long(10));
+        interceptor.setMaximumSize(10L);
 
         // when file is not of allowed types
         ValidationAwareSupport validation = new ValidationAwareSupport();
@@ -201,7 +201,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertFalse(notOk);
         assertFalse(validation.getFieldErrors().isEmpty());
         assertTrue(validation.hasErrors());
-        List errors = (List) validation.getFieldErrors().get("inputName");
+        List errors = validation.getFieldErrors().get("inputName");
         assertEquals(1, errors.size());
         String msg = (String) errors.get(0);
         // the error message should contain at least this test
@@ -235,7 +235,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         mai.setInvocationContext(ActionContext.getContext());
 
         
ActionContext.getContext().setParameters(HttpParameters.create().build());
-        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000));
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000, -1));
 
         interceptor.intercept(mai);
 
@@ -257,7 +257,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         mai.setInvocationContext(ActionContext.getContext());
 
         
ActionContext.getContext().setParameters(HttpParameters.create().build());
-        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000));
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000, -1));
 
         interceptor.intercept(mai);
 
@@ -278,7 +278,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
                 "Unit test of FileUploadInterceptor" +
                 "\r\n" +
                 "-----1234--\r\n");
-        req.setContent(content.getBytes("US-ASCII"));
+        req.setContent(content.getBytes(StandardCharsets.US_ASCII));
 
         MyFileupAction action = new MyFileupAction();
 
@@ -288,14 +288,14 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         mai.setInvocationContext(ActionContext.getContext());
         Map<String, Object> param = new HashMap<>();
         
ActionContext.getContext().setParameters(HttpParameters.create(param).build());
-        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000));
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000, -1));
 
         interceptor.intercept(mai);
 
-        assertTrue(!action.hasErrors());
+        assertFalse(action.hasErrors());
 
         HttpParameters parameters = mai.getInvocationContext().getParameters();
-        assertTrue(parameters.keySet().size() == 3);
+        assertEquals(3, parameters.keySet().size());
         UploadedFile[] files = (UploadedFile[]) 
parameters.get("file").getObject();
         String[] fileContentTypes = 
parameters.get("fileContentType").getMultipleValues();
         String[] fileRealFilenames = 
parameters.get("fileFileName").getMultipleValues();
@@ -303,9 +303,9 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertNotNull(files);
         assertNotNull(fileContentTypes);
         assertNotNull(fileRealFilenames);
-        assertTrue(files.length == 1);
-        assertTrue(fileContentTypes.length == 1);
-        assertTrue(fileRealFilenames.length == 1);
+        assertEquals(1, files.length);
+        assertEquals(1, fileContentTypes.length);
+        assertEquals(1, fileRealFilenames.length);
         assertEquals("text/html", fileContentTypes[0]);
         assertNotNull("deleteme.txt", fileRealFilenames[0]);
     }
@@ -349,7 +349,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         mai.setInvocationContext(ActionContext.getContext());
         Map<String, Object> param = new HashMap<String, Object>();
         
ActionContext.getContext().setParameters(HttpParameters.create(param).build());
-        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000));
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 2000, -1));
 
         interceptor.setAllowedTypes("text/html");
         interceptor.intercept(mai);
@@ -370,6 +370,54 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         assertNotNull("test1.html", fileRealFilenames[0]);
     }
 
+    public void testMultipartRequestMaxStringLength() throws Exception {
+        MockHttpServletRequest req = new MockHttpServletRequest();
+        req.setCharacterEncoding(StandardCharsets.UTF_8.name());
+        req.setMethod("post");
+        req.addHeader("Content-type", "multipart/form-data; boundary=---1234");
+
+        // inspired by the unit tests for jakarta commons fileupload
+        String content = ("-----1234\r\n" +
+                "Content-Disposition: form-data; name=\"file\"; 
filename=\"deleteme.txt\"\r\n" +
+                "Content-Type: text/html\r\n" +
+                "\r\n" +
+                "Unit test of FileUploadInterceptor" +
+                "\r\n" +
+                "-----1234\r\n" +
+                "Content-Disposition: form-data; 
name=\"normalFormField1\"\r\n" +
+                "\r\n" +
+                "it works" +
+                "\r\n" +
+                "-----1234\r\n" +
+                "Content-Disposition: form-data; 
name=\"normalFormField2\"\r\n" +
+                "\r\n" +
+                "long string should not work" +
+                "\r\n" +
+                "-----1234--\r\n");
+        req.setContent(content.getBytes(StandardCharsets.US_ASCII));
+
+        MyFileupAction action = container.inject(MyFileupAction.class);
+
+        MockActionInvocation mai = new MockActionInvocation();
+        mai.setAction(action);
+        mai.setResultCode("success");
+        mai.setInvocationContext(ActionContext.getContext());
+        Map<String, Object> param = new HashMap<>();
+        
ActionContext.getContext().setParameters(HttpParameters.create(param).build());
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, -1, 20));
+
+        interceptor.intercept(mai);
+
+        assertTrue(action.hasActionErrors());
+
+        Collection<String> errors = action.getActionErrors();
+        assertEquals(1, errors.size());
+        String msg = errors.iterator().next();
+        assertEquals(
+                "The request parameter \"normalFormField2\" was too long.  Max 
length allowed is 20, but found 27!",
+                msg);
+    }
+
     public void testMultipartRequestLocalizedError() throws Exception {
         MockHttpServletRequest req = new MockHttpServletRequest();
         req.setCharacterEncoding(StandardCharsets.UTF_8.name());
@@ -384,7 +432,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
                 "Unit test of FileUploadInterceptor" +
                 "\r\n" +
                 "-----1234--\r\n");
-        req.setContent(content.getBytes("US-ASCII"));
+        req.setContent(content.getBytes(StandardCharsets.US_ASCII));
 
         MyFileupAction action = container.inject(MyFileupAction.class);
 
@@ -396,7 +444,7 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         
ActionContext.getContext().setParameters(HttpParameters.create(param).build());
         // set German locale
         ActionContext.getContext().setLocale(Locale.GERMAN);
-        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 10));
+        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, 
createMultipartRequest(req, 10, -1));
 
         interceptor.intercept(mai);
 
@@ -429,9 +477,10 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         return sb.toString();
     }
 
-    private MultiPartRequestWrapper createMultipartRequest(HttpServletRequest 
req, int maxsize) throws IOException {
+    private MultiPartRequestWrapper createMultipartRequest(HttpServletRequest 
req, int maxsize, int maxStringLength) {
         JakartaMultiPartRequest jak = new JakartaMultiPartRequest();
         jak.setMaxSize(String.valueOf(maxsize));
+        jak.setMaxStringLength(String.valueOf(maxStringLength));
         return new MultiPartRequestWrapper(jak, req, 
tempDir.getAbsolutePath(), new DefaultLocaleProvider());
     }
 

Reply via email to