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

lukaszlenart pushed a commit to branch fix/WW-5461-input-name
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 735d1bc88d9beceb18558d12f565a466f96a5b2a
Author: Lukasz Lenart <lukaszlen...@apache.org>
AuthorDate: Tue Sep 3 07:40:17 2024 +0200

    WW-5461 Extends UploadedFile with inputName field
---
 .../showcase/fileupload/FileUploadAction.java      |  6 ++++
 .../webapp/WEB-INF/fileupload/upload-success.jsp   |  3 +-
 .../apache/struts2/showcase/FileUploadTest.java    | 32 ++++++++++++++++++++--
 .../multipart/JakartaMultiPartRequest.java         |  1 +
 .../multipart/JakartaStreamMultiPartRequest.java   | 15 ++++++++--
 .../dispatcher/multipart/StrutsUploadedFile.java   | 19 +++++++++++--
 .../struts2/dispatcher/multipart/UploadedFile.java |  9 ++++++
 .../ActionFileUploadInterceptorTest.java           |  5 ++++
 .../interceptor/FileUploadInterceptorTest.java     |  5 ++++
 .../dispatcher/multipart/PellMultiPartRequest.java |  1 +
 10 files changed, 88 insertions(+), 8 deletions(-)

diff --git 
a/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
 
b/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
index 279c1e928..bdcc2e5bb 100644
--- 
a/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
+++ 
b/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
@@ -37,6 +37,7 @@ public class FileUploadAction extends ActionSupport 
implements UploadedFilesAwar
     private String fileName;
     private String caption;
     private String originalName;
+    private String inputName;
 
     public String input() throws Exception {
         return SUCCESS;
@@ -58,6 +59,10 @@ public class FileUploadAction extends ActionSupport 
implements UploadedFilesAwar
         return originalName;
     }
 
+    public String getInputName() {
+        return inputName;
+    }
+
     public Object getUploadedFile() {
         return uploadedFile.getContent();
     }
@@ -85,5 +90,6 @@ public class FileUploadAction extends ActionSupport 
implements UploadedFilesAwar
         this.fileName = uploadedFile.getName();
         this.contentType = uploadedFile.getContentType();
         this.originalName = uploadedFile.getOriginalName();
+        this.inputName = uploadedFile.getInputName();
     }
 }
diff --git 
a/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp 
b/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
index a1b06277a..4d42c7e45 100644
--- a/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
+++ b/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
@@ -41,7 +41,8 @@
                        <li>FileName: <s:property value="fileName" /></li>
                        <li>Original FileName: <s:property value="originalName" 
/></li>
                        <li>File: <s:property value="uploadedFile" /></li>
-                       <li>Caption:<s:property value="caption" /></li>
+                       <li>Caption: <s:property value="caption" /></li>
+                       <li id="input-name">Input name: <s:property 
value="inputName" /></li>
                </ul>
                </div>
        </div>
diff --git 
a/apps/showcase/src/test/java/it/org/apache/struts2/showcase/FileUploadTest.java
 
