This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch fix/WW-5461-input-name-cherry-pick in repository https://gitbox.apache.org/repos/asf/struts.git
commit 9613325152bf408d39883d952b8087183a99ccf9 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 | 45 +++++++++++----------- .../apache/struts2/showcase/FileUploadTest.java | 9 ++++- .../multipart/JakartaMultiPartRequest.java | 1 + .../multipart/JakartaStreamMultiPartRequest.java | 1 + .../dispatcher/multipart/StrutsUploadedFile.java | 24 +++++++++--- .../struts2/dispatcher/multipart/UploadedFile.java | 9 +++++ .../ActionFileUploadInterceptorTest.java | 5 +++ 8 files changed, 72 insertions(+), 28 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 576772550..e694b75b9 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 @@ -1,51 +1,52 @@ <!-- /* * Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file +* or more contributor license agreements. See the NOTICE file * distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file +* regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at +* with the License. You may obtain a copy of the License at * -* http://www.apache.org/licenses/LICENSE-2.0 +* http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the +* KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <%@ page - language="java" - contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%> + language="java" + contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> - <title>Struts2 Showcase - Fileupload sample</title> + <title>Struts2 Showcase - Fileupload sample</title> </head> <body> <div class="page-header"> - <h1>Fileupload sample</h1> + <h1>Fileupload sample</h1> </div> <div class="container-fluid"> - <div class="row"> - <div class="col-md-12"> - <ul> - <li>ContentType: <s:property value="contentType" /></li> - <li>FileName: <s:property value="fileName" /></li> - <li>Original FileName: <s:property value="originalName" /></li> - <li>File: <s:property value="uploadedFile" /></li> - <li>Size: <s:property value="uploadSize" /></li> - <li>Caption: <s:property value="caption" /></li> - </ul> - </div> - </div> + <div class="row"> + <div class="col-md-12"> + <ul> + <li>ContentType: <s:property value="contentType"/></li> + <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>Size: <s:property value="uploadSize"/></li> + <li>Input name: <s:property value="inputName"/></li> + </ul> + </div> + </div> </div> </body> 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 cb168ab46..00f580d49 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 @@ -24,6 +24,7 @@ import org.htmlunit.html.HtmlForm; import org.htmlunit.html.HtmlInput; import org.htmlunit.html.HtmlPage; import org.htmlunit.html.HtmlSubmitInput; +import org.htmlunit.html.DomElement; import org.junit.Test; import java.io.File; @@ -31,6 +32,11 @@ import java.io.FileWriter; import java.security.SecureRandom; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNotNull; + +import java.nio.file.Files; +import java.nio.file.Paths; public class FileUploadTest { @@ -59,7 +65,8 @@ public class FileUploadTest { "ContentType: text/plain", "Original FileName: " + tempFile.getName(), "Caption: some caption", - "Size: 12" + "Size: 12", + "Input name: upload" ); } } 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 154c174db..3a76adc40 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 @@ -118,6 +118,7 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest { .create(item.getPath().toFile()) .withOriginalName(item.getName()) .withContentType(item.getContentType()) + .withInputName(item.getFieldName()) .build(); values.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 df7b07e22..cebd60250 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 @@ -251,6 +251,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest { .create(file) .withOriginalName(fileName) .withContentType(fileItemInput.getContentType()) + .withInputName(fileItemInput.getFieldName()) .build(); if (uploadedFiles.containsKey(fieldName)) { 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 eb9e93c36..492b46fb0 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,11 +25,13 @@ public class StrutsUploadedFile implements UploadedFile { private final File file; private final String contentType; private final String originalName; + private final String inputName; - 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 @@ -72,18 +74,25 @@ public class StrutsUploadedFile implements UploadedFile { return originalName; } + @Override + public String getInputName() { + return inputName; + } + @Override public String toString() { return "StrutsUploadedFile{" + - "contentType='" + contentType + '\'' + - ", originalName='" + originalName + '\'' + - '}'; + "contentType='" + contentType + '\'' + + ", originalName='" + originalName + '\'' + + ", inputName='" + inputName + '\'' + + '}'; } public static class Builder { private final File file; private String contentType; private String originalName; + private String inputName; private Builder(File file) { this.file = file; @@ -103,8 +112,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 ddebac122..c0af59742 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 @@ -65,4 +65,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 5b63c7147..ec1195490 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 MockHttpServletRequest request;