From 735d1bc88d9beceb18558d12f565a466f96a5b2a Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 3 Sep 2024 07:40:17 +0200 Subject: [PATCH 1/6] WW-5461 Extends UploadedFile with inputName field --- .../showcase/fileupload/FileUploadAction.java | 6 ++++ .../WEB-INF/fileupload/upload-success.jsp | 3 +- .../struts2/showcase/FileUploadTest.java | 32 +++++++++++++++++-- .../multipart/JakartaMultiPartRequest.java | 1 + .../JakartaStreamMultiPartRequest.java | 15 +++++++-- .../multipart/StrutsUploadedFile.java | 19 +++++++++-- .../dispatcher/multipart/UploadedFile.java | 9 ++++++ .../ActionFileUploadInterceptorTest.java | 5 +++ .../FileUploadInterceptorTest.java | 5 +++ .../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 @@
  • FileName:
  • Original FileName:
  • File:
  • -
  • Caption:
  • +
  • Caption:
  • +
  • Input name:
  • 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 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 + * + * @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() }; } From 0c69993cd7ef218d7456375e7a48a92782320248 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:41:09 +0000 Subject: [PATCH 2/6] Bump maven-surefire-plugin.version from 3.4.0 to 3.5.0 Bumps `maven-surefire-plugin.version` from 3.4.0 to 3.5.0. Updates `org.apache.maven.surefire:surefire-junit47` from 3.4.0 to 3.5.0 Updates `org.apache.maven.plugins:maven-surefire-plugin` from 3.4.0 to 3.5.0 - [Release notes](https://github.com/apache/maven-surefire/releases) - [Commits](https://github.com/apache/maven-surefire/compare/surefire-3.4.0...surefire-3.5.0) --- updated-dependencies: - dependency-name: org.apache.maven.surefire:surefire-junit47 dependency-type: direct:production update-type: version-update:semver-minor - dependency-name: org.apache.maven.plugins:maven-surefire-plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 67aa38827..622c8edd4 100644 --- a/pom.xml +++ b/pom.xml @@ -117,7 +117,7 @@ 5.3.39 3.0.8 1.0.7 - 3.4.0 + 3.5.0 6.2.4.Final 2.3.33 From efab3fc6fa35f748636feb54bc62fdf9b4378674 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:41:13 +0000 Subject: [PATCH 3/6] Bump org.assertj:assertj-core from 3.25.3 to 3.26.3 Bumps [org.assertj:assertj-core](https://github.com/assertj/assertj) from 3.25.3 to 3.26.3. - [Release notes](https://github.com/assertj/assertj/releases) - [Commits](https://github.com/assertj/assertj/compare/assertj-build-3.25.3...assertj-build-3.26.3) --- updated-dependencies: - dependency-name: org.assertj:assertj-core dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 67aa38827..8f3bba860 100644 --- a/pom.xml +++ b/pom.xml @@ -985,7 +985,7 @@ org.assertj assertj-core - 3.25.3 + 3.26.3 test From 3111c4b86f6ee50a54ac6b1115028adb43813913 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:41:19 +0000 Subject: [PATCH 4/6] Bump org.apache.maven.plugins:maven-dependency-plugin Bumps [org.apache.maven.plugins:maven-dependency-plugin](https://github.com/apache/maven-dependency-plugin) from 3.6.1 to 3.8.0. - [Release notes](https://github.com/apache/maven-dependency-plugin/releases) - [Commits](https://github.com/apache/maven-dependency-plugin/compare/maven-dependency-plugin-3.6.1...maven-dependency-plugin-3.8.0) --- updated-dependencies: - dependency-name: org.apache.maven.plugins:maven-dependency-plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 67aa38827..573873e95 100644 --- a/pom.xml +++ b/pom.xml @@ -291,7 +291,7 @@ org.apache.maven.plugins maven-dependency-plugin - 3.6.1 + 3.8.0 org.apache.maven.plugins From 4c95983abb604729cbbc7ccdb0e78e11a594b259 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:41:23 +0000 Subject: [PATCH 5/6] Bump org.jfree:jfreechart from 1.5.4 to 1.5.5 Bumps [org.jfree:jfreechart](https://github.com/jfree/jfreechart) from 1.5.4 to 1.5.5. - [Release notes](https://github.com/jfree/jfreechart/releases) - [Commits](https://github.com/jfree/jfreechart/compare/v1.5.4...v1.5.5) --- updated-dependencies: - dependency-name: org.jfree:jfreechart dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- plugins/jfreechart/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/jfreechart/pom.xml b/plugins/jfreechart/pom.xml index 50cc5cbe0..39dbb90ef 100644 --- a/plugins/jfreechart/pom.xml +++ b/plugins/jfreechart/pom.xml @@ -47,7 +47,7 @@ org.jfree jfreechart - 1.5.4 + 1.5.5 provided From 280c3e4f5f56bb6d8f8439c3c7212d7b52565018 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 01:41:29 +0000 Subject: [PATCH 6/6] Bump org.awaitility:awaitility from 4.2.1 to 4.2.2 Bumps [org.awaitility:awaitility](https://github.com/awaitility/awaitility) from 4.2.1 to 4.2.2. - [Changelog](https://github.com/awaitility/awaitility/blob/master/changelog.txt) - [Commits](https://github.com/awaitility/awaitility/compare/awaitility-4.2.1...awaitility-4.2.2) --- updated-dependencies: - dependency-name: org.awaitility:awaitility dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 67aa38827..4a97f0659 100644 --- a/pom.xml +++ b/pom.xml @@ -777,7 +777,7 @@ org.awaitility awaitility - 4.2.1 + 4.2.2 test