b/apps/showcase/src/test/java/it/org/apache/struts2/showcase/FileUploadTest.java
index 498d4d4ad..e4f5f882d 100644
--- 
a/apps/showcase/src/test/java/it/org/apache/struts2/showcase/FileUploadTest.java
+++ 
b/apps/showcase/src/test/java/it/org/apache/struts2/showcase/FileUploadTest.java
@@ -25,10 +25,14 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm;
 import com.gargoylesoftware.htmlunit.html.HtmlInput;
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
-import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 public class FileUploadTest {
 
@@ -45,9 +49,31 @@ public class FileUploadTest {
             uploadInput.setValueAttribute(tempFile.getAbsolutePath());
             final HtmlSubmitInput button = form.getInputByValue("Submit");
             final HtmlPage resultPage = button.click();
+
             DomElement errorMessage = 
resultPage.getFirstByXPath("//span[@class='errorMessage']");
-            Assert.assertNotNull(errorMessage);
-            Assert.assertEquals("File cannot be empty", 
errorMessage.getVisibleText());
+            assertNotNull(errorMessage);
+            assertEquals("File cannot be empty", 
errorMessage.getVisibleText());
+        }
+    }
+
+    @Test
+    public void testFileUpload() throws Exception {
+        try (final WebClient webClient = new WebClient()) {
+            final HtmlPage page = 
webClient.getPage(ParameterUtils.getBaseUrl() + "/fileupload/doUpload.action");
+            final HtmlForm form = page.getFormByName("doUpload");
+            HtmlInput captionInput = form.getInputByName("caption");
+            HtmlFileInput uploadInput = form.getInputByName("upload");
+            captionInput.type("some caption");
+            File tempFile = File.createTempFile("testEmptyFile", ".tmp");
+            Files.write(Paths.get(tempFile.toURI()), "some 
content".getBytes());
+            tempFile.deleteOnExit();
+            uploadInput.setValueAttribute(tempFile.getAbsolutePath());
+            final HtmlSubmitInput button = form.getInputByValue("Submit");
+            final HtmlPage resultPage = button.click();
+
+            DomElement inputName = resultPage.getElementById("input-name");
+            assertNotNull(inputName);
+            assertEquals("Input name: upload", 
inputName.getTextContent().trim());
         }
     }
 
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 1c0f458a0..491d3d41d 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
@@ -246,6 +246,7 @@ public class JakartaMultiPartRequest extends 
AbstractMultiPartRequest {
             UploadedFile uploadedFile = 
StrutsUploadedFile.Builder.create(storeLocation)
                 .withContentType(fileItem.getContentType())
                 .withOriginalName(fileItem.getName())
+                .withInputName(fileItem.getFieldName())
                 .build();
             fileList.add(uploadedFile);
         }
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 428ae112f..3985e0f52 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
@@ -113,6 +113,7 @@ public class JakartaStreamMultiPartRequest extends 
AbstractMultiPartRequest {
             StrutsUploadedFile.Builder.create(fileInfo.getFile())
                 .withContentType(fileInfo.contentType)
                 .withOriginalName(fileInfo.originalName)
+                .withInputName(fileInfo.getInputName())
                 .build()
         ).toArray(UploadedFile[]::new);
     }
@@ -423,7 +424,7 @@ public class JakartaStreamMultiPartRequest extends 
AbstractMultiPartRequest {
         String fileName = itemStream.getName();
         String fieldName = itemStream.getFieldName();
         // create internal structure
-        FileInfo fileInfo = new FileInfo(file, itemStream.getContentType(), 
fileName);
+        FileInfo fileInfo = new FileInfo(file, itemStream.getContentType(), 
fileName, itemStream.getFieldName());
         // append or create new entry.
         if (!fileInfos.containsKey(fieldName)) {
             List<FileInfo> infos = new ArrayList<>();
@@ -447,6 +448,7 @@ public class JakartaStreamMultiPartRequest extends 
AbstractMultiPartRequest {
         private final File file;
         private final String contentType;
         private final String originalName;
+        private final String inputName;
 
         /**
          * Default constructor.
@@ -455,10 +457,11 @@ public class JakartaStreamMultiPartRequest extends 
AbstractMultiPartRequest {
          * @param contentType  content type
          * @param originalName original file name
          */
-        public FileInfo(File file, String contentType, String originalName) {
+        public FileInfo(File file, String contentType, String originalName, 
String inputName) {
             this.file = file;
             this.contentType = contentType;
             this.originalName = originalName;
+            this.inputName = inputName;
         }
 
         /**
@@ -481,6 +484,14 @@ public class JakartaStreamMultiPartRequest extends 
AbstractMultiPartRequest {
         public String getOriginalName() {
             return originalName;
         }
+
+        /**
+         * @return file input name
+         * @since 6.7.0
+         */
+        public String getInputName() {
+            return inputName;
+        }
     }
 
 }
diff --git 
a/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
 
b/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
index 5976f578f..eb9feccc5 100644
--- 
a/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
+++ 
b/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
@@ -25,6 +25,7 @@ public class StrutsUploadedFile implements UploadedFile {
     private final File file;
     private final String contentType;
     private final String originalName;
+    private final String inputName;
 
     /**
      * Use builder instead of constructor
@@ -36,12 +37,14 @@ public class StrutsUploadedFile implements UploadedFile {
         this.file = file;
         this.contentType = null;
         this.originalName = null;
+        this.inputName = null;
     }
 
-    private StrutsUploadedFile(File file, String contentType, String 
originalName) {
+    private StrutsUploadedFile(File file, String contentType, String 
originalName, String inputName) {
         this.file = file;
         this.contentType = contentType;
         this.originalName = originalName;
+        this.inputName = inputName;
     }
 
     @Override
@@ -84,11 +87,17 @@ public class StrutsUploadedFile implements UploadedFile {
         return originalName;
     }
 
+    @Override
+    public String getInputName() {
+        return inputName;
+    }
+
     @Override
     public String toString() {
         return "StrutsUploadedFile{" +
             "contentType='" + contentType + '\'' +
             ", originalName='" + originalName + '\'' +
+            ", inputName='" + inputName + '\'' +
             '}';
     }
 
@@ -96,6 +105,7 @@ public class StrutsUploadedFile implements UploadedFile {
         private final File file;
         private String contentType;
         private String originalName;
+        private String inputName;
 
         private Builder(File file) {
             this.file = file;
@@ -115,8 +125,13 @@ public class StrutsUploadedFile implements UploadedFile {
             return this;
         }
 
+        public Builder withInputName(String inputName) {
+            this.inputName = inputName;
+            return this;
+        }
+
         public UploadedFile build() {
-            return new StrutsUploadedFile(this.file, this.contentType, 
this.originalName);
+            return new StrutsUploadedFile(this.file, this.contentType, 
this.originalName, this.inputName);
         }
     }
 }
diff --git 
a/core/src/main/java/org/apache/struts2/dispatcher/multipart/UploadedFile.java 
b/core/src/main/java/org/apache/struts2/dispatcher/multipart/UploadedFile.java
index ada27ff6c..728a98e43 100644
--- 
a/core/src/main/java/org/apache/struts2/dispatcher/multipart/UploadedFile.java
+++ 
b/core/src/main/java/org/apache/struts2/dispatcher/multipart/UploadedFile.java
@@ -41,4 +41,13 @@ public interface UploadedFile extends Serializable {
 
     String getContentType();
 
+    /**
+     * Represents a name of the input file, eg.:
+     * "myFile" in case of <input type="file" name="myFile">
+     *
+     * @return name of the input file field
+     * @since 6.7.0
+     */
+    String getInputName();
+
 }
diff --git 
a/core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java
 
b/core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java
index 51747b147..1fbb5017b 100644
--- 
a/core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java
+++ 
b/core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java
@@ -91,6 +91,11 @@ public class ActionFileUploadInterceptorTest extends 
StrutsInternalTestCase {
         public String getContentType() {
             return null;
         }
+
+        @Override
+        public String getInputName() {
+            return null;
+        }
     };
 
     private ActionFileUploadInterceptor interceptor;
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 cfb305770..040a2f61a 100644
--- 
a/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
+++ 
b/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
@@ -93,6 +93,11 @@ public class FileUploadInterceptorTest extends 
StrutsInternalTestCase {
         public String getContentType() {
             return null;
         }
+
+        @Override
+        public String getInputName() {
+            return null;
+        }
     };
 
     private FileUploadInterceptor interceptor;
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 d2db975d4..ef5019d4b 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
@@ -71,6 +71,7 @@ public class PellMultiPartRequest extends 
AbstractMultiPartRequest {
         return new 
UploadedFile[]{StrutsUploadedFile.Builder.create(multi.getFile(fieldName))
             .withContentType(multi.getContentType(fieldName))
             .withOriginalName(multi.getFileSystemName(fieldName))
+            .withInputName(fieldName)
             .build()
         };
     }

Reply via email to